queue队列,先进先出
1、优先级队列,元素有优先级
public class PriorityQueue<E> extends AbstractQueue<E> implements java.io.Serializable
队列使用堆排序,二叉树,使用数组保存数据,非线程安全
入队,新节点一直跟自己的父节点比较,不复合比较条件,交换位置,直到根节点
public boolean offer(E e) {
if (e == null)
throw new NullPointerException();
modCount++;
int i = size;
if (i >= queue.length)
grow(i + 1);
size = i + 1;
if (i == 0)
queue[0] = e;
else
siftUp(i, e);
return true;
}
private void siftUp(int k, E x) {
if (comparator != null)
siftUpUsingComparator(k, x);
else
siftUpComparable(k, x);
}
private void siftUpUsingComparator(int k, E x) {
while (k > 0) {
int parent = (k - 1) >>> 1;
Object e = queue[parent];
if (comparator.compare(x, (E) e) >= 0)
break;
queue[k] = e;
k = parent;
}
queue[k] = x;
}
private void siftDownUsingComparator(int k, E x) {
int half = size >>> 1;
while (k < half) {
int child = (k << 1) + 1;
Object c = queue[child];
int right = child + 1;
if (right < size &&
comparator.compare((E) c, (E) queue[right]) > 0)
c = queue[child = right];
if (comparator.compare(x, (E) c) <= 0)
break;
queue[k] = c;
k = child;
}
queue[k] = x;
}
对应线程安全的PriorityBlockingQueue,使用ReentrantLock,在入队出队总量等方法加锁,出队无值时候加入Condition队列
2、延时队列,元素有优先级和延时时间,出队时,时间未到,线程等待
public class DelayQueue<E extends Delayed> extends AbstractQueue<E> implements BlockingQueue<E>
返回并移出队列头,如果元素没有到过期时间,则等待
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();//leader线程需要取完值后唤醒等待队列上的第一个线程,使它去尝试取新的队列头
lock.unlock();
}
}
3、阻塞队列BlockingQueue,即入队时空间不足,线程等待有空间时继续入队,出队时队列为空,线程等待直到有元素
常用的有ArrayBlockingQueue,LinkedBlockingQueue,SynchronousQueue
ArrayBlockingQueue,初始化有界数组,数组存储
public ArrayBlockingQueue(int capacity, boolean fair) {
if (capacity <= 0)
throw new IllegalArgumentException();
this.items = new Object[capacity];
lock = new ReentrantLock(fair);
notEmpty = lock.newCondition();
notFull = lock.newCondition();
}
两个Condition,分别用来等待入队的线程,和出队的线程,入队后唤醒notEmpty,出队后唤醒notFull
LinkedBlockingQueue,有界,或者默认Integer.MAX_VALUE,使用链表存储节点,
/** Lock held by take, poll, etc */
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();
SynchronousQueue,没有容量或者"只有一个长度",入队的线程直接阻塞,直到有出队的线程出现,双方交换数据,有点类似握手
有公平非公平两种模式,默认非公平,