Saturday, May 20, 2006

Java synchronization

Consider the following cases of synchronization, and think of what should be the result in each case: 1. Object "obj" has two methods - M1() and M2(). None of them is synchronized. Two threads T1 and T2, want to run M1() and M2() respectively. Both threads share a common obj object. What will be the result? Result: Each thread will be able to execute its desired method. 2. Object "obj" has two methods - M1() and M2(). Only M2 is synchronized. Two threads T1 and T2, want to run M1() and M2() respectively. Both threads share a common obj object. What will be the result? Result: Each thread will be able to execute its desired method. Since T1 does not need a lock on obj to execute M1(). 3. Object "obj" has two methods - M1() and M2(). Both are synchronized. Two threads T1 and T2, want to run M1() and M2() respectively. Both threads share a common obj object. What will be the result? Result: Since to execute each method a lock is needed on the object, the first thread to get a lock on obj will be able to execute its desired method. 4. Object "obj" has one synchronized - M() . Two threads T1 and T2, want to run M(). Both threads have share a common obj object. What will be the result? Result: The thread that first gets the lock is able to execute the method first. And if this thread does not leave the object lock, then the other thread keeps on waiting. Since each thread needs a lock on obj. Download source code here. Exercise : What will happen if the methods above are static?

Tuesday, May 16, 2006

Karma and Birthdays!

Two days ago, yours truly became a quarter-century old! let me rephrase it: I've lived quarter of a century! The eve of my birthday, saw another of my mood swings - ones which make me dwell on life as such. When I'm in such a mood, I end up in melancholy. Every birthday starts with my reflection on what I have achieved so far in life. What has been my karma? And have I been successful in my dharma? All my friends say that I try to do much. And I say I'm just trying to enjoy my life, in truth I do feel that one life is too less. I guess now I understand why some people strive for immortality, because we find the world around us too intriguing, too beautiful and too lovely to leave. We want to know all the mysteries of life. I will not be there when we first talk to aliens. I will not be there when we can travel at the speed of light. I will not be there when we travel to new worlds. I will not be there when we get to know new civilizations. I will not be there when we get to know new stories and tales. I will not be there when the world we live is changed forever. But I want to be! Come to think of it, I also want to live in the past. I was not there when Ramayana happened in India. I was not there when Mahabharata occured. I was not there when Mughals ruled India. I was not there when India fought for its Independence. I was not there when the World Wars happened. But I wish I was. I feel a subliminal attraction to the thought of birth-rebirth. Was I a soldier in Tipu Sultan's army. Was I with the Pandavas or the Kauravas? Did I ever meet Lord Krishna? I do not know what other lives I have lived. But I want to know. And then there is this life. One day I shall die, and I shall be born again, and I shall have no rememberance of this life. Yet, I want to live this life to the fullest too. For, life is precious it has allowed me live all these long years, meet all the people I like or dislike, see all these things, visit all the places - my life a beautiful life. And yet again I ponder, what is my karma? What is it that for which I was reborn in this world?

Thursday, May 11, 2006

When does finally() block run in the Java?

Consider the following code: What will be the output? (NOTE: The answers are in the color of the background so select the text, so see the answer) public class FinallyTest { public static void main(String[] args) { try { int x = Integer.parseInt(args[0]); return; } catch(Exception e) { System.out.println("There was an exception"); } finally { System.out.println("Ran the finally code"); } } } ******************************* Scenario 1 : No input is given Result: There was an exception Ran the finally code Scenario 2 : Pass '1' as argument on command line Result: Ran the finally code ******************************* Now consider this code: public class FinallyTest 2 { public static void main(String[] args) { try { int x = Integer.parseInt(args[0]); } catch(Exception e) { System.out.println("There was an exception"); return; } finally { System.out.println("Ran the finally code"); } } } ******************************* Scenario 1 : No input is given Result: There was an exception Ran the finally code Scenario 2 : Pass '1' as argument on command line Result: Ran the finally code ******************************* Now consider this code: public class FinallyTest { public static void main(String[] args) { try { int x = Integer.parseInt(args[0]); } catch(Exception e) { System.out.println("There was an exception"); System.exit(0); } finally { System.out.println("Ran the finally code"); } } } ******************************* Scenario 1 : No input is given Result: There was an exception Scenario 2 : Pass '1' as argument on command line Result: Ran the finally code ******************************* Now consider this code: public class FinallyTest { public static void main(String[] args) { try { int x = Integer.parseInt(args[0]); System.exit(0); } catch(Exception e) { System.out.println("There was an exception"); } finally { System.out.println("Ran the finally code"); } } } ******************************* Scenario 1 : No input is given Result: There was an exception Ran the finally code Scenario 2 : Pass '1' as argument on command line Result: No output *******************************

Wednesday, May 10, 2006

HashCode() and Equals()

/** This code tries to explain the why it is necessary to carefully examine changes to equals() and hashCode() methods of an object. The equals method of an object should be: - Reflexive. - Symmetric. - Transitive. - consistent with the hashcode. */ import java.util.*; public class HashEqualsDemonstration { public static void main(String[] args) { HashMap h = new HashMap(); Arbitrary A = new Arbitrary(1); Arbitrary B = new Arbitrary(1); h.put(A,new String("Sample value for key value = 1")); System.out.println(h.get(B)); //since our key to hashmap is a mutable type we can change the value //but since our hashmap is not based on the state //we will get erroneous result. A.setValue(2); //this should be null since A has changed System.out.println(h.get(A)); //hence hashmap should be based on the state of the object //in case of a mutable type. } } class Arbitrary { int value; public Arbitrary() { value = -1; } public Arbitrary(int val) { value = val; } public int getValue() { return value; } public void setValue(int i) { value = i; } public boolean equals(Object o) { if(this == o) return true; if(!(o instanceof Arbitrary)) return false; Arbitrary temp = (Arbitrary) o; return (value == temp.getValue()); } public int hashCode() { return super.hashCode(); } };

Tuesday, May 09, 2006

Rate your life quiz.

This Is My Life, Rated
Life: 6.8
Mind: 7.3
Body: 5.6
Spirit: 8.8
Friends/Family: 3.2
Love: 0
Finance: 8.7
Take the Rate My Life Quiz
I like taking such kind of quizzes just for the sake of it. You could say, I might be enjoying them seriously! Ha! This one tries to rate your life and its components - Mind, Body, Spirit, Friends/Family, Love and Finance, on a scale of 10. I guess you might be wondering why my Love score is a big O - .. hehe.. keep guessing. Try and take the test yourself.