The code is incomplete. As for your question, its possible that main thread executes before any other thread that you start.
[Edit] Now that you've posted the complete code, yes its possible that main thread executes before b thread (even though the possibility is very less but its possible). If b thread starts before the main thread, there will be a deadlock..
As for your question, its possible that main thread executes before any other thread that you start...
THe answer is exam lab is that
there would be no output .
As when thread b starts ,it acquires lock on a.then,when it goes to it's doTask2 method,it joins and waits till thread main completes.
Thread main too needs lock on a to call line2.
The case when main thread runs before b thread is not taken into account
So, answer in Exam Lab is wrong
i know that Thread.sleep(1000)
Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds. The thread does not lose ownership of any monitors.
i ran the program and no output appears
but ,i still have one confusion that
are executed first than any other code including Thread.sleep(1000) in main thread
Ankit Garg wrote:There can be output from the program. Even if main method sleeps for 1 second, the other thread might not get an opportunity to execute. The chances are very very low but its possible...
Agreed! But, these kind of question won't be in the exam, for sure, you can predict the output without this kind little possibility!
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|