• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

PRoblem On threads

 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Is the output possible:Thread-0 runs before main
THread-0
A
main
B

i had ran this code 10 times,still the same output
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure what you mean by "Is the output possible: Thread-0 runs before main" - you're observing it, so obviously it's possible.

Since the "B" thread is put to sleep, the JVM will choose some other thread to run, and the "A" thread is the only other user thread in existence - so this behavior is to be expected.
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thread-0 cannot print anything before main method because of the while loop in the GotIT method which will only end when the main method sets x to 1...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mohitkumar gupta wrote:

Is the output possible:Thread-0 runs before main
THread-0
A
main
B

You can't get this output! There is a endless loop on the GotIT() method!
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a endless loop on the GotIT() method!

That is not an endless loop. That loop only terminates when x is not 0...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:
That is not an endless loop. That loop only terminates when x is not 0...

Yea, endless loo, if x won't be reset with the value 1 by the main thread! Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic