File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes urgent:PLS HELP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "urgent:PLS HELP" Watch "urgent:PLS HELP" New topic

urgent:PLS HELP

Raj Mehra
Ranch Hand

Joined: Nov 20, 2000
Posts: 51
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%
khudiram Barik

Joined: Feb 12, 2001
Posts: 17
I run your example as it is and the out put are as follow:--

C:\khudiram>java thread2
Please check the compile class, me doubt if you are using the object without synchronized keyword specified.
Angela Lamb
Ranch Hand

Joined: Feb 22, 2001
Posts: 156
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
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.
I agree. Here's the link:
subject: urgent:PLS HELP
jQuery in Action, 3rd edition