博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
wait, notify 使用清晰讲解
阅读量:5123 次
发布时间:2019-06-13

本文共 11544 字,大约阅读时间需要 38 分钟。

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

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();            }}
View Code

 

之后 三个和尚懂得互相谦让了。 每个人吃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:37)    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:76)    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:116)    at java.lang.Thread.run(Unknown Source)

 

转载于:https://www.cnblogs.com/IamThat/p/6475068.html

你可能感兴趣的文章
WCF揭秘——使用AJAX+WCF服务进行页面开发
查看>>
【题解】青蛙的约会
查看>>
IO流
查看>>
mybatis调用存储过程,获取返回的游标
查看>>
设计模式之装饰模式(结构型)
查看>>
面向对象的设计原则
查看>>
Swift3.0服务端开发(三) Mustache页面模板与日志记录
查看>>
【转】 FPGA设计的四种常用思想与技巧
查看>>
EntityFrameWork 实现实体类和DBContext分离在不同类库
查看>>
autopep8
查看>>
GIT在Linux上的安装和使用简介
查看>>
基于C#编程语言的Mysql常用操作
查看>>
s3c2440实验---定时器
查看>>
MyEclipse10安装SVN插件
查看>>
[转]: 视图和表的区别和联系
查看>>
Regular Experssion
查看>>
图论例题1——NOIP2015信息传递
查看>>
uCOS-II中的任务切换-图解多种任务调度时机与问题
查看>>
CocoaPods的安装和使用那些事(Xcode 7.2,iOS 9.2,Swift)
查看>>
Android 官方新手指导教程
查看>>