Condition 类的 awiat 方法和 Object 类的 wait 方法等效

Condition 类的 signal 方法和 Object 类的 notify 方法等效

Condition 类的 signalAll 方法和 Object 类的 notifyAll 方法等效

ReentrantLock 类可以唤醒指定条件的线程,而 object 的唤醒是随机的

synchronized方式对比下
 public static Student student = null;

public static void waitTest() throws InterruptedException {
student = new Student();
Thread t1 = new Thread(() -> {
synchronized (student) {
System.out.println("t1 wait!");
try {
student.wait();
System.out.println("t1 开始重新run!");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t1.start(); Thread.sleep(3000); Thread t2 = new Thread(() -> {
synchronized (student) {
System.out.println("t2 我通知了 notify!");
student.notify();
}
});
t2.start(); }
Condition实现方式
public static ReentrantLock lock = new ReentrantLock();
public static Condition condition = lock.newCondition(); private static void reentrantLock() throws InterruptedException { Thread t1 = new Thread(() -> {
lock.lock();
try {
System.out.println("我进入了T1!");
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
System.out.println("释放T1锁!");
} });
t1.start(); Thread.sleep(2000); Thread t2 = new Thread(() -> {
lock.lock();
try {
System.out.println("我进入了T2!");
condition.signal();
System.out.println("我是T2,我唤醒了T1,T1进入等待状态!");
//condition.signalAll();
} finally {
lock.unlock();
System.out.println("释放T2锁!");
}
}); t2.start(); }

最新文章

  1. dubbox
  2. iOS阶段学习第19天笔记(协议-Protocol)
  3. C++ 代码换行
  4. starling 笔记
  5. nodejs学习[持续更新]
  6. 学习web前端技术的笔记,仅供自己查阅备忘,移动对font-size的控制(并非原创)
  7. 11 PopupMenu菜单和代码例子
  8. C++ 命名管道 与Winform跨进程通信
  9. [Codeforces896C] Willem, Chtholly and Seniorious (ODT-珂朵莉树)
  10. js扩展运算符(spread)三个点(...)
  11. ReportViewe调用Reporting Services报表时报错Session超时
  12. 30.Mysql常见问题和应用技巧
  13. winform(记事本--保存和退出)
  14. 【C#】C# in deep 笔记
  15. Use Jupyter notebook on Fedora 28
  16. 山寨一个std::bind\boost::bind
  17. POJ 3122 Pie
  18. linux安装spark-2.3.0集群
  19. 网络编程(Socket)
  20. 浏览器报错:unexpected end of input 解决方法

热门文章

  1. WPF教程一:创建Hello world来理解XAML的内容及编译
  2. Linux | 通配符 & 转义符
  3. C语言:随机抽奖
  4. ESP8266 NodeMCU小白手把手入门(实操篇)以土壤湿度和DHT传感器为例讲解读取传感器的值
  5. 线性回归与梯度下降(ML作业)
  6. vue tab实现右定位
  7. Debian10 / Ubuntu 20.10 /Linux Mint 20安装Microsoft Edge浏览器Dev版(每周更新)
  8. SpringBoot时代背景
  9. js问题记录
  10. 适合普通大学生的 Java 后端开发学习路线