废话不多说,直接粘测试代码public class Test{
public static void main(String[] args) {
try {
ThreadB tb = new ThreadB();
tb.start();
System.out.println("0");
synchronized (tb) {
System.out.println("3");
tb.wait();
System.out.println("4");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}public class ThreadB extends Thread {
@Override
public void run() {
synchronized (this) {
System.out.println("1");
notify();
System.out.println("2");
}
}
}
为什么输出结果
有时候是:
0
1
2
3
有时候是:
0
1
2
3
4