zoukankan      html  css  js  c++  java
  • 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();        
        }
    }
    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)
  • 相关阅读:
    微信公众平台开发心得笔记
    MFC中 在线程里获取主对话框上编辑框中的内容
    MFC中 使用Tab Control 控件在对话框中添加属性页
    MFC中 将std::string转换为LPCTSTR的方法
    MFC中 给按钮添加图片的方法
    MFC中 使用多线程实现语音播放功能
    MFC中 Group Box 控件会覆盖掉部分其他控件
    MFC中 CSting与std::string互相转换
    MFC中 给对话框添加背景图片
    MFC中 通过对子对话框的操作来更新主对话框
  • 原文地址:https://www.cnblogs.com/IamThat/p/6475068.html
Copyright © 2011-2022 走看看