wait/notify/notifyAll在Object类中
wait/notify/notifyAll在Object类中
因为我们在使用synchronized锁 对象锁可以是任意对象,所以wait/notify/notifyAll需要放在Object类中。
wait/notify/简单的用法
public class Thread03 extends Thread {
@Override
public void run() {
try {
synchronized (this) {
System.out.println(Thread.currentThread().getName() + “>>当前线程阻塞,同时释放锁!<<”);
this.wait();
}
System.out.println(">>run()<<");
} catch (InterruptedException e) {
}
}public static void main(String[] args) {Thread03 thread = new Thread03();thread.start();try {Thread.sleep(3000);} catch (Exception e) {}synchronized (thread) {// 唤醒正在阻塞的线程thread.notify();}}
}
多线程通讯实现生产者与消费者
public class Thread04 {
class Res {
/**
* 姓名
/
private String userName;
/*
* 性别
/
private char sex;
/*
* 标记
*/
private boolean flag = false;
}
class InputThread extends Thread {private Res res;public InputThread(Res res) {this.res = res;}@Overridepublic void run() {int count = 0;while (true) {synchronized (res) {//flag = false 写入输入 flag = true 则不能写入数据 只能读取数据try {// 如果flag = true 则不能写入数据 只能读取数据 同时释放锁!if (res.flag) {res.wait();}} catch (Exception e) {}if (count == 0) {this.res.userName = "余胜军";this.res.sex = '男';} else {this.res.userName = "小薇";this.res.sex = '女';}res.flag = true;res.notify();}count = (count + 1) % 2;}}
}class OutThread extends Thread {private Res res;public OutThread(Res res) {this.res = res;}@Overridepublic void run() {while (true) {synchronized (res) {try {if (!res.flag) {res.wait();}} catch (Exception e) {}System.out.println(res.userName + "," + res.sex);res.flag = false;res.notify();}}}
}public static void main(String[] args) {new Thread04().print();
}public void print() {Res res = new Res();InputThread inputThread = new InputThread(res);OutThread outThread = new OutThread(res);inputThread.start();outThread.start();
}
}
/**
- flag 默认值==false
- flag false 输入线程 输入值 输出线程 先拿到锁 释放锁
- flag true 输出线程 输出值
*/
public boolean flag = false;
Join/Wait与sleep之间的区别
sleep(long)方法在睡眠时不释放对象锁
join(long)方法先执行另外的一个线程,在等待的过程中释放对象锁 底层是基于wait封装的,
Wait(long)方法在等待的过程中释放对象锁
三个线程 T1,T2,T3,怎么确保它们按顺序执行?
Thread t1 = new Thread(() -> System.out.println(Thread.currentThread().getName() + “,线程执行”), “t1”);
Thread t2 = new Thread(() -> System.out.println(Thread.currentThread().getName() + “,线程执行”), “t2”);
Thread t3 = new Thread(() -> System.out.println(Thread.currentThread().getName() + “,线程执行”), “t3”);
t1.start();
t2.start();
t3.start();
public class Thread05 {
public static void main(String[] args) throws InterruptedException {Thread t1 = new Thread(() -> {try {Thread.sleep(3000);} catch (Exception e) {}System.out.println(Thread.currentThread().getName() + ",线程执行");}, "t1");Thread t2 = new Thread(() -> {try {t1.join();} catch (InterruptedException e) {}System.out.println(Thread.currentThread().getName() + ",线程执行");}, "t2");Thread t3 = new Thread(() -> {try {t2.join();} catch (InterruptedException e) {}System.out.println(Thread.currentThread().getName() + ",线程执行");}, "t3");t1.start();t2.start();t3.start();
}
}
wait/notify/notifyAll在Object类中相关推荐
- 为什么 wait/notify/notifyAll 在 Object 类定义而不是 Thread 类?
作者:Yujiaao 来源:segmentfault.com/a/1190000019962661 一个较难回答的 Java 问题, Java 编程语言又不是你设计的,你如何回答这个问题呢?需要对 J ...
- Object类中的两个方法——wait和notify使用总结
Object类中的wait .和 notify方法是用来处理线程的方法,既然定义在Object类中,两个方法的重要程度可见一斑: 两个方法的使用上都很复杂:通过阅读API获得更多对方法的理解: The ...
- Thread和Object类中关于线程的相关方法
Thread和Object类中线程的7个重要方法概览 类 方法名称 简介 Thread sleep相关 相关表示sleep所有的重载方法,参数不同,实际作用大同小异 Thread join() 等待其 ...
- Object类中常见的方法
一.Object类中的toString()方法 1. object 默认方法 toString方法,toString() 输出一个对象的地址字符串(哈希code码)! 2.可以通过重写toString ...
- Object类中的方法
基本描述: (1)Object类位于java.lang包中,java.lang包包含着Java最基础和核心的类,在编译时会自动导入: (2)Object类是所有Java类的祖先.每个类都使用 Obje ...
- java Object类中的各个方法及其作用
原文链接 一.简介Object类 Object类是所有类的父类,即每个类都直接或简介继承自该类.所以一个Object类型的变量可以引用任何对象,不论是类实例还是数组. 在不明确给出父类的情况下,Jav ...
- Java常见面试题—Object 类中的方法
Object 类中的方法 Object() 默认构造方法 clone() 创建并返回此对象的一个副本. equals(Object obj) 指示某个其他对象是否与此对象"相等". ...
- python中的object是什么意思_Python object类中的特殊方法代码讲解
python版本:3.8class object: """ The most base type """ # del obj.xxx或del ...
- object类中的equals与自定义equals方法详解
object类中的equals与自定义equal方法详解 1.this怎么理解?this == obj表示什么? this就是当前你new出来的对象,这里指谁调用equal方法this指的就是谁,ob ...
最新文章
- qcustomplot删除一条曲线_微凉秋日的成熟风穿搭,选一条V领连衣裙搭配,优雅知性显身材...
- Win7下程序结束为何没有调用 DLL_PROCESS_DETACH?
- django后台多页面分页逻辑python代码
- BZOJ3729: Gty的游戏(伪ETT)
- zjoi2018day1 游记
- BZOJ3083: 遥远的国度
- SAP FI 系列 (022) - 货币和汇率的配置
- 中国农业银行总行西安成都软件开发中心笔试面试流程
- 电路设计实例:232 DB9接口 引脚定义
- STL vector中的max_size方法
- html使table整体居中,如何让整个table表格居中?
- 函数的基本概念以及函数封装
- Top计算机博士去BATH还是985非升即走?
- STM32开发(2)----CubeMX的安装和使用
- shell技巧4 - nm命令解决AppStore2.5.2被拒问题
- 计算机函数名功能,修改计算机名的 API 函数 SetComputerName
- 你也能成为 “最强大脑”
- udf,udaf,udtf之间的区别
- px和毫米的换算_js转换px与mm, cm
- 外观模式 Facade
热门文章
- 安装程序无法创建新的系统分区也无法定位现有系统分区_如何拥有一个 Windows 10 和 Debian 10 的双系统...
- tensorflow就该这么学--4(识别手写数字)
- pytorch 学习笔记目录
- 文巾解题 5. 最长回文子串
- Python应用实战案例-pyspark库从安装到实战保姆级讲解
- 《数据中台实战》:如何通过标签平台圈出产品高价值用户?
- 深究 ElasticSearch 查询的秘密
- Python入门100题 | 第052题
- 数据分析系列:完善统计图(matplotlib)
- 政策定价风控审批策略