Here, run method is synchronized , so second thread (m1) had to wait till first thread completes execution... but why main thread is waiting more than 800 milliseconds ( at m.join(800)) and why main thread is waiting more than 1000 milliseconds? ( at m.join(1000) ).
I expected the output of first S.o.p between Line1-Line5 and output of second s.o.p between Line7- Line11. Becuase API says,
public final void join(long millis)
Waits at most millis milliseconds for this thread to die. , but here main thread is waiting more than specified no. of milliseconds.
at line 3, thread m is started and will become ready to run. soon after some time, its run method starts running as expected. so main will proceed at line 4 and prints message. at line 5, join is encountered and at the point, main is blocked for 800 ms. as per your expectation, it comes out exactly after 800 ms. and main continues at line 6. At that point m1 becomes "ready-to-run". now as run method is synchronized, it has to wait for m to complete and hence for free lock. so thread m's run method is completely executed before m1's run() method is entered. rest of the output is obvious.
Just keep in mind that, join() call blocks main method. join(long time) call blocks main method for 'time' ms. when method is synchrnized, only one thread can enter and execute it completely before another thread is entered.
posted 13 years ago
Hi Dilip, Thanx 4 ur response. But you hv not cleared my doubt. Please refer the output in both the cases, and my doubt is,
why main thread is waiting more than 800 milliseconds ( at m.join(800)) and why main thread is waiting more than 1000 milliseconds? (output with synchronized run) ( at m.join(1000) ).???
Naresh, perhaps this is because of the thread scheduler? You're creating three threads of execution (main, 0 and 1) and it's quite possible that main is waiting for only 800 ms but the thread scheduler is not letting main run. Instead, thread 0 is allowed to run to completion. One very important point about thread scheduling: very little is guaranteed. So even though main doesn't want to wait to more than 800 ms, it's up to the scheduler if it'll let main run or thread 0 to run. Try changing the priorities of the threads and see what happens. Also run your code 5 times and see how the output behaves all 5 times. HTH, Sashi
hi naresh, actually when u start the thread in which the run method is synchronized first the thread acquires the lock on that object and executes the run method. during this process parallely the main thread get executed but due to the stmt obj.join()here the join method is synchronized, that is the main thread has to acquire the lock on the object on which it is invoking the join method but currently the lock is held by the first thread which u started earlier in the main thread so only after the thread completes the run method it relinquishes the lock then the main thread acquires the lock and executes the join method.
for all this the time taken to execute the join method is 500*no of iterations(due to first thread)hope u r clear with the concept