package me.zhengjie;
public class Test {
public static void main(String[] args) {
System.out.println("hello world");
B b = new B();
b.start();
System.out.println("waiting 1");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
b.setDone();
System.out.println("waiting 2");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("waiting end");
}
}
class B extends Thread {
@Override
public void run() {
super.run();
System.out.println("B start run");
this.get();
System.out.println("B run");
}
boolean isDone = false;
public void get() {
synchronized (this) { // 旋锁
while (!isDone) { // 是否有结果了
try {
wait(); //没结果是释放锁,让当前线程处于等待状态
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void setDone() {
isDone = true;
synchronized (this) { //获取锁,因为前面wait()已经释放了callback的锁了
notifyAll(); // 唤醒处于等待的线程
}
}
}
wait主要是让当前对象运行的线程挂起,然后调用notify之后继续执行。这种在调用某个函数,又不能立刻拿到结果的情况下,让线程挂起很有用。在rpc远程过程调用的底层原理上,就用到了这个特性,用来等待远程服务拿到结果后再继续执行,非常方便。
运行结果如下:
如果把代码中的 b.setDone() 这一行注释掉,则会变成这样: