问:为什么是 while 而不是 if ?
synchronized (obj) {
while (check pass) {
wait();
}
// do your business
}
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() == ) { // line 1
wait(); // line2
// line 3
}
int v = list.remove(); // line 4
notifyAll(); // line 5
return v;
}
synchronized int size() {
return list.size();
}
}
下面的代码用了 1 个线程来 put,10 个线程来 get:
final Buf buf = new Buf();
ExecutorService es = Executors.newFixedThreadPool(11);
for (int i = ; 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 = ; 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: , Size:
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)
synchronized int get() throws InterruptedException {
while (list.size() == ) {
wait();
}
int v = list.remove();
notifyAll();
return v;
}
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());
}
}, , 1, TimeUnit.SECONDS);
for (int i = ; 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 = ; 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);
问:什么时候用 notifyAll 或者 notify?
synchronized void put(int v) throws InterruptedException {
if (list.size() == MAX) {
wait();
}
list.add(v);
notify();
}
synchronized int get() throws InterruptedException {
while (list.size() == ) {
wait();
}
int v = list.remove();
notify();
return v;
}
任何时候,被唤醒的来执行的线程是不可预知。比如有 5 个线程都在一个对象上,实际上我不知道 下一个哪个线程会被执行。
synchronized 语义实现了有且只有一个线程可以执行同步块里面的代码。
C – 消费者 调用 get。
如果 C1 把 C2 唤醒了,所以P2 (其他的都得等)只能在put方法上等着。(等待获取synchoronized (this) 这个monitor)。
C2 检查 while 循环发现此时队列是空的,所以就在 wait 里面等着。
C3 也比 P2 先执行,那么发现也是空的,只能等着了。
8. 这时候没有别的调用了,那么现在这三个线程(P3, C2,C3)就全部变成 suspend 了,也就是死锁了。