Home arrow static arrow Java Programming [Archive] - wait and notify(All)
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - wait and notify(All)
This topic has 3 replies on 1 page.

Posts:15
Registered: 5/31/04
wait and notify(All)  
Jul 10, 2004 7:02 AM



 
Yep, one of these questions again. I HAVE READ MANY (almost all) other posts on the topic. And read the tutorial on wait() and notify(). But I still have a very weak understanding of 'em and I am unable to use 'em as I wish.

All I want to do is wake up a thread that is in the wait() state?! For something that seams so easy, I can not get it to work. In the following example I want to ceate a new t object with a wait period of 12 seconds, but dismiss it early (that is, less than the 12 seconds). However, with the code running, I have to wait the full 12 seconds, thus notify has no worked. (I understand that this would be MY fault (act or omission) and not Java's fault):

import java.io.*; class X{   private T t = nullpublic static void main(String args[])   {      new X();       System.out.println("Sample has Finished.");       return;   }    public X()   {      t = new T(12000);       for (int i = 0; i < 10; ++i)      System.out.println("Some Message.");       t.Stop();   }} class T implements Runnable{   private long _Out;    public T(long Out)   {      this._Out = Out;       new Thread(this).start();   }    public void Stop()   {      synchronized(this)      {         System.out.println("Before.");         notifyAll();         System.out.println("After.");      }       return;   } /* Thread Required. Invoked via calling start method. */   public void run()   {      synchronized(this)      {         try         {            wait(this._Out);   /* Wake me up. */         }         catch (InterruptedException x)         {          }      }       System.out.println("Thread has Finished.");       return;   }}


I also attempted the following but it still did not work as desired. What gives?

import java.io.*; class X{   private T t = nullpublic static void main(String args[])   {       X spot = new X();       System.out.println("Sample has Finished.");       return;   }    public X()   {      t = new T(this, 12000);       for (int i = 0; i < 10; ++i)      System.out.println("Wait.");       synchronized(this)      {         notifyAll();         // t.Stop();      }   }} class T implements Runnable{   private long _Out;   private X x;    public T(X x, long Out)   {      this._Out = Out;      this.x = x;       new Thread(this).start();   }    public void Stop()   {  //    synchronized(this)  //    {      System.out.println("Before.");         notifyAll();      System.out.println("After."); //     }       return;   } /* Thread Required. Invoked via calling start method. */   public void run()   {      synchronized(x)      {         try         {            wait(this._Out);   /* Wake me up. */         }         catch (InterruptedException x)         {          }      }       System.out.println("Thread has Finished.");       return;   }}


Can some kind Java Guru, please point me to my error / missunderstanding, or even better tweek my example to get it to work. I've been tring all night.

Thanks heaps!!!
 

Posts:3,258
Registered: 00-08-28
Re: wait and notify(All)  
Jul 10, 2004 7:58 AM (reply 1 of 3)



 
this might not be a direct answer to your question but this article might prove to be helpful

http://www-106.ibm.com/developerworks/java/library/j-spnotif/index.html?dwzone=java
 

Posts:373
Registered: 5/8/01
Re: wait and notify(All)  
Jul 10, 2004 8:11 AM (reply 2 of 3)



 
Hi,

the problem is that the notifyAll() is called before wait(). The start of the new thread takes some time, so the t.Stop() is called way before the new thread is running. Just put a System.out before the wait() and you'll see it.

Andr´┐Ż
 

Posts:15
Registered: 5/31/04
Re: wait and notify(All)  
Jul 10, 2004 6:12 PM (reply 3 of 3)



 
As I said, My Fault. And you were right - I was calling notifyAll before wait. So it did work after all - cool - thanks twupack!!!
 
This topic has 3 replies on 1 page.