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<Integer> queue = new ArrayBlockingQueue<Integer>(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<Integer> queue;
	public Consumers(BlockingQueue<Integer> 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<Integer> queue;
	public Producers(BlockingQueue<Integer> 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源码包下载地址

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gentle+

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值