Java 多线程例子 线程同步互斥操作 生产者消费者进程

 

Food类:食物 ,代表缓存里面的产品

FoodCache:缓存,用栈实现

Producer:生产者进程

Consumer:消费者进程

Demo:例子

 

 

 

 

 

 

 

 

 

 

/*
 * @(#)Food.java, 2012-10-18 上午09:49:26
 *
 * All rights reserved.
 */
package com.wang.thread;

/**
 * 在这里加入功能说明
 *
 * @author wangxiaowei
 * @version $Revision: 1.4 $, 2012-10-18$
 */
public class Food
{
	private int id;
	public Food(int id)
	{
		this.id = id;
	}
	/**
	 * @return Returns the id.
	 */
	public int getId()
	{
		return id;
	}

	/**
	 * @param id The id to set.
	 */
	public void setId(int id)
	{
		this.id = id;
	}
}
/*
 * @(#)FoodCache.java, 2012-10-18 上午09:51:24
 *
 * All rights reserved.
 */
package com.wang.thread;

/**
 * 在这里加入功能说明
 *
 * @author wangxiaowei
 * @version $Revision: 1.4 $, 2012-10-18$/*
 * @(#)Consumer.java, 2012-10-18 上午10:13:53
 *
 * All rights reserved.
 */
package com.wang.thread;

/**
 * 在这里加入功能说明
 *
 * @author wangxiaowei
 * @version $Revision: 1.4 $, 2012-10-18$
 */
public class Consumer extends Thread
{
	 private FoodCache foodCache;
	   public Consumer(FoodCache foodCache)
	   {
		   this.foodCache = foodCache;
	   }
	   public void run()
	   {
		   for(int i=0; i<10; i++) 
		   {
			 synchronized(foodCache)
			 {
				 Food food = foodCache.pop();
				 System.out.println("消费了:"+food.getId()+"号食物!");
			 }
		   }
	   }

}
 /*
 * @(#)Producer.java, 2012-10-18 上午10:08:42
 *
 * All rights reserved.
 */
package com.wang.thread;

/**
 * 在这里加入功能说明
 *
 * @author wangxiaowei
 * @version $Revision: 1.4 $, 2012-10-18$
 */
public class Producer extends Thread
{
   private FoodCache foodCache;
   public Producer(FoodCache foodCache)
   {
	   this.foodCache = foodCache;
   }
   public void run()
   {
	   for(int i=0; i<10; i++) 
	   {
		 synchronized(foodCache)
		 {
			 Food food = new Food(i);
			 System.out.println("生产了第"+food.getId()+"号食物");
			 foodCache.push(food);
		 }
	   }
   }
		 
}
 /*
 * @(#)Demo.java, 2012-10-18 上午10:15:53
 *
 * All rights reserved.
 */
package com.wang.thread;

/**
 * 在这里加入功能说明
 *
 * @author wangxiaowei
 * @version $Revision: 1.4 $, 2012-10-18$
 */
public class Demo
{

	/**
	 * 
	 */
	public static void main(String[] args)
	{
		// TODO Auto-generated method stub
		FoodCache foodCache = new FoodCache();
		Producer producer = new Producer(foodCache);
		Consumer consumer = new Consumer(foodCache);
		producer.start();
		consumer.start();

	}

}
  */ public class FoodCache { private int length = 10 ; private Food[] cache = new Food[length]; private int top = -1; public FoodCache() { } public Food pop() { while(top==-1) { try { System.out.println("缓存没有食物了,我的心在等待 ........."); this.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } this.notify(); return cache[top--]; } public void push(Food food) { while(top==length-1) { try { this.wait(); System.out.println("缓存里面的食物已经满了,快来吃我吧!"); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } this.notify(); cache[++top] = food; } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值