下面列出了java.util.concurrent.DelayQueue#remove ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Decrement reference count for the given session's UGI. Resets the time at which the UGI will
* expire to UGI_CACHE_EXPIRY milliseconds in the future.
*
* @param session the session for which we want to release the UGI.
* @param cleanImmediatelyIfNoRefs if true, destroys the UGI for the given session (only if it
* is now unreferenced).
*/
@SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter")
public void release(SessionId session, boolean cleanImmediatelyIfNoRefs) {
Entry entry = cache.get(session);
if (entry == null) {
throw new IllegalStateException("Cannot release UGI for this session; it is not cached: " + session);
}
DelayQueue<Entry> expirationQueue = getExpirationQueue(session.getSegmentId());
synchronized (expirationQueue) {
entry.decrementRefCount();
expirationQueue.remove(entry);
if (cleanImmediatelyIfNoRefs && entry.isNotInUse()) {
closeUGI(entry);
} else {
// Reset expiration time and put it back in the queue
// only when we don't close the UGI
entry.resetTime();
expirationQueue.offer(entry);
}
}
}
private void insert(
DelayQueue<WaitForEntry<T>> q, Map<T, WaitForEntry<T>> knownEntries, WaitForEntry entry) {
WaitForEntry existing = knownEntries.get((T) entry.data);
if (existing != null) {
if (Duration.between(existing.readyAtMillis, entry.readyAtMillis).isNegative()) {
q.remove(existing);
existing.readyAtMillis = entry.readyAtMillis;
q.add(existing);
}
return;
}
q.offer(entry);
knownEntries.put((T) entry.data, entry);
}
/**
* retainAll(c) retains only those elements of c and reports true if changed
*/
public void testRetainAll() {
DelayQueue q = populatedQueue(SIZE);
DelayQueue p = populatedQueue(SIZE);
for (int i = 0; i < SIZE; ++i) {
boolean changed = q.retainAll(p);
if (i == 0)
assertFalse(changed);
else
assertTrue(changed);
assertTrue(q.containsAll(p));
assertEquals(SIZE - i, q.size());
p.remove();
}
}
/**
* retainAll(c) retains only those elements of c and reports true if changed
*/
public void testRetainAll() {
DelayQueue q = populatedQueue(SIZE);
DelayQueue p = populatedQueue(SIZE);
for (int i = 0; i < SIZE; ++i) {
boolean changed = q.retainAll(p);
if (i == 0)
assertFalse(changed);
else
assertTrue(changed);
assertTrue(q.containsAll(p));
assertEquals(SIZE - i, q.size());
p.remove();
}
}
/**
* isEmpty is true before add, false after
*/
public void testEmpty() {
DelayQueue q = new DelayQueue();
assertTrue(q.isEmpty());
assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
q.add(new PDelay(1));
assertFalse(q.isEmpty());
q.add(new PDelay(2));
q.remove();
q.remove();
assertTrue(q.isEmpty());
}
/**
* remove removes next element, or throws NSEE if empty
*/
public void testRemove() {
DelayQueue q = populatedQueue(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(new PDelay(i), q.remove());
}
try {
q.remove();
shouldThrow();
} catch (NoSuchElementException success) {}
}
/**
* removeAll(c) removes only those elements of c and reports true if changed
*/
public void testRemoveAll() {
for (int i = 1; i < SIZE; ++i) {
DelayQueue q = populatedQueue(SIZE);
DelayQueue p = populatedQueue(i);
assertTrue(q.removeAll(p));
assertEquals(SIZE - i, q.size());
for (int j = 0; j < i; ++j) {
PDelay x = (PDelay)(p.remove());
assertFalse(q.contains(x));
}
}
}
/**
* isEmpty is true before add, false after
*/
public void testEmpty() {
DelayQueue q = new DelayQueue();
assertTrue(q.isEmpty());
assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
q.add(new PDelay(1));
assertFalse(q.isEmpty());
q.add(new PDelay(2));
q.remove();
q.remove();
assertTrue(q.isEmpty());
}
/**
* remove removes next element, or throws NSEE if empty
*/
public void testRemove() {
DelayQueue q = populatedQueue(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(new PDelay(i), q.remove());
}
try {
q.remove();
shouldThrow();
} catch (NoSuchElementException success) {}
}
/**
* removeAll(c) removes only those elements of c and reports true if changed
*/
public void testRemoveAll() {
for (int i = 1; i < SIZE; ++i) {
DelayQueue q = populatedQueue(SIZE);
DelayQueue p = populatedQueue(i);
assertTrue(q.removeAll(p));
assertEquals(SIZE - i, q.size());
for (int j = 0; j < i; ++j) {
PDelay x = (PDelay)(p.remove());
assertFalse(q.contains(x));
}
}
}
/**
* Replace the given activity in the given queue. If not in the queue, adds it to the queue. The activity
* runs after the specified delay (the delay of the previous entry, if any, is ignored)
*
* @param group the queue - all activities within a queue are executed serially
* @param activity the activity
* @param delay the delay
* @param unit the delay unit
*/
public synchronized void replace(QueueGroups group, Activity activity, long delay, TimeUnit unit)
{
ActivityHolder holder = new ActivityHolder(activity, TimeUnit.MILLISECONDS.convert(delay, unit));
DelayQueue<ActivityHolder> queue = queues.get(group);
queue.remove(holder);
queue.offer(holder);
}