码迷,mamicode.com
首页 > 其他好文 > 详细

ReentrantLock Condition 实现消费者生产者问题

时间:2014-07-27 09:45:42      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:

import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

//产品
class Product
{
	 String name=null;
	public Product(String name)
	{
		this.name=name;
	}
	
}

class  Buffer
{
  private Queue<Product> queue=new LinkedList<Product>();//一个普通队列
  private final int size=5;  //最大长度为,可以自己调整
  public void add(Product p)//
  {
	  synchronized (queue) {
		  while(queue.size()==size)
		  {
			  System.out.println(Thread.currentThread().getName()+"队列已经满了,生产者释放锁");
			  try {
				queue.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		  }
		  queue.offer(p);
		 
		  System.out.println(Thread.currentThread().getName()+"入队    "+queue.size());
		  
		  queue.notify();
		
	}
	  
 }
  
  public void remove()
  {
	  synchronized (queue) {
		  while(queue.size()<=0)
		  {

			  System.out.println(Thread.currentThread().getName()+"队列为空,释放锁");
			  try {
				queue.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		  }
		  queue.poll();
		  System.out.println(Thread.currentThread().getName()+"出队,剩下"+queue.size());
		  queue.notify();
		
	}
	  
  }
	

}
class Buffer2
{ private Queue<Product>  queque=new LinkedList<Product>();
private int size=5;
private ReentrantLock lock=new ReentrantLock(true);
private Condition notFull=lock.newCondition();
private Condition notEmpty=lock.newCondition();
public void add(Product p)
{
	lock.lock();
	try
	{
	while(queque.size()==size)
	{
		
		
			notFull.await();
	}
		
	
	queque.add(p);
	notEmpty.signal();
	} catch (InterruptedException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally
	{
	
	lock.unlock();
	}
	
}
public void remove() 
{
	try {
		lock.lockInterruptibly();
	} catch (InterruptedException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	try
	{
		while(queque.size()==0) notEmpty.await();
		queque.poll();
		notFull.signal();
		
	} catch (InterruptedException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally{
		lock.unlock();
		
	}
	
	
	
}




 
	

}

class Producer implements Runnable
{
	private Buffer2 buf;
	
	public Producer(Buffer2 buf)
	{
		this.buf=buf;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		for(int i=0;i<10;i++)
		{
			try {
				Thread.sleep(3000);  //控制生产速度
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			buf.add(new Product("zhang "+i));
		}
		
		
	}
	


}
class Customer implements Runnable
{
  private Buffer2 buf=null;
  public Customer(Buffer2 buf)
  {
	  this.buf=buf;
  }
	@Override
	public void run() {
		for(int i=0;i<10;i++)
		{
			try {
				Thread.sleep(1);//控制生产速度,,
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}//
			buf.remove();
		}
		
	}
	

}

public class 生产消费者 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//学学使用线程池
		Buffer2 buf=new Buffer2();
		ExecutorService exe=Executors.newCachedThreadPool();
		int i=0;
		while(i++<2)
		{
			exe.submit(new Producer(buf));
			
		}
		i=0;
		while(i++<2)
		{
			exe.submit(new Customer(buf));
		}
		exe.shutdown();

	}

}

  

ReentrantLock Condition 实现消费者生产者问题

标签:

原文地址:http://www.cnblogs.com/hansongjiang/p/3870332.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!