Java线程(四) 生产者-消费者实现

方法一:使用 synchronized + wait, notify实现。(一个生产者、一个消费者)

package pack2;

import java.util.ArrayList;
import java.util.Random;

public class PCTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ProductBox box = new ProductBox(5);
		Producer producer = new Producer(box);
		Consumer consumer = new Consumer(box);
		Thread pt = new Thread(producer);
		Thread ct = new Thread(consumer);
		pt.start();
		ct.start();
		try {
			pt.join();
			ct.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
class ProductBox
{
	public ProductBox(int num)
	{
		maxCapacity = num;
		products = new int[num];
		produceIndex = consumeIndex = productsNum = 0;
		generator = new Random();
	}
	public synchronized void produce()
	{
		while(productsNum == maxCapacity)
		{
			try {
				System.out.println("producer waiting now");
				wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		int tmp = generator.nextInt() % 100;
		products[produceIndex] = tmp;
		productsNum ++;
		produceIndex = (produceIndex + 1) % maxCapacity;
		System.out.println("producer thread " + Thread.currentThread().getName() + " produce a new product " + tmp + 
				", current index is " + produceIndex);
		notify();
	}
	public synchronized void consume()
	{
		while(productsNum == 0)
		{
			try {
				System.out.println("consumer waiting now");
				wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		int tmp = products[consumeIndex];
		productsNum --;
		consumeIndex = (consumeIndex + 1) % maxCapacity;
		System.out.println("consumer thread " + Thread.currentThread().getName() + " consume a product " + tmp +
				", current index is " + consumeIndex);
		notify();
	}
	private int maxCapacity;
	private int[] products;
	private int produceIndex;
	private int consumeIndex;
	private int productsNum;
	private Random generator;
}
class Producer implements Runnable
{
	public Producer(ProductBox box)
	{
		productBox = box;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		try{
			for(int i = 0; i < 10; i ++)
			{
				productBox.produce();
				Thread.sleep(10);
			}
		}
		catch(InterruptedException e)
		{
			e.printStackTrace();
		}
	}
	private ProductBox productBox;
}
class Consumer implements Runnable
{

	public Consumer(ProductBox box)
	{
		productBox = box;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		try{
			for(int i = 0; i < 10; i ++)
			{
				productBox.consume();
				Thread.sleep(100);
			}
		}
		catch(InterruptedException e)
		{
			e.printStackTrace();
		}
	}
	private ProductBox productBox;
	
}

方法二: ReetrantLock + await, signal, signalAll实现。( 多个生产者,多个消费者)

import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class PCTest2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ProductBox box = new ProductBox(5);
/*		Producer producer = new Producer(box);
		Consumer consumer = new Consumer(box);
		Thread pt = new Thread(producer);
		Thread ct = new Thread(consumer);*/
		Thread pt[] = new Thread[3];
		Thread ct[] = new Thread[3];
		for(int i = 0; i < 3; i ++)
		{
			Producer producer = new Producer(box);
			Consumer consumer = new Consumer(box);
			pt[i] = new Thread(producer);
			ct[i] = new Thread(consumer);
		}
		for(int i = 0; i < 3; i ++)
		{
			ct[i].start();
			pt[i].start();
		}
		for(int i = 0; i < 3; i ++)
		{
			try {
				pt[i].join();
				ct[i].join();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		Producer.countTotal();
		Consumer.countTotal();
	}

}
class ProductBox
{
	public ProductBox(int num)
	{
		maxCapacity = num;
		products = new int[num];
		produceIndex = consumeIndex = productsNum = 0;
		generator = new Random();
	}
	public void produce()
	{
		lock.lock();
		try
		{
		while(productsNum == maxCapacity)
		{
			try {
				System.out.println("producer " + Thread.currentThread().getName() + " waiting now");
				condCanPoroduce.await();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		int tmp = generator.nextInt() % 100;
		products[produceIndex] = tmp;
		productsNum ++;
		produceIndex = (produceIndex + 1) % maxCapacity;
		System.out.println("producer thread " + Thread.currentThread().getName() + " produce a new product " + tmp + 
				", current totalNum is " + productsNum);
		condCanConsume.signalAll();
		}
		finally{
			lock.unlock();
		}
	}
	public void consume()
	{
		lock.lock();
		try
		{
		while(productsNum == 0)
		{
			try {
				System.out.println("consumer " + Thread.currentThread().getName() + " waiting now");
				condCanConsume.await();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		int tmp = products[consumeIndex];
		productsNum --;
		consumeIndex = (consumeIndex + 1) % maxCapacity;
		System.out.println("consumer thread " + Thread.currentThread().getName() + " consume a product " + tmp +
				", current totalnum is " + productsNum);
		condCanPoroduce.signalAll();
		}
		finally{
			lock.unlock();
		}
	}
	private int maxCapacity;
	private int[] products;
	private int produceIndex;
	private int consumeIndex;
	private int productsNum;
	private Random generator;
	private final ReentrantLock lock = new ReentrantLock();  
	private final Condition condCanPoroduce = lock.newCondition();
	private final Condition condCanConsume = lock.newCondition();
}
class Producer implements Runnable
{
	public Producer(ProductBox box)
	{
		productBox = box;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		try{
			for(int i = 0; i < 30; i ++)
			{
				productBox.produce();
				totalProducts ++;
				Thread.sleep(100);
			}
		}
		catch(InterruptedException e)
		{
			e.printStackTrace();
		}
	}
	public static void countTotal()
	{
		System.out.println("Producers total produced products is " + totalProducts);
	}
	private ProductBox productBox;
	private static int totalProducts = 0;
}
class Consumer implements Runnable
{

	public Consumer(ProductBox box)
	{
		productBox = box;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		try{
			for(int i = 0; i < 30; i ++)
			{
				productBox.consume();
				totalProducts ++;
				Thread.sleep(100);
			}
		}
		catch(InterruptedException e)
		{
			e.printStackTrace();
		}
	}
	public static void countTotal()
	{
		System.out.println("Consumers total consumed products is " + totalProducts);
	}
	private ProductBox productBox;
	private static int totalProducts = 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值