About notify() and wait() methods?
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   zhang_binbin
Posted On:   Thursday, October 25, 2001 11:38 PM

I create a thread pool . When a thread is idle,it call wait() and is put to the thread pool. When there's work to do , I'm told to get a thread from the thread pool and call the notify() as following: w = (Request)pool.pop(); w.wake (data); But the notify() method "Wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of the implementation. " Does that mean you couldn't wake a specified thread? Thanks    More>>

I create a thread pool . When a thread is idle,it call wait() and is put to the thread pool. When there's work to do , I'm told to get a thread from the thread pool and call the notify() as following:

w = (Request)pool.pop();

w.wake (data);


But the notify() method
"Wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of the implementation. "



Does that mean you couldn't wake a specified thread?



Thanks

   <<Less

Re: About notify() and wait() methods?

Posted By:   Alex_Chaffee  
Posted On:   Friday, October 26, 2001 12:41 PM

Each thread would be waiting on the object attached to its own Thread object. There is no contention between different threads as long as they are using

synchronized (this) {
this.wait();
}

when created, and

synchronized (this) {
this.notify();
}
to wake up (assuming the notify is called on the same Thread object, but from inside a different (manager) thread).


It's confusing that there's two totally separate things with the same name -- thread and java.lang.Thread -- especially since the thread starts its life inside the Thread's run() method and so forth.

append the wake() method

Posted By:   zhang_binbin  
Posted On:   Thursday, October 25, 2001 11:59 PM

synchronized void wake (Object data)

{
_data = data;

notify();

}
About | Sitemap | Contact