生产者与消费者小例子

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 block
				e.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 block
				e.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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值