写笔试的时候遇到了一个多线程的题目,是用线程作为 synchronized 的锁
题目: 问输出语句的顺序
Thread threadA = new Thread(()->{
System.out.println("threadA is running");
try{
Thread.sleep(2020);
}catch (InterruptedException e){
e.printStackTrace();
}
System.out.println("threadA exit");
});
threadA.start();
while(threadA.isAlive()){
synchronized (threadA){
threadA.wait();
}
}
System.out.println("main thread exit");
答案:
threadA is running
threadA exit
main thread exit
易错点1: wait() 是抢到锁,执行语句的线程等待并释放锁,跟哪个对象调用无关.此处为 main线程
易错点2:没有显式notify(), main线程 应该会持续等待,不会输出"main thread exit"
分析:本题中使用 threadA 作为监视器(锁)
1.当 main线程 执行threadA.wait()时 释放锁 进入以threadA线程对象wait()的线程等待队列中 threadA执行run()方法,输出语句1,2
2.threadA执行完毕 进入终止 (TERMINATED) 状态 以线程对象threadA运行的wait()线程(这里也就是main线程)退出等待队列,相当于唤醒,notify()/notifyAll()
3.main线程继续执行剩下逻辑,输出语句3
应用:我们可以使用 while()循环和wait() 来保证 线程的执行顺序
while(threadA.isAlive()){
synchronized (threadA){
threadA.wait();
}
}
- threadA.wait() 保证执行代码的线程 执行顺序 在threadA之后
- threadA.isAlive() 确保此时 threadA 还没有执行完 --> 去掉此条件可能出现 threadA 已经执行完毕之后 其他线程再执行wait() 语句,进入盲等状态