生产者消费者模式引子

来源:互联网 发布:微信群发软件 编辑:程序博客网 时间:2024/06/17 17:49

最近在看Java多线程这块的知识,遇到不少问题,感觉这个难点比较棘手,下面给出自己写的一个消费者生成者Demo。在写的时候,遇到不少奇怪的现象,经过自己反复琢磨与调试,基本上运行正常,附上代码,供所需者参考。

package com.abc.example;public class Test {public static void main(String[] args){<span style="white-space:pre"></span>Container container=new Container();<span style="white-space:pre"></span>Producer pd=new Producer(container);<span style="white-space:pre"></span>Consumer cs=new Consumer(container);<span style="white-space:pre"></span>new Thread(pd).start();<span style="white-space:pre"></span>new Thread(cs).start();}}



package com.abc.example;public class Consumer implements Runnable{<span style="white-space:pre"></span>private Container container=null;<span style="white-space:pre"></span>int cnt=0;<span style="white-space:pre"></span>public Consumer(Container container)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>this.container=container;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>public void run() {<span style="white-space:pre"></span>// TODO Auto-generated method stub<span style="white-space:pre"></span>while(true)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>synchronized (container) {<span style="white-space:pre"></span>try { <span style="white-space:pre"></span>while((cnt=container.getNum())==0)<span style="white-space:pre"></span>{ <span style="white-space:pre"></span>System.out.println("空了,请等待");<span style="white-space:pre"></span>container.wait(); <span style="white-space:pre"></span>} <span style="white-space:pre"></span>cnt--;<span style="white-space:pre"></span>container.setNum(cnt);<span style="white-space:pre"></span>System.out.println("拿走一件东西,目前还有"+container.getNum()+"件东西");<span style="white-space:pre"></span>Thread.sleep((long)Math.random()*3000);<span style="white-space:pre"></span>container.notifyAll();<span style="white-space:pre"></span>}catch (InterruptedException e) {<span style="white-space:pre"></span>// TODO Auto-generated catch block<span style="white-space:pre"></span>e.printStackTrace();<span style="white-space:pre"></span>}<span style="white-space:pre"></span>} <span style="white-space:pre"></span>}<span style="white-space:pre"></span>}}



package com.abc.example;public class Producer implements Runnable{private Container container=null;int cnt=0;public Producer(Container container){this.container=container;}public void run() {// TODO Auto-generated method stubwhile(true){synchronized (container) {try {while((cnt=container.getNum())==12){System.out.println("满了,请等待");container.wait();} cnt++;container.setNum(cnt);System.out.println("放入一件东西,目前有"+container.getNum()+"件东西");Thread.sleep((long)Math.random()*3000);container.notifyAll();}catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}}



package com.abc.example;public class Container {private int num;public int getNum() {return num;}public void setNum(int num) {this.num = num;}}


0 0
原创粉丝点击