• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

wait() and notifyAll()

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}

}
}
}
 
reply
    Bookmark Topic Watch Topic
  • New Topic