jdk有几个BlockingQueue阻塞队列的实现类,总结一下,不同点及用处。
测试代码
package com.example.demo2.cusdemo.queuedemo;
import java.util.Random;
import java.util.concurrent.*;
public class TestBlockQueue {
public static void main(String[] args) {
// BlockingQueue q = new ArrayBlockingQueue(3);
// BlockingQueue q = new PriorityBlockingQueue(3);
// BlockingQueue q = new SynchronousQueue();
BlockingQueue q = new DelayQueue();
// BlockingQueue q = new LinkedBlockingQueue(3);
Producer p = new Producer(q);
Consumer c1 = new Consumer(q);
Consumer c2 = new Consumer(q);
new Thread(p).start();
new Thread(c1).start();
new Thread(c2).start();
}
}
class Producer implements Runnable {
private final BlockingQueue queue;
Producer(BlockingQueue q) { queue = q; }
public void run() {
try {
while (true) { queue.put(produce()); }
// while (true) { queue.add(produce()); }
// while (true) { queue.offer(produce(),5, TimeUnit.MILLISECONDS); }
} catch (Exception ex) {
ex.printStackTrace();
}
}
Object produce() {
return new Random().nextInt(50);
}
}
class Consumer implements Runnable {
private final BlockingQueue queue;
Consumer(BlockingQueue q) { queue = q; }
public void run() {
try {
while (true) { consume(queue.take()); }
} catch (InterruptedException ex) {
}
}
void consume(Object x) {
System.out.println(Thread.currentThread().getName()+"---消费者---"+x);
}
}
看类图
第一个:ArrayBlockingQueue
入队方法
public boolean add(E e) ---- 内部会调用offer(e),返回false就抛异常,需要自己捕获,否则线程遇到这个异常就结束了
public boolean offer(E e)----往队列里添加元素,添加成功返回true,添加失败返回false。
public void put(E e) throws InterruptedException---这个方法,使用最多,但是要注意使用场景,往队列添加元素时,队列满了,线程会进入阻塞,等元素消费为0时,再唤醒线程添加。在实际使用中,就会造成超时。任务添加失败。需要使用者综合考虑
public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException---
这个方法是,如果队列满了,就阻塞一段时间,再判断队列中是否有空位置,有---再进行入队,入队成功返回true ;没有count == items.length,就返回false;
出队方法
public E poll()----队列中count=0就返回null,否则返回队列中的元素
public E take() throws InterruptedException----从队列中取值,队列中有元素,就返回,没有就阻塞,和put()方法配合使用
public E poll(long timeout, TimeUnit unit) throws InterruptedException---队列有元素,取出返回,没有就阻塞一段时间,看队列中是否有元素,还没有返回null。
public E peek(),从队列中取出第一个元素,有可能为null
优点
数组队列,采用的是环形数组,存取效率比较高。但是存取用的是同一个锁lock。这也体现了读写互斥原则。效率相对就慢了。
第二个:LinkedBlockingQueue
存取采用了2个锁
private final ReentrantLock takeLock = new ReentrantLock();
/** Wait queue for waiting takes */
private final Condition notEmpty = takeLock.newCondition();
/** Lock held by put, offer, etc */
private final ReentrantLock putLock = new ReentrantLock();
/** Wait queue for waiting puts */
private final Condition notFull = putLock.newCondition();
这是一个单项链表结构
static class Node<E> {
E item;
/**
* One of:
* - the real successor Node
* - this Node, meaning the successor is head.next
* - null, meaning there is no successor (this is the last node)
*/
Node<E> next;
Node(E x) { item = x; }
}
这个存取效率相对比较高
第三个:PriorityBlockingQueue
优先级队列:这个队列维护了一个小顶堆,会根据排序规则,堆顶存最小的元素。如果排序规则变化,就是大顶堆了。每次取出的时候,都是取顶部元素。
使用场景:会员优先买票。级别高的优先做某些事情。
第四个:SynchronousQueue
同步队列,可以指定公平和非公平方式。队列中每次放一个任务,放第二个的时候就阻塞了,等待消费者,消费完,才会唤醒生成线程,继续王队列放任务。
第五个:DelayQueue
延时队列,这个会计算延时多久,然后线程将阻塞一段时间,再继续执行任务。