- 简单介绍
DelayQueue是一个延时阻塞队列,实现了BlockingQueue接口,是一个线程安全的队列。它可以根据用户设定的阻塞时间,阻塞队列出队这一操作一定的时间。 - 一个例子
给出一个例子
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;
public class DelayQueueTest {
public static void main(String[] args) throws InterruptedException {
DelayItem<String> delayItem1 = new DelayItem<>("test_1", 10, TimeUnit.SECONDS);
DelayItem<String> delayItem2 = new DelayItem<>("test_2", 5, TimeUnit.SECONDS);
DelayItem<String> delayItem3 = new DelayItem<>("test_3", 1000);
DelayQueue<DelayItem> delayQueue = new DelayQueue<>();
delayQueue.put(delayItem1);
delayQueue.put(delayItem2);
delayQueue.put(delayItem3);
for (int i = 0; i < 3; i++) {
DelayItem delayItem = delayQueue.take();
System.out.println(delayItem.data + "---" +
LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
}
}
static class DelayItem<T> implements Delayed {
private T data;
private long time;
public DelayItem(T data, long time) {
this(data, time, TimeUnit.MILLISECONDS);
}
public DelayItem(T data, long time, TimeUnit unit) {
this.data = data;
this.time = System.currentTimeMillis() + (time > 0 ? unit.toMillis(time) : 0);
}
@Override
public long getDelay(TimeUnit unit) {
long delay = time - System.currentTimeMillis();
return unit.convert(delay, TimeUnit.MILLISECONDS);
}
@Override
public int compareTo(Delayed o) {
DelayItem item = (DelayItem) o;
return time - item.time > 0 ? 1 : -1;
}
}
}
插入到DelayQueue里面的元素必须实现 Delayed接口,Delayed接口继承至Comparable接口
public interface Delayed extends Comparable<Delayed> {
/**
* Returns the remaining delay associated with this object, in the
* given time unit.
*
* @param unit the time unit
* @return the remaining delay; zero or negative values indicate
* that the delay has already elapsed
*/
long getDelay(TimeUnit unit);
}
所以要实现getDelay方法(设置出队操作阻塞的时间)和compareTo方法(设置队列中元素的排列顺序,一般按照延时来排列)。
这个东西是怎么做到延时阻塞的呢
- 阅读源码
它里面的成员变量比较少,先来看看成员变量
private final transient ReentrantLock lock = new ReentrantLock();
private final PriorityQueue<E> q = new PriorityQueue<E>();
private Thread leader = null;
private final Condition available = lock.newCondition();
为了线程安全,它定义了一个ReentrantLock重入锁,以及相对应的Condition。
这里有一个问题,这个ReentrantLock使用了transient对象修饰,但是DelayQueue这个类并没有实现Serializable接口,以及DelayQueue父类一直往上也没有实现Serializable,那为啥要用transient修饰,这里不太理解transient的用途
它内部有一个PriorityQueue优先级队列,这就是为什么Delayed要继承Comparable,因为要根据我们设置的优先级进行排序。
还有一个Thread对象,用于记录使用DelayQueue的主线程,这个有什么用,看接下来的源码
/**
* Inserts the specified element into this delay queue.
*
* @param e the element to add
* @return {@code true} (as specified by {@link Collection#add})
* @throws NullPointerException if the specified element is null
*/
public boolean add(E e) {
return offer(e);
}
/**
* Inserts the specified element into this delay queue.
*
* @param e the element to add
* @return {@code true}
* @throws NullPointerException if the specified element is null
*/
public boolean offer(E e) {
final ReentrantLock lock = this.lock;
lock.lock();
try {
q.offer(e);
if (q.peek() == e) {
leader = null;
available.signal();
}
return true;
} finally {
lock.unlock();
}
}
/**
* Inserts the specified element into this delay queue. As the queue is
* unbounded this method will never block.
*
* @param e the element to add
* @throws NullPointerException {@inheritDoc}
*/
public void put(E e) {
offer(e);
}
这是往队列里面插入元素的方法,执行前lock(),执行后unlock(),为保证线程安全的常用做法,然后调用优先级队列的offer方法(实现于Queue接口)插入元素。之后还做了一个操作,peek方法取出队列的最后一个元素(并不出队),看它是不是就是刚刚添加的元素,如果是就将leader置为空,然后signal()方法激活await的线程。这一个操作的目的是什么呢?凭借经验猜测,大概是让其他所有await线程争夺这个leader。是不是这样?往下看。
/**
* Retrieves and removes the head of this queue, or returns {@code null}
* if this queue has no elements with an expired delay.
*
* @return the head of this queue, or {@code null} if this
* queue has no elements with an expired delay
*/
public E poll() {
final ReentrantLock lock = this.lock;
lock.lock();
try {
E first = q.peek();
if (first == null || first.getDelay(NANOSECONDS) > 0)
return null;
else
return q.poll();
} finally {
lock.unlock();
}
}
这是出队的代码,与一般的出队不同的是多了一个getDelay的判断,这也是我们实现的getDelay方法第一次在代码中出现,获取延时的剩余时间,还有剩余时间就返回空。它没有延时的效果。
/**
* Retrieves and removes the head of this queue, waiting if necessary
* until an element with an expired delay is available on this queue.
*
* @return the head of this queue
* @throws InterruptedException {@inheritDoc}
*/
public E take() throws InterruptedException {
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
for (;;) {
E first = q.peek();
if (first == null)
available.await();
else {
long delay = first.getDelay(NANOSECONDS);
if (delay <= 0)
return q.poll();
first = null; // don't retain ref while waiting
if (leader != null)
available.await();
else {
Thread thisThread = Thread.currentThread();
leader = thisThread;
try {
available.awaitNanos(delay);
} finally {
if (leader == thisThread)
leader = null;
}
}
}
}
} finally {
if (leader == null && q.peek() != null)
available.signal();
lock.unlock();
}
}
这个方法就是例子中从队列里取元素的方法,也是整个DelayQueue最关键的代码(个人认为)。首先for循环不断的读,读到的元素为空,一直阻塞着。不为空,然后剩余时间也没有了,出队并return。然后,判断leader线程是否为空,不为空就阻塞。为空,当前线程设为leader,并且通过awaitNanos方法阻塞一定的时间(也就是元素剩余时间)。还可以看到,leader置为空都在finally代码块中,也就是说,要么出异常,要么程序return,才会把leader置为空。这验证了的刚刚猜测,leader其实就是DelayQueue的主线程,只要leader不为空说明有线程已经在await了而且是调用awaitNanos进入的await。这个时候其他进入take方法的线程都会等主线程执行完。为什么要这样设置,为了一个线程调用awaitNanos等待第一个元素的时候,其他的线程也调用awaitNanos等待第一个元素。
刚刚在读offer方法的时候,也有leader = null 这个操作,它的判断条件是插入的元素排在第一个,这个时候得让激活所有的线程让他们重新去争leader,重新去设置awaitNanos的时间。
这里还有一个让人不太注意的点就是,try里面的代码return的时候,会先执行finally代码块,具体顺序如何,变量如何保持,这里不做说明。
最后再看看另外一个常用的出队的方法
/**
* Retrieves and removes the head of this queue, waiting if necessary
* until an element with an expired delay is available on this queue,
* or the specified wait time expires.
*
* @return the head of this queue, or {@code null} if the
* specified waiting time elapses before an element with
* an expired delay becomes available
* @throws InterruptedException {@inheritDoc}
*/
public E poll(long timeout, TimeUnit unit) throws InterruptedException {
long nanos = unit.toNanos(timeout);
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
for (;;) {
E first = q.peek();
if (first == null) {
if (nanos <= 0)
return null;
else
nanos = available.awaitNanos(nanos);
} else {
long delay = first.getDelay(NANOSECONDS);
if (delay <= 0)
return q.poll();
if (nanos <= 0)
return null;
first = null; // don't retain ref while waiting
if (nanos < delay || leader != null)
nanos = available.awaitNanos(nanos);
else {
Thread thisThread = Thread.currentThread();
leader = thisThread;
try {
long timeLeft = available.awaitNanos(delay);
nanos -= delay - timeLeft;
} finally {
if (leader == thisThread)
leader = null;
}
}
}
}
} finally {
if (leader == null && q.peek() != null)
available.signal();
lock.unlock();
}
}
这个方法是一个定时等待,也就是在等待的时间内,队列头部的元素没有剩余时间了,就返回。跟take方法一样,会抢leader,会阻塞,就是多了几个对delay和nanos的判断。
附带提一下available.awaitNanos(delay)这个方法的返回值,它返回你设定的等待时间与实际的等待时间之差,一般为0,除非提前被signal()激活