【发布时间】:2018-04-25 16:47:11
【问题描述】:
while (!closingTime){
depot.enterramp();
}
这是启动如下功能
synchronized (list) {
while (list.size() == 0) {
System.out.println(":: DEPOT\t:: " + "NO BUS FOUND IN THE APPROACHES DEPOT, WAITING THE BUS COMING");
try {
list.wait();
} catch (InterruptedException iex) {
iex.printStackTrace();
}
}
目前我的线程正在等待 notify() [此代码在 enterramp() 中]
public class Clock extends Thread {
public void run() {
try {
Thread.sleep(15000);
notifyTime();
} catch (Exception e) {
}
}
public synchronized void notifyTime() {
System.out.println(":: CLOCK\t:: ALERT ALERT !!! DEPOT HAVE TO CLOSE IN 30 MINUTES, NO MORE ACCEPTING THE BUSES");
closingTime = true;
return;
}
这是时钟休眠 15 秒并将关闭时间设为 true
synchronized (list) {
list.add(bus);
list.notify();
}
这是我通知它的部分
我面临的问题是当关闭== true,但是线程卡在wait()区域,当我的关闭时间== true时,如何让线程退出wait()???
【问题讨论】:
-
我相信中断线程是正确的方法(
Thread.interrupt())。 -
但是线程当前卡在wait()中,在通知线程之前我不能有任何动作...
-
你需要通知线程。你在哪里调用你发布的最后一个
synchronized-block? -
第一盒代码在第二盒代码中继续运行,所以当关闭时间不正确时,线程会一直保持wait()直到被notify调用。
标签: java multithreading return wait notify