wait, notify 使用清晰讲解
2024-09-05 17:45:35
一个庙里, 三个和尚,只有一个碗, 三个和尚都要吃饭,所以每次吃饭的时候, 三个和尚抢着碗吃。
package interview.java.difference.l05; public class WaitAndNotifyAndNotifyAll { static class Bowl{
private String id; public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} } static class Monk1Eat implements Runnable{
private Bowl bowl;
private String name; public Monk1Eat(Bowl bowl){
this.bowl=bowl;
name="monk1";
} public String getName() {
return name;
} public void run() {
while(true){
System.out.println(this.getName()+" is eating with bowl"+bowl.getId());
}
} } static class Monk2Eat implements Runnable{
private Bowl bowl;
private String name; public Monk2Eat(Bowl bowl){
this.bowl=bowl;
name="monk2";
} public String getName() {
return name;
} public void run() { while(true){
System.out.println(this.getName()+" is eating with bowl"+bowl.getId());
}
} } static class Monk3Eat implements Runnable{
private Bowl bowl;
private String name; public Monk3Eat(Bowl bowl){
this.bowl=bowl;
name="monk3";
} public String getName() {
return name;
} public void run() {
while(true){
System.out.println(this.getName()+" is eating with bowl"+bowl.getId());
}
} } public static void main(String[] args) {
Bowl bowl = new Bowl();
bowl.setId("onlyOneBowl");
Thread monk1=new Thread(new Monk1Eat(bowl));
Thread monk2=new Thread(new Monk2Eat(bowl));
Thread monk3=new Thread(new Monk3Eat(bowl));
monk1.start();
monk2.start();
monk3.start();
}
}
之后 三个和尚懂得互相谦让了。 每个人吃5分钟,换另一个人吃,随机换,这时候,停下吃的那个和尚等待,并把碗给另一个人。注意,是锁wait,然后锁notify另一个人。不是和尚wait然后notify,这样会报出illegalmonitorstate的异常。
package interview.java.difference.l05; public class WaitAndNotifyAndNotifyAll { static class Bowl{
private String id; public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} } static class Monk1Eat implements Runnable{
private Bowl bowl;
private String name;
private long now; public Monk1Eat(Bowl bowl){
this.bowl=bowl;
name="monk1";
now=System.currentTimeMillis();
} public String getName() {
return name;
} public void run() {
synchronized (bowl) {
while(true){
try {
bowl.wait(3*1000);
bowl.notify();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(this.getName()+" is eating with bowl"+bowl.getId());
try {
System.out.println(this.getName()+" is digesting");
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} } } static class Monk2Eat implements Runnable{
private Bowl bowl;
private String name;
private long now; public Monk2Eat(Bowl bowl){
this.bowl=bowl;
name="monk2";
now=System.currentTimeMillis();
} public String getName() {
return name;
} public void run() {
synchronized (bowl) {
while(true){
try {
bowl.wait(3*1000);
bowl.notify();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(this.getName()+" is eating with bowl"+bowl.getId());
try {
System.out.println(this.getName()+" is digesting");
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} } } static class Monk3Eat implements Runnable{
private Bowl bowl;
private String name;
private long now; public Monk3Eat(Bowl bowl){
this.bowl=bowl;
name="monk3";
now=System.currentTimeMillis();
} public String getName() {
return name;
} public void run() {
synchronized (bowl) {
long now=System.currentTimeMillis();
while(true){
try {
bowl.wait(3*1000);
bowl.notify();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(this.getName()+" is eating with bowl"+bowl.getId());
try {
System.out.println(this.getName()+" is digesting");
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} } } public static void main(String[] args) {
Bowl bowl = new Bowl();
bowl.setId("onlyOneBowl");
Thread monk1=new Thread(new Monk1Eat(bowl));
Thread monk2=new Thread(new Monk2Eat(bowl));
Thread monk3=new Thread(new Monk3Eat(bowl));
monk1.start();
monk2.start();
monk3.start();
}
}
使用线程去notify 和wait
代码:
package interview.java.difference.l05; public class WaitAndNotifyAndNotifyAll { static class Bowl{
private String id; public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} } static class Monk1Eat implements Runnable{
private Bowl bowl;
private String name;
private long now; public Monk1Eat(Bowl bowl){
this.bowl=bowl;
name="monk1";
now=System.currentTimeMillis();
} public String getName() {
return name;
} public void run() {
synchronized (bowl) {
while(true){
try {
Thread.currentThread().wait(3*1000);
Thread.currentThread().notify();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(this.getName()+" is eating with bowl"+bowl.getId());
try {
System.out.println(this.getName()+" is digesting");
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} } } static class Monk2Eat implements Runnable{
private Bowl bowl;
private String name;
private long now; public Monk2Eat(Bowl bowl){
this.bowl=bowl;
name="monk2";
now=System.currentTimeMillis();
} public String getName() {
return name;
} public void run() {
synchronized (bowl) {
while(true){
try {
Thread.currentThread().wait(3*1000);
Thread.currentThread().notify();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(this.getName()+" is eating with bowl"+bowl.getId());
try {
System.out.println(this.getName()+" is digesting");
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} } } static class Monk3Eat implements Runnable{
private Bowl bowl;
private String name;
private long now; public Monk3Eat(Bowl bowl){
this.bowl=bowl;
name="monk3";
now=System.currentTimeMillis();
} public String getName() {
return name;
} public void run() {
synchronized (bowl) {
long now=System.currentTimeMillis();
while(true){
try {
Thread.currentThread().wait(3*1000);
Thread.currentThread().notify();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(this.getName()+" is eating with bowl"+bowl.getId());
try {
System.out.println(this.getName()+" is digesting");
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} } } public static void main(String[] args) {
Bowl bowl = new Bowl();
bowl.setId("onlyOneBowl");
Thread monk1=new Thread(new Monk1Eat(bowl));
Thread monk2=new Thread(new Monk2Eat(bowl));
Thread monk3=new Thread(new Monk3Eat(bowl));
monk1.start();
monk2.start();
monk3.start();
}
}
报错:
Exception in thread "Thread-0" Exception in thread "Thread-2" Exception in thread "Thread-1" java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at interview.java.difference.l05.WaitAndNotifyAndNotifyAll$Monk1Eat.run(WaitAndNotifyAndNotifyAll.java:)
at java.lang.Thread.run(Unknown Source)
java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at interview.java.difference.l05.WaitAndNotifyAndNotifyAll$Monk2Eat.run(WaitAndNotifyAndNotifyAll.java:)
at java.lang.Thread.run(Unknown Source)
java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at interview.java.difference.l05.WaitAndNotifyAndNotifyAll$Monk3Eat.run(WaitAndNotifyAndNotifyAll.java:)
at java.lang.Thread.run(Unknown Source)
最新文章
- SSIS 实例——将SQL获取的信息传递到Email中
- 好玩的SQL
- Java for LeetCode 070 Climbing Stairs
- jsp页面之间传参用el表达式获取
- React 初探
- 必须会的SQL语句(六)查询
- Oracle 存储过程实例
- 【HDOJ】1045 Fire Net
- jersey构建rest服务返回json数据
- windows oid 利用SNMP获得主机信息(转)
- 龙邱STM32单片机用J-LINK下载无法被识别的解决方法
- AJAX校验商品价格(类似校验用户名)
- arm swi 软中断 一例
- HDU 5914 Triangle(打表——斐波那契数的应用)
- LODOP打印超过后隐藏内容样式
- python之路--初识面向对象
- java 编程思想
- Xadmin显示视图
- springboot系列三、springboot 单元测试、配置访问路径、多个配置文件和多环境配置,项目打包发布
- 搭建ssh框架项目(二)
热门文章
- shell基础#1
- day 02 while 循环 格式化输出 运算符 and or not - 编码的初识
- npm学习(八)之如何使用语义化版本
- WPF C# 字符串读写文件
- 005-监控项item详解,手动创建item实例
- 牛客练习赛47 A	DongDong破密码 (异或性质,递推)
- MYSQL explain详解[转载]
- DP | Luogu P1466 集合 Subset Sums
- 洛谷P3374 【模板】树状数组 1&;&;P3368 【模板】树状数组 2题解
- 【洛谷P2292】L语言