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)
  • 相关阅读:
    获取窗口句柄的几个办法
    我做的第一个网站
    学生信息管理系统
    程序题
    java冒泡排序
    模拟售票窗口,用4个线程模拟4个窗口在售票,共有8张票,用线程同步来实现
    设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1,写出程序。
    windows server 2012将计算机、回收站、文档等图标添加到桌面
    Javascript金额转化
    eclipse 每次切换工作空间都要重新配置
  • 原文地址:https://www.cnblogs.com/IamThat/p/6475068.html
Copyright © 2011-2022 走看看