返回目录
api
java.lang.Object
java.util.AbstractCollection<E>
java.util.ArrayDeque<E>
Serializable, Cloneable, Iterable<E>, Collection<E>, Deque<E>, Queue<E>
ArrayDeque()
Constructs an empty array deque with an initial capacity sufficient to hold 16 elements.
ArrayDeque(Collection<? extends E> c)
Constructs a deque containing the elements of the specified collection, in the order they are returned by the collection's iterator.
ArrayDeque(int numElements)
Constructs an empty array deque with an initial capacity sufficient to hold the specified number of elements.
//插入元素
boolean add(E e)
Inserts the specified element at the end of this deque.
void addFirst(E e)
Inserts the specified element at the front of this deque.
void addLast(E e)
Inserts the specified element at the end of this deque.
//插入元素 ,还是调用上面的add方法
boolean offer(E e)
Inserts the specified element at the end of this deque.
boolean offerFirst(E e)
Inserts the specified element at the front of this deque.
boolean offerLast(E e)
Inserts the specified element at the end of this deque.
//获取元素,但是不删除元素
E element()
Retrieves, but does not remove, the head of the queue represented by this deque.
E getFirst()
Retrieves, but does not remove, the first element of this deque.
E getLast()
Retrieves, but does not remove, the last element of this deque.
//获取元素并删除
E poll()
Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque), or returns null if this deque is empty.
E pollFirst()
Retrieves and removes the first element of this deque, or returns null if this deque is empty.
E pollLast()
Retrieves and removes the last element of this deque, or returns null if this deque is empty.
返回目录
api
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractQueue<E>
java.util.concurrent.ArrayBlockingQueue<E>
Serializable, Iterable<E>, Collection<E>, BlockingQueue<E>, Queue<E>
返回目录
api
返回目录
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractQueue<E>
java.util.concurrent.ConcurrentLinkedQueue<E>
Serializable, Iterable<E>, Collection<E>, Queue<E>
返回目录
api
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractQueue<E>
java.util.concurrent.DelayQueue<E>
Iterable<E>, Collection<E>, BlockingQueue<E>, Queue<E>
DelayQueue()
Creates a new DelayQueue that is initially empty.
DelayQueue(Collection<? extends E> c)
Creates a DelayQueue initially containing the elements of the given collection of Delayed instances.
class DelayQueueModel implements Delayed {
// 数据
private String str;
// 延时时间
private long delayTime;
// 到期时间
private long timeOut;
/**
* 构造函数 到期时间 = 延时时间 - 当前系统时间
* @param str 数据
* @param timeOut 延迟时间
*/
DelayQueueModel(String str,long timeOut){
this.str = str;
this.delayTime = timeOut;
this.timeOut = timeOut + System.currentTimeMillis();
}
//返回距离你自定义的超时时间还有多少
public long getDelay(TimeUnit unit){
return unit.convert(timeOut - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
}
//比较getDelay()函数的返回值
public int compareTo(Delayed other){
if (other == this)
return 0;
DelayQueueModel delayQueueModel = (DelayQueueModel)other;
long time = (getDelay(TimeUnit.MILLISECONDS) - delayQueueModel.getDelay(TimeUnit.MILLISECONDS));
return (time == 0) ? 0 : ((time < 0) ? -1 : 1);
}
void print(){
System.out.println("元素:" + str + "\t 延时时间:" + delayTime + "\t 过期时间:" + new Date(timeOut));
}
}
public class DelayQueueDemo {
public static void main(String[] args) {
DelayQueue<DelayQueueModel> queue = new DelayQueue<>();
for(int i = 0; i < 10; i++){
DelayQueueModel delayQueueModel = new DelayQueueModel("第"+ (i + 1) +"个", 1000*(10-i));
queue.put(delayQueueModel);
}
for(int i = 0; i < 15; i++){
try {
queue.take().print();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
输出
最先出队的是delay时间最小的元素,也就是最快过期的元素
元素:第10个 延时时间:1000 过期时间:Sat Dec 08 18:53:51 CST 2018
元素:第9个 延时时间:2000 过期时间:Sat Dec 08 18:53:52 CST 2018
元素:第8个 延时时间:3000 过期时间:Sat Dec 08 18:53:53 CST 2018
元素:第7个 延时时间:4000 过期时间:Sat Dec 08 18:53:54 CST 2018
元素:第6个 延时时间:5000 过期时间:Sat Dec 08 18:53:55 CST 2018
元素:第5个 延时时间:6000 过期时间:Sat Dec 08 18:53:56 CST 2018
元素:第4个 延时时间:7000 过期时间:Sat Dec 08 18:53:57 CST 2018
元素:第3个 延时时间:8000 过期时间:Sat Dec 08 18:53:58 CST 2018
元素:第2个 延时时间:9000 过期时间:Sat Dec 08 18:53:59 CST 2018
元素:第1个 延时时间:10000 过期时间:Sat Dec 08 18:54:00 CST 2018
返回目录
api
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractQueue<E>
java.util.concurrent.LinkedBlockingQueue<E>
Serializable, Iterable<E>, Collection<E>, BlockingQueue<E>, Queue<E>
返回目录
api
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractQueue<E>
java.util.concurrent.LinkedBlockingDeque<E>
Serializable, Iterable<E>, Collection<E>,
BlockingDeque<E>, BlockingQueue<E>, Deque<E>, Queue<E>
返回目录
api
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractQueue<E>
java.util.concurrent.LinkedTransferQueue<E>
Serializable, Iterable<E>, Collection<E>,
BlockingQueue<E>, TransferQueue<E>, Queue<E>
LinkedTransferQueue()
//Creates an initially empty LinkedTransferQueue.
LinkedTransferQueue(Collection<? extends E> c)
//Creates a LinkedTransferQueue initially containing the elements
// of the given collection, added in traversal order of the
// collection's iterator.
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractQueue<E>
java.util.PriorityQueue<E>
Serializable, Iterable<E>, Collection<E>, Queue<E>
PriorityQueue()
//Creates a PriorityQueue with the default initial capacity (11)
//that orders its elements according to their natural ordering.
PriorityQueue(Collection<? extends E> c)
//Creates a PriorityQueue containing the elements in the
//specified collection.
PriorityQueue(Comparator<? super E> comparator)
//Creates a PriorityQueue with the default initial capacity
// and whose elements are ordered according to the specified comparator.
PriorityQueue(int initialCapacity)
//Creates a PriorityQueue with the specified initial capacity that orders
// its elements according to their natural ordering.
PriorityQueue(int initialCapacity, Comparator<? super E> comparator)
//Creates a PriorityQueue with the specified initial capacity
//that orders its elements according to the specified comparator.
PriorityQueue(PriorityQueue<? extends E> c)
//Creates a PriorityQueue containing the elements
// in the specified priority queue.
PriorityQueue(SortedSet<? extends E> c)
//Creates a PriorityQueue containing the elements
//in the specified sorted set.
API
返回目录
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractQueue<E>
java.util.concurrent.PriorityBlockingQueue<E>
Serializable, Iterable<E>, Collection<E>, BlockingQueue<E>, Queue<E>
PriorityBlockingQueue()
Creates a PriorityBlockingQueue with the default initial capacity (11) that orders its elements according to their natural ordering.
PriorityBlockingQueue(Collection<? extends E> c)
Creates a PriorityBlockingQueue containing the elements in the specified collection.
PriorityBlockingQueue(int initialCapacity)
Creates a PriorityBlockingQueue with the specified initial capacity that orders its elements according to their natural ordering.
PriorityBlockingQueue(int initialCapacity, Comparator<? super E> comparator)
Creates a PriorityBlockingQueue with the specified initial capacity that orders its elements according to the specified comparator.
返回目录
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractQueue<E>
java.util.concurrent.SynchronousQueue<E>
Serializable, Iterable<E>, Collection<E>, BlockingQueue<E>, Queue<E>
SynchronousQueue()
Creates a SynchronousQueue with nonfair access policy.
SynchronousQueue(boolean fair)
Creates a SynchronousQueue with the specified fairness policy.