我正在开发一个使用原始 Java 并发结构的项目,例如 wait()、notify()、notifyAll()、Thread.run()、synhronized 等。在我的项目中,有多个线程(Thread 类的扩展) 谁会定期从队列中获取对象。因此,我使用了一个具有内部 TimerTask 类的 Timer 类。
我的问题是,我无法让其他线程定期唤醒。我的主要课程不是这些线程或计时器类。因此,我称他们从另一个班级运行。我不知道如何让这些线程每 100 毫秒等待并通知一次。我的计时器课程是:
public class Controller extends Timer{
int counter;
TimerTask task;
final Controller c = this;
public class PeriodicTime extends TimerTask {
@Override
public void run() {
if(counter > 0) {
//do some stuff to wake up threads
}
counter++;
}
}
Controller () {
super ();
this.task = new PeriodicTime();
counter = 0;
this.schedule(task, 300, 100);
}
}
我的线程类是:
public class Element extends Thread {
public void run() {
// do something to get an object from another class (a queue)
}
}
现在,我真的很困惑如何定期释放线程类。我什至无法确定是否使用 wait()/notify()。
正如我之前所说,我将创建多个 Element 类。他们将同步工作。那么,我该怎么办?