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