aspose file tools*
The moose likes Threads and Synchronization and the fly likes wait() and notifyAll() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "wait() and notifyAll()" Watch "wait() and notifyAll()" New topic
Author

wait() and notifyAll()

John Green
Greenhorn

Joined: May 27, 2003
Posts: 1
I wanted to wake up t1 thread in t2 thread.
So I put notifyAll() in run() method of T2.
But it doesn't work.
No "t1 woke up" message. It just repeats "t2 run start"
What should I do?

[ May 27, 2003: Message edited by: John Green ]
Saket Barve
Ranch Hand

Joined: Dec 19, 2002
Posts: 229
The way to do it is to create T1 object in class T2, and start it from there.
Next, you'll have to call t1.interrput() from t2 in order to evoke the InterruptedException. Realize that notifyAll() will work only if the object from which you are calling it is the owner of that object.
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

The first thing you should know is that the documentation on the commands is VERY good. So look in the documentation to understand wait and notify.
notifyAll() is the same as this.notifyAll(); Which means it will notify anyone waiting on "this" object. you are calling this.wait() on the t1 object, but calling this.notifyAll() on the t2 object. you must make the calls on the same objects to get what you are looking for.
Geoffrey Ren
Greenhorn

Joined: Nov 25, 2002
Posts: 18
class T3
{
public static void main(String[] args)
{
T2 t2 = new T2();
t2.start();
System.out.println("hmm");
}
}
public class T1 extends Thread
{
public synchronized void run()
{
while(true)
{
System.out.println("t1 run start");
try
{
System.out.println("t1 wait");
wait();
}
catch(InterruptedException e)
{
System.out.println("e");
}
System.out.println("t1 woke up..............");
}
}

}
class T2 extends Thread
{
T1 t1;

public synchronized void run()
{
t1 = null;
while(true)
{
System.out.println("t2 run start");
try
{
t1 = new T1();
t1.start();

sleep(500);
}
catch(InterruptedException e)
{
}
System.out.println("invoke the t1 thread!!");
synchronized(t1)
{
t1.notifyAll();
}

}
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: wait() and notifyAll()
 
Similar Threads
wait & notify
notify() vs notifyall()
Do I need notify()/wait() etc. when I just mark all Data-methods synchronized?
Question about wait() and notify()
Synchronizing between two threads, one implements Runnable the other extends Thread