一個廟裏, 三個和尚,只有一個碗, 三個和尚都要吃飯,因此每次吃飯的時候, 三個和尚搶着碗吃。java
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的異常。ide
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 和waitthis
代碼:spa
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)