wait, notify 使用清晰讲解
一个庙里, 三个和尚,只有一个碗, 三个和尚都要吃饭,所以每次吃饭的时候, 三个和尚抢着碗吃。
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)
wait, notify 使用清晰讲解的更多相关文章
- 清晰讲解SQL语句中的内连接,通用于Mysql和Oracle,全是干货哦
本文章目的:力求清晰明了讲解SQL语句的内连接的各种应用,没有深奥的理解! 前奏:这篇文章和下篇文章会将内连接和外连接讲解清楚SQL语句的多表查询常用的有以下几种:两表联合查询(1)内连接(2)外连接 ...
- 清晰讲解SQL语句中的外连接,通用于Mysql和Oracle,全是干货哦
直入主题: 我们做一个操作,将员工SCOTT的部门去掉,再次通过内连接查看数据,看看会产生什么现象? 使用内连接,查询数据 问题:找不到SCOTT员工了,只有13条数据,这显然不合理:这就是内连接的缺 ...
- SVM清晰讲解——线性可分问题
转载作者:liangdas 引言: 1995年Cortes和Vapnik于首先提出了支持向量机(Support Vector Machine),由于其能够适应小样本的分类,分类速度快等特点,性能不差于 ...
- 关于清晰讲解linux正则表达式的博文分享
http://www.cnblogs.com/chengmo/archive/2010/10/10/1847287.html linux shell 正则表达式(BREs,EREs,PREs)差异比 ...
- Coursera公开课笔记: 斯坦福大学机器学习第六课“逻辑回归(Logistic Regression)” 清晰讲解logistic-good!!!!!!
原文:http://52opencourse.com/125/coursera%E5%85%AC%E5%BC%80%E8%AF%BE%E7%AC%94%E8%AE%B0-%E6%96%AF%E5%9D ...
- 清晰讲解LSB、MSB和大小端模式及网络字节序
时隔一个月又回到了博客园写文章,很开心O(∩_∩)O~~ 今天在做需求的涉及到一个固件版本的概念,其中固件组的人谈到了版本号从MSB到LSB排列,检索查阅后将所得整理如下. MSB.LSB? MSB( ...
- Java多线程基础——线程间通信
在使用多线程的时候,经常需要多个线程进行协作来完成一件事情.在前面两章分析了Java多线程的基本使用以及利用synchronized来实现多个线程同步调用方法或者执行代码块.但上面两章的内容涉及到的例 ...
- 从零开始一起学习SLAM | 理解图优化,一步步带你看懂g2o代码
首发于公众号:计算机视觉life 旗下知识星球「从零开始学习SLAM」 这可能是最清晰讲解g2o代码框架的文章 理解图优化,一步步带你看懂g2o框架 小白:师兄师兄,最近我在看SLAM的优化算法,有种 ...
- 大数据入门第二天——基础部分之zookeeper(上)
一.概述 1.是什么? 根据凡技术必登其官网的原则,我们先去官网瞅一瞅:http://zookeeper.apache.org/ Apache ZooKeeper is an effort to de ...
随机推荐
- QQ音乐爬虫
#今日目标 **QQ音乐爬虫** 今天要爬取的是QQ音乐任意歌手的所有音乐歌词,因为笔者是周杰伦的忠实粉丝,所以专门写了个爬虫来爬取他的音乐的歌词,因为他的音乐在咪咕音乐可以听,所以便没有去爬取. 好 ...
- luogu P4383 [九省联考2018]林克卡特树lct
传送门 题目操作有点奇怪,不过可以发现这就是把树先变成\(k+1\)个连通块,然后每个连通块选一条路径(本题中一个点也是一条路径),然后依次接起来.所以实际上要求的是选出\(k+1\)条点不相交的路径 ...
- luffyapi项目 --短信认证的基本操作
一.开通腾讯云短信 SDK 文档 :https://cloud.tencent.com/document/product/382/11672 1.官网注册实名账号:https://cloud.tenc ...
- 一、Signalr WebApi客服-数据传输实体
一.定义消息传输的格式 res不受自己控制 接受ret是自己处理,但是必须包含头像等一系列信息,所有发送的时候消息也是需要传头像的.
- auto_ptr为什么不能做为vector的元素
昨天看effectve c++的时候,发现了auto_ptr这个东西.由于我待过的公司都是用的老版c++,代码里智能指针什么的完全没有出现过,都是直接操作的原始指针.虽说我很少出错,但是总归还是不太安 ...
- 魔术矩阵Java代码
//该魔术矩阵默认从右上角45度递增 //@漫流——595128841在qq点com //import java.util.Arrays; //用于打印API函数 public class 魔方矩阵 ...
- HDU5840 Problem This world need more Zhu 分块 树剖
给一颗n个点的有点权的树,有m个询问,对于每个询问u,v,k,首先将点u到点v的最短路径上的所有点按顺序编号,u的编号为1,求树链上所有点的新编号cnt满足cnt%k==0的点的权值的最大值.n,m, ...
- Windows navcat 连接虚拟机mysql
linux下mysql的安装与使用 https://www.cnblogs.com/shenjianping/p/10984540.html linux安装mysql教程 https://www.cn ...
- 【HDU4276】The Ghost Blows Light
题目大意:给定一棵有根树,1 号节点为根节点,点有点权,边有边权,初始给定一个价值,每经过一条边都会减少该价值,每经过一个点都会增加相应的答案贡献值,求如何在给定价值的情况下最大化答案贡献,并要求最后 ...
- java 死锁演示
java 死锁演示 java死锁 模拟死锁生成 死锁是由多个线程竞争同一个资源导致 package com.feshfans; /** * 1. 本代码为展示 java 中死锁的产生 * 2. 死锁的 ...