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)

 

I'm falling off the sky all alone.The courage inside is gonna break the fall. Nothing can dim my light within. I am That I am 程序 = 数据结构 + 算法
posted @ 2017-02-27 16:48  IamThat  阅读(472)  评论(0编辑  收藏  举报