Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JTest and threads  RSS feed

 
paul wheaton
Trailboss
Posts: 22255
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A question on JTest asks
<pre>
Which methods may stop a thread from executing?
A. sleep()
B. stop()
C. yield()
D. wait()
E. notify()
F. notifyAll()
G. synchronized()

</pre>
The answer given by JTest is A, B, C and D.
What about E and F? I thought that notify() would stop the thread and give control to somthing that is waiting?
 
Michael Finney
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. I believe it just takes a thread in the Monitor's (the Object with the synchronized methods/statements) pool and puts it into the Ready state. It does not force the current thread to yield.
 
paul wheaton
Trailboss
Posts: 22255
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct that it does not release the CPU - this much I have figured out since posting that question.
But, does it move it to the "ready" state, or the "waiting for lock" state? (is there a "waiting for lock" state?)
 
Pawel Veselov
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Umm, those terms are confusing.
Monitor can be locked or unlocked. A thread can be waiting on monitor, having the monitor locked (resource is busy) or waiting for the monitor to unlock (waiting for a resource).
So, notify() just tells the monitor the wait() should be finished, but doesn't change the monitor's ownership(locking). So, if you have one thread waiting on monitor and another doing notify() on the same monitor, the execution of the first thread will continue only after the second thread will release the monitor (exit 'synchronized' block).

------------------
With best of best regards, Pawel S. Veselov ( aka Black Angel )

 
josephine chen
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to RH
Notify method selects one of the thread in the monitors waiting pool and moves it to Seeking lock state.
Eventually when it gets the lock it is executed
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!