File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes wait() Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "wait()" Watch "wait()" New topic
Author

wait()

ricky gonzalez
Ranch Hand

Joined: Jun 30, 2000
Posts: 167
Hi, I was wondering what does wait() do in a Thread object. I suppose it blocks the particular thread it is in, and it also releases the lock of the Thread object itself?
Also, is there a difference between wait() in a Thread object and wait() in an ordinary object?
Thanks!
a ali
Greenhorn

Joined: Oct 03, 2000
Posts: 10
wait() is the method of the Object class not of Thread class. wait() should be called inside a synchronized method/block. When wait is called, the thread running the synchronized method releases the lock on the object.
ricky gonzalez
Ranch Hand

Joined: Jun 30, 2000
Posts: 167
Thanks! I thought wait() is kind of like suspend() except wait() releases the lock on some object and suspend does not.
class AThread extends Thread{
boolean run;
AThread(){
start();
}
public synchronized void run(){
while(run){
wait();
}
}
}
What lock exactly does the above wait() release?
a ali
Greenhorn

Joined: Oct 03, 2000
Posts: 10
In your example the wait() method releases lock on the AThread object.
Look at this example :
public class MyThread extends Thread {
boolean isRunnable = true;

public MyThread() {
start();
}
public synchronized void run() {
System.out.println("In run method");
while ( isRunnable ) {
try {
wait();
}
catch ( InterruptedException ie ) {
}
}
}

public static void main ( String[] args ) {
MyThread firstThread = new MyThread();
try {
sleep(1000);
}
catch ( InterruptedException ie ) {}
OtherClass oClass = new OtherClass();
oClass.printString("Hello", firstThread);
}
}
class OtherClass {
public void printString(String s, Object o ) {
synchronized(o) {
System.out.println(s);
}
}
}
Output of the above example is
In run method
Hello
The run method releases the lock on MyThread object when wait() is executed. When oClass object runs it's printString() method, it tries to obtain the lock on MyThread object and it gets it. Hence it prints "Hello". If you remove the wait() from MyThread in the above example, then only "In run method" is called since oclass's printString() does not get lock on MyThread.
Jerry Pulley
Ranch Hand

Joined: Sep 19, 2000
Posts: 221
Ricky;
<code>wait()</code>, as you know, is a method of <code>Object</code>. You call <code>wait()</code> on the object you're using as a monitor; that's the thing that you're synchronizing your threads on. The confusion comes about because <code>Thread</code> (like everything else) extends <code>Object</code> and so has a <code>wait()</code> (and <code>notify()</code>) method. You would only call these methods on a thread if you're using that <code>Thread</code> object as a monitor, not to represent a thread of execution. The role of "monitor" and the role of "thread controller" have nothing to do with one another. Calling <code>wait()</code> on a <code>Thread</code> object is a pretty silly thing to do.
jply

[This message has been edited by Jerry Pulley (edited October 20, 2000).]
ricky gonzalez
Ranch Hand

Joined: Jun 30, 2000
Posts: 167
a ali, Jerry
Thank you so much for your help! But I thought wait() can be used in a Thread object as a way to block the execution of the Thread, much like suspend(). Here is what I had learned from you guys, please correct me if I am wrong:
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!
Jerry Pulley
Ranch Hand

Joined: Sep 19, 2000
Posts: 221
Ricky;
I'm responding in this (the original) thread vice the new one (Oct. 21 5:03 pm) with the same question.
No, calling <code>Thread.wait()</code> would not cause the (<code>Thread</code>) object on which it is invoked to wait; it would cause the other one (the thread using that <code>Thread</code> object as a monitor, i.e. the thread in which the call occurred) to wait. <code>wait()</code> is nothing like <code>suspend()</code>.
jply
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: wait()
 
Similar Threads
Another one on Threads
Thread question
Khalid mock exam question (pls help urgent)
Is wait() also a thread object method as the thread is also an object?
wait()