并发编程-concurrent指南-阻塞双端队列-链阻塞双端队列LinkedBlockingDeque
LinkedBlockingDeque是双向链表实现的阻塞队列。该阻塞队列同时支持FIFO和FILO两种操作方式,即可以从队列的头和尾同时操作(插入/删除);
在不能够插入元素时,它将阻塞住试图插入元素的线程;在不能够抽取元素时,它将阻塞住试图抽取的线程。;
LinkedBlockingDeque还是可选容量的,防止过度膨胀,默认等于Integer.MAX_VALUE。;
LinkedBlockingDueue没有进行读写锁的分离,因此同一时间只能有一个线程对其操作,因此在高并发应用中,它的性能要远远低于LinkedBlockingQueue。
具体代码:
import java.util.concurrent.LinkedBlockingDeque; public class Main { public static void main(String[] args) { //给个3的队列 LinkedBlockingDeque<Integer> linkedBlockingDeque = new LinkedBlockingDeque<Integer>(3); //生产者 Producer firstProducer = new FirstProducer(linkedBlockingDeque);//头部插入 Producer lastProducer = new LastProducer(linkedBlockingDeque);//尾部插入 firstProducer.start(); // lastProducer.start(); //消费者 Consumer firstConsumer = new FirstConsumer(linkedBlockingDeque);//头部获取 Consumer lastConsumer = new LastConsumer(linkedBlockingDeque);//尾部获取 // firstConsumer.start(); lastConsumer.start(); } }
import java.util.Random; import java.util.concurrent.LinkedBlockingDeque; /** * 生产者 */ public abstract class Producer extends Thread{ protected LinkedBlockingDeque<Integer> linkedBlockingDeque; public Producer(LinkedBlockingDeque<Integer> linkedBlockingDeque){ this.linkedBlockingDeque = linkedBlockingDeque; } @Override public void run() { while(true){ int random = new Random().nextInt(1000); try { this.putQueue(random);//插入数据 } catch (InterruptedException e) { e.printStackTrace(); } } } public abstract void putQueue(int random) throws InterruptedException; }
import java.util.concurrent.LinkedBlockingDeque; /** * 头部插入 */ public class FirstProducer extends Producer{ public FirstProducer(LinkedBlockingDeque<Integer> linkedBlockingDeque) { super(linkedBlockingDeque); } @Override public void putQueue(int random) throws InterruptedException { System.out.println("头部生产产品:"+random); super.linkedBlockingDeque.putFirst(random); System.out.println("头部生产产品:"+random+",结束"); } }
import java.util.concurrent.LinkedBlockingDeque; /** * 尾部插入:默认是尾部插入 */ public class LastProducer extends Producer{ public LastProducer(LinkedBlockingDeque<Integer> linkedBlockingDeque) { super(linkedBlockingDeque); } @Override public void putQueue(int random) throws InterruptedException { super.linkedBlockingDeque.putLast(random); } }
import java.util.concurrent.LinkedBlockingDeque; /** * 消费者 */ public abstract class Consumer extends Thread{ protected LinkedBlockingDeque<Integer> linkedBlockingDeque; public Consumer(LinkedBlockingDeque<Integer> linkedBlockingDeque){ this.linkedBlockingDeque = linkedBlockingDeque; } @Override public void run() { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("------------------------------------"); while(true){ try { System.out.println("尾部等待3秒开始获取产品"); Thread.sleep(3000); this.takeQueue();//获取数据 } catch (InterruptedException e) { e.printStackTrace(); } } } public abstract void takeQueue() throws InterruptedException; }
import java.util.concurrent.LinkedBlockingDeque; /** * 头部获取 */ public class FirstConsumer extends Consumer{ public FirstConsumer(LinkedBlockingDeque<Integer> linkedBlockingDeque) { super(linkedBlockingDeque); } @Override public void takeQueue() throws InterruptedException { System.out.println("头部获取产品开始"); Integer random = super.linkedBlockingDeque.takeFirst(); System.out.println("头部获取产品:"+random+",结束"); } }
import java.util.concurrent.LinkedBlockingDeque; /** * 尾部获取 */ public class LastConsumer extends Consumer{ public LastConsumer(LinkedBlockingDeque<Integer> linkedBlockingDeque) { super(linkedBlockingDeque); } @Override public void takeQueue() throws InterruptedException { Integer random = super.linkedBlockingDeque.takeLast(); System.out.println("尾部获取产品:"+random+",结束"); } }
源码地址:https://github.com/qjm201000/concurrent_linkedBlockingDeque.git