生产者与消费者小例子
来源:互联网 发布:机顶盒桌面软件 编辑:程序博客网 时间:2024/04/29 18:05
package 生产者与消费者;public class FoodClass {//food是共享的资源private String food;//当cooker为false,表示锅不是空的private boolean cooker = false;public FoodClass() {}public synchronized void set(String food) {if(!cooker) {try {this.wait();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}} this.food = food;this.cooker = false;this.notify();}public synchronized void get() {if(cooker) {try {this.wait();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}System.out.println(food);this.cooker = true;this.notify();}}
package 生产者与消费者;public class SetFoodClass implements Runnable{<span style="white-space:pre"></span><span style="white-space:pre"></span>private FoodClass fc;<span style="white-space:pre"></span>private int x = 0;<span style="white-space:pre"></span><span style="white-space:pre"></span>public SetFoodClass(FoodClass fc) {<span style="white-space:pre"></span>this.fc = fc;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>@Override<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>if(x % 2 != 0) {<span style="white-space:pre"></span>fc.set("吃饭!!!!!!!!!!!!!!!!!!!!!!!");<span style="white-space:pre"></span>} else {<span style="white-space:pre"></span>fc.set("喝粥!");<span style="white-space:pre"></span>}<span style="white-space:pre"></span>x++;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}}
package 生产者与消费者;public class GetFoodClass implements Runnable{<span style="white-space:pre"></span><span style="white-space:pre"></span>private FoodClass fc;<span style="white-space:pre"></span><span style="white-space:pre"></span>public GetFoodClass(FoodClass fc) {<span style="white-space:pre"></span>this.fc = fc;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>@Override<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>fc.get();<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}
package 生产者与消费者;public class TestClass {<span style="white-space:pre"></span><span style="white-space:pre"></span>public static void main(String[] args) {<span style="white-space:pre"></span><span style="white-space:pre"></span>FoodClass fc = new FoodClass();<span style="white-space:pre"></span><span style="white-space:pre"></span>SetFoodClass sfc = new SetFoodClass(fc);<span style="white-space:pre"></span>GetFoodClass gfc = new GetFoodClass(fc);<span style="white-space:pre"></span><span style="white-space:pre"></span>Thread th1 = new Thread(sfc);<span style="white-space:pre"></span>Thread th2 = new Thread(gfc);<span style="white-space:pre"></span><span style="white-space:pre"></span>th1.start();<span style="white-space:pre"></span>th2.start();<span style="white-space:pre"></span>}}
主要是锁关键字synchronized和wait()还有notify()的使用,访问同一资源用同一把锁才可以。
后来想使用计时器来实现,可是发现程序却死在那里了,不知道为什么,下次再尝试吧。
0 0
- 生产者与消费者小例子
- 《生产者与消费者》简单例子?
- 线程同步 生产者消费者小例子
- 关于生产者与消费者的例子
- java---生产者与消费者的例子
- 操作系统多线程例子--生产者与消费者
- Thread生产者消费者例子
- Java生产者消费者例子
- 一个生产者与消费者模型的多线程例子程序
- 面向对象写生产者与消费者的例子(java)
- 学习和理解JAVA线程同步--生产者与消费者例子
- 学习和理解JAVA线程同步--生产者与消费者例子
- linux线程,生产者消费者例子
- 生产者-消费者的经典例子
- 多线程--生产者消费者--简单例子
- 多线程生产者和消费者例子
- <JAVA>多线程-生产者消费者例子
- Java生产者消费者例子尝试
- UVA 1395 Slim Span
- MVC 自定义AuthorizeAttribute实现权限管理
- Linux系统备份与恢复--概述
- solr-数据库导入
- Ubuntu12.04-git的安装
- 生产者与消费者小例子
- 文件切割与合并
- 给刚出道的游戏开发人员的几点忠告
- error could not create the java virtual machine.error:a fatal exception has occurred...问题之解决
- UVA 1151 Buy or Build
- 计算一个文件夹里的字符数
- 树
- VS2008 + Qt4设置exe图标
- C#中的"#region"跟"#endregion"以及public,string,using,等进一步的了解