Java并发知识整理

笔试题

线程间协作方式: wait notify notifyAll

都是__Object类的final native方法__

  1. 调用wait()能阻塞当前线程,必须拥有当前对象的monitor
  2. notify()能唤醒一个正等待对象的monitor的线程
  3. notifyAll()能唤醒等待monitor的线程

由1可知,调用wait()方法必须在同步块、方法中调用。
调用wait()后则交出当前对象的monitor,进入等待状态,(注意与Thread.sleep的区别)
使用notify()唤醒任意一个等待该monitor的线程,也必须在同步块中使用。
唤醒后不会立即获得monitor,等待退出synchronized块,释放对象锁后才能得到锁执行。

Author: whllhw
Link: https://whllhw.ml/posts/2019/04/15/Java并发知识整理/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.