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

Posts:37
Registered: 7/31/04
Notify () before wait()  
Aug 6, 2004 1:10 PM



 
i have two synchronized method in seperate Thread the first Method do Notify () the second Method MAke wait () ;

but when i run the program the first thread run fatser so its notify ( ) first and the secod thraed take time to do its job and then its Do Wait () which waiting the previois notify from last thread ( which was going )this let my second thraed go in waitingggggggggggggggg..... hoow i can solve this plz
 

Posts:37
Registered: 7/31/04
Re: Notify () before wait()  
Aug 6, 2004 1:14 PM (reply 1 of 5)



 
This is my two Function which is in sepearte thraed

[code]
synchronized (pyWait)///first method in X thread
{
pxWait.notify();
}

synchronized (pyWait) /// Second Method in Y Thread
{

try {pyWait.wait(); } catch (InterruptedException e) { }

}
[\code]
 

Posts:1,577
Registered: 00-02-25
Re: Notify () before wait()  
Aug 6, 2004 1:38 PM (reply 2 of 5)



 
notify() and wait() are typically used where there's a shared object, such as an event queue or semaphore. The reader reads until the queue is empty, and then wait()s. The sender adds to the queue and notify()s.
 

Posts:161
Registered: 8/7/97
Re: Notify () before wait()  
Aug 6, 2004 1:51 PM (reply 3 of 5)



 
try this

boolean alreadyDone = falsesynchronized (pyWait){   alreadyDone = true;   pxWait.notify();} synchronized (pyWait){    if(alreadyDone==false) {      try {pyWait.wait(); } catch (InterruptedException e) {      }   }}
 

Posts:18,384
Registered: 21.03.00
Re: Notify () before wait()  
Aug 6, 2004 3:12 PM (reply 4 of 5)



 
Hi,

You should also make the alreadyDone flag volatile.

/Kaj
 

Posts:196
Registered: 6/24/97
Re: Notify () before wait()  
Aug 6, 2004 3:39 PM (reply 5 of 5)



 
what does volatile have to do with this ?

If all you are trying to do is make thread B execute after Thread A has completed its work, try using Thread.join(..) (See the javadoc for details) instead of wait/notify.
 
This topic has 5 replies on 1 page.