public class Main implements Runnable {
public Integer a = 1;
public Integer b = 2;
public static void main(String[] args) throws ExecutionException, InterruptedException {
Main m = new Main();
ExecutorService ser = Executors.newFixedThreadPool(3);
ser.submit(m);
// ser.submit(m);
ser.shutdown();
}
@Override
public void run() {
while (true) {
if (a <= 0) {
break;
}
System.out.println(a);
synchronized (a) {
try {
a.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
a--;
}
b++;
System.out.println(b);
if (b >= 10) {
synchronized (a) {
a.notify();
}
}
}
}
}
对象调用wait方法之后就会处于阻塞状态。将不会再向下执行,即输出1之后就不会执行了