问题1 为什么是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.execute(new Runnable() {
@Override
public void run() {
while (true ) {
try {
buf.put(1);
Thread.sleep(20);
}
catch (InterruptedException e) {
e.printStackTrace();
break;
}
}
}
});
for (int i = 0; i < 10; i++) {
es.execute(new Runnable() {
@Override
public void run() {
while (true ) {
try {
buf.get();
Thread.sleep(10);
}
catch (InterruptedException e) {
e.printStackTrace();
break;
}
}
}
});
}
es.shutdown();
es.awaitTermination(1, TimeUnit.DAYS);
这段代码很快可能说一开始就会报错
Java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:653) at java.util.ArrayList.remove(ArrayList.java:492) at TestWhileWaitBuf.get(TestWhileWait.java:80)atTestWhileWait2.run(TestWhileWait.java:47) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745)
很明明,在remove’的时候报错了.
那么我们来阐明下:
假设此刻有A, B两个线程来执行get 操纵, 我们假设如下的步调产生了:
1. A 拿到了锁 line 0
2. A 发明size==0, (line 1), 然后进入期待,并释放锁 (line 2)
3. 此时B拿到了锁, line0, 发明size==0, (line 1), 然后进入期待,并释放锁 (line 2)
4. 这个时候有个线程C往内里加了个数据1, 那么 notifyAll 所有的期待的线程都被叫醒了.
5. AB 从头获取锁, 假设 又是A拿到了. 然后 他就走到line 3, 移除了一个数据, (line4) 没有问题.
6. A 移除数据后 想通知别人, 此时list的巨细有了变革, 于是挪用了notifyAll (line5), 这个时候就把B给叫醒了, 那么B接着往下走.
7. 这时候B就出问题了, 因为 其实 此时的竞态条件已经不满意了 (size==0). B觉得还可以删除就实验去删除, 功效就跑了异常了.
那么fix很简朴, 在get的时候加上while就好了:
synchronized int get() throws InterruptedException {
while (list.size() == 0) {
wait();
}
int v = list.remove(0);
notifyAll();
return v;
}
同样的, 我们可以实验修改put的线程数 和 get的线程数来 发明假如put内里不是while的话 也是不可的:
我们可以用一个外部周期性任务来打印当前list的巨细, 你会发明巨细并不是牢靠的最大5:
final Buf buf = new Buf();
ExecutorService es = Executors.newFixedThreadPool(11);
ScheduledExecutorService printer = Executors.newScheduledThreadPool(1);
printer.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
System.out.println(buf.size());
}
}, 0, 1, TimeUnit.SECONDS);
for (int i = 0; i < 10; i++)
es.execute(new Runnable() {
@Override
public void run() {
while (true ) {
try {
buf.put(1);
Thread.sleep(200);
}
catch (InterruptedException e) {
e.printStackTrace();
break;
}
}
}
});
for (int i = 0; i < 1; i++) {
es.execute(new Runnable() {
@Override
public void run() {
while (true ) {
try {
buf.get();
Thread.sleep(100);
}
catch (InterruptedException e) {
e.printStackTrace();
break;
}
}
}
});
}
es.shutdown();
es.awaitTermination(1, TimeUnit.DAYS);

这里 我想应该说清楚了为啥必需是while 照旧if了
问题2:什么时候用notifyAll可能notify
大大都人城市这么汇报你: