【发布时间】:2012-10-11 07:50:05
【问题描述】:
我想知道在 java 中是否有可能有一个实现 Runnable 的类,如果该类的一个对象进入 wait() (因此线程停止运行直到它收到信号),同一类的另一个对象可以通知它继续运行?
理想情况下我想做的是:
public class ThreadClass implements Runnable{
public void run(){
//.. Does some arbitrary work
if( aCondition){ //have it wait until something signals it to contine exection.
this.wait();
}
//Continues to do more work.
//finished whatever it needed to do. Signal all other threads of this object type to continue their work.
this.notifyAll();
}
}
这可能吗?如果可以,我将如何去做?我正在尝试使对象本身可以管理自己和所有其他相同类型的对象。这样使用这个类的人就不用担心管理它了。
【问题讨论】:
标签: java multithreading wait runnable notify