---
title: "Java 多线程例子 线程同步互斥操作 生产者消费者进程"
author: "wisgood"
date: "2012-10-18T10:39:13Z"
tags: []
categories: ["编程语言"]
slug: "java"
draft: false
lastmod: "2012-10-18T10:39:13Z"
id: 1700775
url: "https://wisgood.iteye.com/blog/1700775"
---

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;
	}
	
}
```