Is it possible that main thread runs the line 1 and line2 and line3 first and then line 4.
b thread may start after the main thread
[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..
UPDATED the code
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
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!
Humans and their filthy friendship brings nothing but trouble. My only solace is this tiny ad:
Rocket Oven Kickstarter - from the trailbosshttps://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss