This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
The reason why you are getting random vaues is because each thread you created is operating on a different object. If you want to get the values in order, you should implement Runnable and create threads using this same instance of class implementing this runnable as follows:
[ July 20, 2006: Message edited by: Aum Tao ] [ July 20, 2006: Message edited by: Aum Tao ]
SCJP 1.4 85%
Joined: Jun 21, 2006
Thanks very much Aum Tao
I got but still small is there that in extends Thread it is calling the same run method of the class and how the synchronized allow to mutiple threads to execute.
Though it is the "same" run method, the "this" reference on which 'run' method runs is different in the three cases. Note that your run method is synchronized on "this" reference (in other words, it requires a lock on 'this' reference before it can run.) Recall that synchronized instance methods will block each other only if they are running on the same instance. In the current case, since the instances are different, different locks are required, and hence they would not block each other.
[As an aside, thats not the case with the static synchronized methods.]