生产者消费者模式引子
来源:互联网 发布:微信群发软件 编辑:程序博客网 时间: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
- 生产者消费者模式引子
- 生产者—消费者模式
- 生产者-消费者模式
- 消费者与生产者模式
- 消费者与生产者模式
- 消费者与生产者模式
- 生产者消费者模式浅析
- 生产者消费者模式
- 生产者/消费者模式
- 生产者/消费者模式
- 生产者-消费者模式
- 【转】生产者消费者模式
- 生产者、消费者模式
- 生产者/消费者模式
- 生产者/消费者模式
- 生产者/消费者模式(转载)
- 生产者消费者模式浅析
- 生产者消费者模式浅析
- linux编程学习3-ISO C标准定义的头文件
- 第12周项目1- 教师兼干部类
- Internal Sorting: Distribution counting: Sorting by counting
- 判断整数序列是不是二元查找树的后序遍历结果
- java中重写、重载、多态几个概念区别分析
- 生产者消费者模式引子
- HDU 2020 绝对值排序
- 第12周项目2 - 摩托车继承自行车和机动车
- 查找最小的k个元素(堆处理和非堆处理)
- Android FragmentPagerAdapter 数据刷新问题
- Spring中bean的初始化方法
- Python学习之四sys.argv
- 高并发服务器学习之原子性
- CentOS6.5 yum安装nginx