This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
Dear friends, When two threads are created through start method do they both start running simultaneously. pls explain to me the output of this program.
Thnx
SCJP2 2001 84%<br />SCJD2 2003 100%
khudiram Barik
Greenhorn
Joined: Feb 12, 2001
Posts: 17
posted
0
I run your example as it is and the out put are as follow:--
C:\khudiram>java thread2 raj i k dee i k Please check the compile class, me doubt if you are using the object without synchronized keyword specified. Thanks.
Angela Lamb
Ranch Hand
Joined: Feb 22, 2001
Posts: 156
posted
0
It is impossible to predict when a thread will be run, since each JVM implements thread scheduling in a different way. This is why the synchronized keyword is necessary. If two threads are both reading/writing the same data, you can't be sure that one thread will finish its work before the other thread starts running.
Andy Ceponis
Ranch Hand
Joined: Dec 20, 2000
Posts: 782
posted
0
One more thing. You have synchronized the doit() and check() methods but not the run(). Having the check and doit methods synch'd doesnt do you alot of good. It doesnt really help to have only one thread print out its info at a time if both are running at the same time. If you synchronized the run method, you would get more predictable results. Especially in this case. But synch'ing the rn method is usually not the best way to go.