当我偶然发现一些我不理解的行为时,我试图使用 Java 同步“原语”(synchronized、wait()、notify() )来实现类似于 Java 的有界BlockingQueue接口的东西。
我创建了一个能够存储 1 个元素的队列,创建了两个等待从队列中获取值的线程,启动它们,然后尝试将两个值放入主线程的同步块中的队列中。大多数情况下它可以工作,但有时等待一个值的两个线程似乎开始互相唤醒并且不让主线程进入同步块。
这是我的(简化的)代码:
import java.util.LinkedList;
import java.util.Queue;
public class LivelockDemo {
private static final int MANY_RUNS = 10000;
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < MANY_RUNS; i++) { // to increase the probability
final MyBoundedBlockingQueue ctr = new MyBoundedBlockingQueue(1);
Thread t1 = createObserver(ctr, i + ":1");
Thread t2 = createObserver(ctr, i + ":2");
t1.start();
t2.start();
System.out.println(i + ":0 ready to enter synchronized block");
synchronized (ctr) {
System.out.println(i + ":0 entered synchronized block");
ctr.addWhenHasSpace("hello");
ctr.addWhenHasSpace("world");
}
t1.join();
t2.join();
System.out.println();
}
}
public static class MyBoundedBlockingQueue {
private Queue<Object> lst = new LinkedList<Object>();;
private int limit;
private MyBoundedBlockingQueue(int limit) {
this.limit = limit;
}
public synchronized void addWhenHasSpace(Object obj) throws InterruptedException {
boolean printed = false;
while (lst.size() >= limit) {
printed = __heartbeat(':', printed);
notify();
wait();
}
lst.offer(obj);
notify();
}
// waits until something has been set and then returns it
public synchronized Object getWhenNotEmpty() throws InterruptedException {
boolean printed = false;
while (lst.isEmpty()) {
printed = __heartbeat('.', printed); // show progress
notify();
wait();
}
Object result = lst.poll();
notify();
return result;
}
// just to show progress of waiting threads in a reasonable manner
private static boolean __heartbeat(char c, boolean printed) {
long now = System.currentTimeMillis();
if (now % 1000 == 0) {
System.out.print(c);
printed = true;
} else if (printed) {
System.out.println();
printed = false;
}
return printed;
}
}
private static Thread createObserver(final MyBoundedBlockingQueue ctr,
final String name) {
return new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println(name + ": saw " + ctr.getWhenNotEmpty());
} catch (InterruptedException e) {
e.printStackTrace(System.err);
}
}
}, name);
}
}
这是我在“阻塞”时看到的内容:
(skipped a lot)
85:0 ready to enter synchronized block
85:0 entered synchronized block
85:2: saw hello
85:1: saw world
86:0 ready to enter synchronized block
86:0 entered synchronized block
86:2: saw hello
86:1: saw world
87:0 ready to enter synchronized block
............................................
..........................................................................
..................................................................................
(goes "forever")
但是,如果我将 addWhenHasSpace 和 getWhenNotEmpty 方法的 while(...) 循环内的 notify() 调用更改为 notifyAll(),它“总是”通过。
我的问题是:为什么在这种情况下 notify() 和 notifyAll() 方法的行为会有所不同,以及 notify() 的行为为什么会这样?
在这种情况下,我希望这两种方法的行为方式相同(两个线程等待,一个阻塞),因为:
- 在我看来,在这种情况下 notifyAll() 只会唤醒另一个线程,与 notify() 相同;
- 看起来唤醒线程的方法的选择会影响被唤醒的线程(我猜想变成 RUNNABLE)和主线程(已经被 BLOCKED)后来竞争锁的方式——这不是我所期望的javadoc 以及在互联网上搜索该主题。
或者也许我完全做错了什么?