线程(7)--wait()方法、notify()方法、notifyAll()方法
来源:互联网 发布:中建七局网络协同平台 编辑:程序博客网 时间:2024/05/18 02:54
wait()方法:线程阻塞,如果传入了时间参数,阻塞时长到达执行的时间参数值,则自动恢复运行;如果没有传入时间参数,则直到调用该对象的notify或notifyAll方法才能使被该对象阻塞的线程恢复运行
notify()方法: 用于唤醒该对象阻塞的一个线程
notifyAll()方法: 用于唤醒该对象阻塞的所有线程
注意:
- 使用这三个方法前,必须同步调用这三个方法的对象
- 保证调用wait方法和notify或notifyAll方法的对象是同一个对象
下面用一个案例,来说明wait()方法、notify()方法和 notifyAll()方法的使用
案例说明:
案例一:八戒被妖精迷倒了,被悟空知道了,悟空要让那个妖精把八戒救醒!
案例二:八戒被妖精迷倒了,沙僧也被妖精迷倒了,被悟空知道了,悟空要让那个妖精把八戒和沙僧救醒!
案例一:(代码)分析:在本案例中,八戒需要被妖精迷倒(wait()方法),悟空需要通过妖精让八戒醒来(notify),使用同一个妖精,所以需要创建妖精线程、八戒线程、悟空线程(同步)
- 创建妖精线程
package com.wait.entity;/** * @version 2017-12-20 上午10:34:00 * 妖精类:默认继承object方法 * wait方法:阻塞线程(施法) * notify:释放第一个被wait阻塞的线程 * notifyAll:释放所有被wait阻塞的线程 */public class Ghost extends Thread{ @Override public void run() { System.out.println("释放迷药或是解开迷药!"); }}
- 创建八戒线程
package com.wait.entity;/** * @version 2017-12-20 下午3:49:20 */public class BaJie extends Thread{ private Ghost ghost; public BaJie(Ghost ghost){ this.ghost = ghost; } //重写run()方法 @Override public void run() { super.run(); System.out.println("我(八戒)被妖精迷倒了!"); //调用wait、notify、notifyAll方法之前,必须先同步对象 //wait()方法,如果带有时间参数的话,等时间结束,线程自动向下执行。如果没有时间参数,线程一直处于等待当中,直到执行notify方法或是notifyAll方法 synchronized (ghost) { try { ghost.wait(1000); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("我(八戒)醒了!"); }}
- 创建悟空线程
package com.wait.entity;/** * @version 2017-12-20 下午4:07:02 */public class WuKong extends Thread{ private Ghost ghost; public WuKong(Ghost ghost) { super(); this.ghost = ghost; } @Override public void run() { super.run(); System.out.println("悟空抓住了妖精"); synchronized (ghost) { //案例一使用 ghost.notify(); //案例二使用 ghost.notifyAll(); } }}
- 创建测试类
package com.wait.entity;/** * @version 2017-12-20 下午4:10:40 */public class TestNotify { public static void main(String args[]){ //创建妖精对象 Ghost ghost = new Ghost(); ghost.start(); //创建八戒线程 BaJie bajie = new BaJie(ghost); bajie.start(); //创建悟空的线程 WuKong wukong = new WuKong(ghost); wukong.start(); }}
案例二:在案例一的基础上,加上沙僧类和将TestNotify类,替换成TestNotifyAll类
- 创建沙僧类
package com.wait.entity;/** * @author 张丹华 * @version 2017-12-20 下午4:21:42 */public class ShaSeng extends Thread{ private Ghost ghost; public ShaSeng(Ghost ghost) { super(); this.ghost = ghost; } @Override public void run() { System.out.println("徒儿,快来救师傅!"); synchronized(ghost){ try { ghost.wait(4000); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("徒儿,我们继续赶路吧!"); }}
- TestNotifyAll类
package com.wait.entity;/** * @version 2017-12-20 下午4:10:40 */public class TestNotify { public static void main(String args[]) throws InterruptedException{ //创建妖精对象 Ghost ghost = new Ghost(); ghost.start(); //创建八戒线程 BaJie bajie = new BaJie(ghost); bajie.start(); //创建沙僧线程 ShaSeng shaseng = new ShaSeng(ghost); shaseng.start(); Thread.sleep(1000); //创建悟空的线程 WuKong wukong = new WuKong(ghost); wukong.start(); }}
以上就是我关于wait()方法、notify()方法和notifyAll()方法的理解;如有不懂的地方,欢迎在下面留言。
阅读全文
0 0
- 线程(7)--wait()方法、notify()方法、notifyAll()方法
- wait notify notifyall 线程同步方法简述
- 线程通信(wait()、notify()、notifyAll() 方法)
- 线程,对象的wait(),notify(),notifyAll()方法实例解析
- sleep() yield() wait() notify() notifyAll()方法
- Java中的wait、notify、notifyall方法
- java多线程:wait(),notify(),notifyAll()方法
- java notify() notifyAll() wait()方法学习
- Synchronized wait() notifyAll/notify 另一个方法(合作关系)
- Object的wait、notify、notifyall方法解析
- Wait Set及wait、notify、notifyAll方法解读
- java 线程 wait() notify() 方法
- 线程wait( )、sleep( )、notify( )方法
- 线程wait( )、sleep( )、notify( )方法
- java的wait方法和notify还有notifyAll方法
- Java中的wait()方法与notify(),notifyAll()方法
- 多线程五,线程间通信,wait、notify,notifyAll方法(14,毕向东老师)
- 多线程五,线程间通信2,wait、notify,notifyAll方法(14,毕向东老师)
- display two or more logcat filters at the same time in Android Studio
- 管中窥豹IIoT(一):工业物联网价值与解决方案通用模型
- LeetCode日常刷题(3)
- C语言中比较字符串大小
- Centos7.3_64位系统下yum命令安装MySQL5.7
- 线程(7)--wait()方法、notify()方法、notifyAll()方法
- 4-apache kylin企业级开源大数据分析平台
- poi事件驱动模式导入读取大批量excel2003/2007(减少内存消耗)
- 为什么“15。。。”会导致微信ANR?
- 腾讯云_tomcat_SSL证书安装
- java源码之Arraylist
- 访问Google的两种方法
- Jmeter学习—001—JDBC测试计划—连接Oracle
- 验证用户名只包含字母,中文