问:为什么是 while 而不是 if ?
大多数人都知道常见的使用 synchronized 代码:
synchronized (obj) {
while (check pass) {
wait();
}
// do your business
}
那么问题是为啥这里是 while 而不是 if 呢?这个问题我最开始也想了很久,按理来说已经在 synchronized 块里面了嘛,就不需要了。这个也是我前面一直是这么认为的,直到最近看了一个 Stackoverflow 上的问题才对这个问题有了比较深入的理解。
试想我们要试想一个有界的队列。那么常见的代码可以是这样:
static class Buf {
private final int MAX = 5;
private final ArrayList<Integer> list = new ArrayList<>();
synchronized void put(int v) throws InterruptedException {
if (list.size() == MAX) {
wait();
}
list.add(v);
notifyAll();
}
synchronized int get() throws InterruptedException {
// line 0
if (list.size() == 0) { // line 1
wait(); // line2
// line 3
}
int v = list.remove(0); // line 4
notifyAll(); // line 5
return v;
}
synchronized int size() {
return list.size();
}
}
注意到这里用的 if,那么我们来看看它会报什么错呢?
下面的代码用了 1 个线程来 put,10 个线程来 get:
final Buf buf = new Buf();
ExecutorService es = Executors.newFixedThreadPool(11);
for (int i = 0; i < 1; i++)
es.