Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conceptual Question About Java Concurrency

Tags:

java

If I I have a class whose run() method sleeps for 1000 ms and then print "Thread". And I start this thread from my main program, then have my main program immediately sleep for 2000 ms, and then print "Main Thread".

Is it guaranteed that Thread will be printed before Main Thread?

like image 617
Saobi Avatar asked Nov 07 '09 00:11

Saobi


Video Answer


1 Answers

Nope. Conceptually, it's possible that the system you're running on will be so busy that the new thread doesn't even get a chance to run anything before the main thread has had a chance to sleep and print "Main Thread". In reality that's very unlikely, of course, but fundamentally sleep is not a coordination primitive.

like image 68
Jon Skeet Avatar answered Sep 22 '22 11:09

Jon Skeet