一个庙里, 三个和尚,只有一个碗, 三个和尚都要吃饭,所以每次吃饭的时候, 三个和尚抢着碗吃。

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 使用清晰讲解的更多相关文章

  1. 清晰讲解SQL语句中的内连接,通用于Mysql和Oracle,全是干货哦

    本文章目的:力求清晰明了讲解SQL语句的内连接的各种应用,没有深奥的理解! 前奏:这篇文章和下篇文章会将内连接和外连接讲解清楚SQL语句的多表查询常用的有以下几种:两表联合查询(1)内连接(2)外连接 ...

  2. 清晰讲解SQL语句中的外连接,通用于Mysql和Oracle,全是干货哦

    直入主题: 我们做一个操作,将员工SCOTT的部门去掉,再次通过内连接查看数据,看看会产生什么现象? 使用内连接,查询数据 问题:找不到SCOTT员工了,只有13条数据,这显然不合理:这就是内连接的缺 ...

  3. SVM清晰讲解——线性可分问题

    转载作者:liangdas 引言: 1995年Cortes和Vapnik于首先提出了支持向量机(Support Vector Machine),由于其能够适应小样本的分类,分类速度快等特点,性能不差于 ...

  4. 关于清晰讲解linux正则表达式的博文分享

    http://www.cnblogs.com/chengmo/archive/2010/10/10/1847287.html  linux shell 正则表达式(BREs,EREs,PREs)差异比 ...

  5. 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 ...

  6. 清晰讲解LSB、MSB和大小端模式及网络字节序

    时隔一个月又回到了博客园写文章,很开心O(∩_∩)O~~ 今天在做需求的涉及到一个固件版本的概念,其中固件组的人谈到了版本号从MSB到LSB排列,检索查阅后将所得整理如下. MSB.LSB? MSB( ...

  7. Java多线程基础——线程间通信

    在使用多线程的时候,经常需要多个线程进行协作来完成一件事情.在前面两章分析了Java多线程的基本使用以及利用synchronized来实现多个线程同步调用方法或者执行代码块.但上面两章的内容涉及到的例 ...

  8. 从零开始一起学习SLAM | 理解图优化,一步步带你看懂g2o代码

    首发于公众号:计算机视觉life 旗下知识星球「从零开始学习SLAM」 这可能是最清晰讲解g2o代码框架的文章 理解图优化,一步步带你看懂g2o框架 小白:师兄师兄,最近我在看SLAM的优化算法,有种 ...

  9. 大数据入门第二天——基础部分之zookeeper(上)

    一.概述 1.是什么? 根据凡技术必登其官网的原则,我们先去官网瞅一瞅:http://zookeeper.apache.org/ Apache ZooKeeper is an effort to de ...

随机推荐

  1. fid解释

    VID就是VLAN ID,这个意思很明白.PVID就是PORT VID,当一个PORT属于多个VLAN时,当它收到不带TAG的数据时,它 就给数据加上TAG,其中VID=PVID.FID就是FILTE ...

  2. python中几个常见的魔法方法

    首先,什么是魔法方法呢?在python中方法名如果是__xxxx__()的,那么就有特殊的功能,因此叫做"魔法"方法. __ init__()方法 当一个实例被创建的时候调用的初始 ...

  3. 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 H. Skiing

    题意:在这个寒假中,鲍勃有计划在山区度假胜地滑雪.这个滑雪胜地有M个不同的滑雪道和N个不同的标志位于那些转弯处点.从第S标记到第T标志的第i路径具有长度L.每个路径必须遵循降低高度的原则,起点必须严格 ...

  4. python-event事件-模仿红绿灯

    import time import threading event =threading.Event() def lighter(): count=0 event.set()#先设置成绿灯 whil ...

  5. luoguP4578_ [FJOI2018]所罗门王的宝藏

    题意 一个n*m的矩阵,初始值全为0,每一行每一列操作一次可以加1或者减1,问能否操作得到给定矩阵. 分析 行和列的分别的加减是可以相互抵消的,因此我们只需要考虑行的加和列的减. 对于给定矩阵每一个数 ...

  6. Win7安装Visual Studio 2019闪退问题

    最近在Win7 系统上安装最新版的VS2019发现 每次在这个画面之后就闪退了,即便换了台电脑也是一样的情况,于是我意识到,这应该是系统本身的问题 经过调查发现是只需要安装两个更新就可以了 这两个更新 ...

  7. JavaNIO

    Java New IO 简称 nio,在jdk1.4提供了新的api,有如下特性: 1.为所有原始类型提供缓存支持 2.字符集编解码解决方案 3.Channel:新的原始io抽象 4.支持锁和内存映射 ...

  8. .net中对象序列化技术

    序列化是将对象状态转换为可保持或传输的格式的过程.与序列化相对的是反序列化,它将流转换为对象.这两个过程结合起来,可以轻松地存储和传输数据.例如,可以序列化一个对象,然后使用 HTTP 通过 Inte ...

  9. 如何在通过脚手架create-react-app 创建的react项目中配置 less

    首先感慨下 自己竟然有半年没登账户 ,干嘛去啦? 从刚接触vue 接手做两次版本之后 又让我这个小菜鸡 开始开发react项目,连react生命周期还没搞明白的时候 就开始进行第一版本的开发了,第一个 ...

  10. Android开发艺术探索笔记之Activity

    内容来源:Android开发艺术探索第一章:Activity的生命周期与启动模式 不能在onPause中做重量级的操作,因为必须执行完成以后新Activity才能Resume.onPause和onSt ...