• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help!

 
ricky gonzalez
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am confused about wait()'s role in thread. Is the following assumptions correct?
a) In ordinary object, wait() is called to release the lock on that particular object from the Thread that called a synchronized method of that object.
And notify() is called for the Thread that reliquished that object's lock to reclaim the lock.
b) In a Thread object, wait() does the above and blocks the execution of the Thread itself until some other Thread called thread.Notify().
Thanks!
 
Tanveer Rameez
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
about wait you are right. calling wait by the thread X, makes it give up the lock it was holding on the object, and go to wait state.
now if another thread Y executing a synchronized method/block of that object calls on notify(), then any thread waiting on that object (it may be thread X also), is awakened to running state. But thread X does not immediately obtains the lock on the object. It merely goes to the ready queue. It only gets the object lock if the thread Y gives up the lock, and the scheduler selects it to get hold of the CPU.
notifyAll() awakens all the thread waiting on that object.
friends,tell me if I am wrong
regards
Tanveer Rameez
 
Jerry Pulley
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ricky,
Since Tanveer answered part a) of your question, I'll only address part b).
Your statement in b) is not correct. <code>Thread.wait()</code> behaves no differently from any other object's <code>wait()</code> method. It will perform as you describe only in the odd circumstance when a <code>Thread</code> is synchronizing on itself. <code>wait()</code> is always called on the monitor object.
Many authors write confusing example code like:<pre>
public class Test extends Thread {
...
synchronized void run() { ... }
}</pre>
In this case, the <code>Test</code> class itself is a <code>Thread</code>, and <code>run()</code> synchronizes on an object of <code>Test</code>. If <code>wait</code> is called in <code>run</code> then the thread will block, but only because the object itself is the monitor, not because the object happens to be a <code>Thread</code>.
Let me make something plain: This is very poor code. The main class is almost never a thread from an analysis and design point of view, and code should reflect that point of view. A misguided attempt to reduce the number of classes often leads to such mixing of responsibilities. The confusion engendered by such code alone is enought to make one avoid it.
Jerry
 
Bob Moranski
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, so does notify() makes the thread relinquish the lock on that object?
Also what is the differentce bewteen notify() and notifyAll()? Do they both let ALL threads know that the lock to this object is available?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!