Methods Summary |
---|
public boolean | add(E o)Adds the specified element to this queue.
return offer(o);
|
public void | clear()Atomically removes all of the elements from this delay queue.
The queue will be empty after this call returns.
final ReentrantLock lock = this.lock;
lock.lock();
try {
q.clear();
} finally {
lock.unlock();
}
|
public int | drainTo(java.util.Collection c)
if (c == null)
throw new NullPointerException();
if (c == this)
throw new IllegalArgumentException();
final ReentrantLock lock = this.lock;
lock.lock();
try {
int n = 0;
for (;;) {
E first = q.peek();
if (first == null || first.getDelay(TimeUnit.NANOSECONDS) > 0)
break;
c.add(q.poll());
++n;
}
if (n > 0)
available.signalAll();
return n;
} finally {
lock.unlock();
}
|
public int | drainTo(java.util.Collection c, int maxElements)
if (c == null)
throw new NullPointerException();
if (c == this)
throw new IllegalArgumentException();
if (maxElements <= 0)
return 0;
final ReentrantLock lock = this.lock;
lock.lock();
try {
int n = 0;
while (n < maxElements) {
E first = q.peek();
if (first == null || first.getDelay(TimeUnit.NANOSECONDS) > 0)
break;
c.add(q.poll());
++n;
}
if (n > 0)
available.signalAll();
return n;
} finally {
lock.unlock();
}
|
public java.util.Iterator | iterator()Returns an iterator over the elements in this queue. The iterator
does not return the elements in any particular order. The
returned iterator is a thread-safe "fast-fail" iterator that will
throw {@link java.util.ConcurrentModificationException}
upon detected interference.
final ReentrantLock lock = this.lock;
lock.lock();
try {
return new Itr(q.iterator());
} finally {
lock.unlock();
}
|
public boolean | offer(E o)Inserts the specified element into this delay queue.
final ReentrantLock lock = this.lock;
lock.lock();
try {
E first = q.peek();
q.offer(o);
if (first == null || o.compareTo(first) < 0)
available.signalAll();
return true;
} finally {
lock.unlock();
}
|
public boolean | offer(E o, long timeout, java.util.concurrent.TimeUnit unit)Inserts the specified element into this delay queue. As the queue is
unbounded this method will never block.
return offer(o);
|
public E | peek()Retrieves, but does not remove, the head of this queue,
returning null if this queue has no elements with an
unexpired delay.
final ReentrantLock lock = this.lock;
lock.lock();
try {
return q.peek();
} finally {
lock.unlock();
}
|
public E | poll(long timeout, java.util.concurrent.TimeUnit unit)Retrieves and removes the head of this queue, waiting
if necessary up to the specified wait time if no elements with
an unexpired delay are
present on this queue.
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
long nanos = unit.toNanos(timeout);
try {
for (;;) {
E first = q.peek();
if (first == null) {
if (nanos <= 0)
return null;
else
nanos = available.awaitNanos(nanos);
} else {
long delay = first.getDelay(TimeUnit.NANOSECONDS);
if (delay > 0) {
if (delay > nanos)
delay = nanos;
long timeLeft = available.awaitNanos(delay);
nanos -= delay - timeLeft;
} else {
E x = q.poll();
assert x != null;
if (q.size() != 0)
available.signalAll();
return x;
}
}
}
} finally {
lock.unlock();
}
|
public E | poll()Retrieves and removes the head of this queue, or null
if this queue has no elements with an unexpired delay.
final ReentrantLock lock = this.lock;
lock.lock();
try {
E first = q.peek();
if (first == null || first.getDelay(TimeUnit.NANOSECONDS) > 0)
return null;
else {
E x = q.poll();
assert x != null;
if (q.size() != 0)
available.signalAll();
return x;
}
} finally {
lock.unlock();
}
|
public void | put(E o)Adds the specified element to this delay queue. As the queue is
unbounded this method will never block.
offer(o);
|
public int | remainingCapacity()Always returns Integer.MAX_VALUE because
a DelayQueue is not capacity constrained.
return Integer.MAX_VALUE;
|
public boolean | remove(java.lang.Object o)Removes a single instance of the specified element from this
queue, if it is present.
final ReentrantLock lock = this.lock;
lock.lock();
try {
return q.remove(o);
} finally {
lock.unlock();
}
|
public int | size()
final ReentrantLock lock = this.lock;
lock.lock();
try {
return q.size();
} finally {
lock.unlock();
}
|
public E | take()Retrieves and removes the head of this queue, waiting
if no elements with an unexpired delay are present on this queue.
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
for (;;) {
E first = q.peek();
if (first == null) {
available.await();
} else {
long delay = first.getDelay(TimeUnit.NANOSECONDS);
if (delay > 0) {
long tl = available.awaitNanos(delay);
} else {
E x = q.poll();
assert x != null;
if (q.size() != 0)
available.signalAll(); // wake up other takers
return x;
}
}
}
} finally {
lock.unlock();
}
|
public java.lang.Object[] | toArray()
final ReentrantLock lock = this.lock;
lock.lock();
try {
return q.toArray();
} finally {
lock.unlock();
}
|
public T[] | toArray(T[] array)
final ReentrantLock lock = this.lock;
lock.lock();
try {
return q.toArray(array);
} finally {
lock.unlock();
}
|