【发布时间】:2016-05-17 20:37:13
【问题描述】:
我想知道如何解决以下问题: 我创建了多个相同类型的线程。它们都有一个带有定时while循环和同步块的运行方法,其中首先调用wait(),然后调用notify()。这导致所有线程都处于等待()状态,并且没有一个线程调用通知()。 我怎样才能克服这种僵局情况?有没有使用 wait()/notify() 的解决方案?
public class Deadlock3 implements Runnable {
LinkedList<Integer> intList;
public Deadlock3(LinkedList<Integer> list) {
intList = list;
new Thread(this).start();
}
public void run() {
long startTime = System.currentTimeMillis();
try {
while (System.currentTimeMillis() - startTime < 10) {
synchronized (intList) {
Integer number = intList.removeFirst();
System.out.println(number + " removed");
number = (number + 3) % 21;
intList.addLast(number);
System.out.println(Thread.currentThread().getName()+" - "+number + " added");
intList.wait();
intList.notifyAll();
}
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<Integer>();
for (int i = 0; i < 20; i++) {
list.add(i);
}
for (Integer i : list) {
System.out.println(i);
}
for (int i = 0; i < 4; i++) {
new Deadlock3(list);
}
}
}
感谢您的回答...
【问题讨论】:
-
我不明白。你为什么要写这段代码,因为知道
wait将永远阻塞,因为notify它不存在任何东西?你想做什么? -
你误解了如何使用等待和通知,把这段代码扔掉,阅读docs.oracle.com/javase/tutorial/essential/concurrency/…
-
任务是找出死锁情况的原因和解决方案。我确实理解为什么线程卡在 wait()-position ,但我不知道如何解决它。我可以在“外部”调用 notify()/notifyAll() 以保持线程运行吗?
-
@drwood187 是的,如果一个线程调用
wait,另一个线程需要调用notify[All]。 -
原因是,在其他线程调用
intList.notify()之前,对intList.wait()的调用不会返回。您的所有线程都执行 wait() 调用,然后它们中的任何一个都可以执行 notify() 调用。如果他们都在等待,那么谁来叫醒他们?
标签: java multithreading wait synchronized notify