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.
SCJP2 2001 84%<br />SCJD2 2003 100%
Joined: Feb 12, 2001
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.
Joined: Feb 22, 2001
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.
Joined: Dec 20, 2000
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.