Java阻塞队列实现生产者消费者

学习java阻塞队列 先要学习ReetrantLock 网址https://www.cnblogs.com/baizhanshi/p/6419268.html

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class test7 {
	private static int queueSize = 10;//容量设置
	private static BlockingQueue queue = new ArrayBlockingQueue(queueSize,false);//true带表公平,false反之,原因如下注释
    /*  这里是构造函数,可以看出来是用ReetrantLock这个来控制公平的
        public ArrayBlockingQueue(int capacity, boolean fair) { 
        if (capacity <= 0)
            throw new IllegalArgumentException();
        this.items = (E[]) new Object[capacity];
        lock = new ReentrantLock(fair);
        notEmpty = lock.newCondition();
        notFull =  lock.newCondition();
    }*/
	public static void main(String[] args)  {
		Producers producer = new Producers(queue);
		Consumers consumer= new Consumers(queue);
		producer.start();
		consumer.start();
	}


}
class Consumers extends Thread{
	BlockingQueue queue;
	public Consumers(BlockingQueue queue) {
		// TODO Auto-generated constructor stub
		this.queue=queue;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		consumer();
	}
	private void consumer() {
		while(true) {
			try {
				Thread.sleep(1);	
				System.out.println("消费者拿了一个"+queue.take()+"号产品");
				
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
class Producers extends Thread{
	BlockingQueue queue;
	public Producers(BlockingQueue queue) {
		this.queue=queue;
	}

	@Override
	public void run() {
		produce();
	}

	private void produce() {
		int i=0;
		while(true){
			try {
				Thread.sleep(1);
				queue.put(i);
				System.out.println("增加了一个"+i+"号产品");
				i++;
				
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}

take源码

public E take() throws InterruptedException {
			    final ReentrantLock lock = this.lock;
			    lock.lockInterruptibly();
			    try {
			        try {
			            while (count == 0)
			                notEmpty.await();
			        } catch (InterruptedException ie) {
			            notEmpty.signal(); // propagate to non-interrupted thread
			            throw ie;
			        }
			        E x = extract();
			        return x;
			    } finally {
			        lock.unlock();
			    }
			}
private E extract() {
        final E[] items = this.items;
        E x = items[takeIndex];
        items[takeIndex] = null;
        takeIndex = inc(takeIndex);
        --count;
        notFull.signal();
        return x;
    }


put源码

public void put(E e) throws InterruptedException {
        if (e == null) throw new NullPointerException();
        final E[] items = this.items;
        final ReentrantLock lock = this.lock;
        lock.lockInterruptibly();
        try {
            try {
                while (count == items.length)
                    notFull.await();
            } catch (InterruptedException ie) {
                notFull.signal(); // propagate to non-interrupted thread
                throw ie;
            }
            insert(e);
        } finally {
            lock.unlock();
        }
    }
private void insert(E x) {
        items[putIndex] = x;
        putIndex = inc(putIndex);
        ++count;
        notEmpty.signal();
    }

以上的源码包下载地址

java源码包下载地址

你可能感兴趣的:(Java阻塞队列实现生产者消费者)