Java Code Examples for java.util.concurrent.DelayQueue#remove()

The following examples show how to use java.util.concurrent.DelayQueue#remove() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UGICache.java    From pxf with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
        }
    }
}
 
Example 2
Source File: DefaultDelayingQueue.java    From java with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 3
Source File: DelayQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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();
    }
}
 
Example 4
Source File: DelayQueueTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
    }
}
 
Example 5
Source File: DelayQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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());
}
 
Example 6
Source File: DelayQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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) {}
}
 
Example 7
Source File: DelayQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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));
        }
    }
}
 
Example 8
Source File: DelayQueueTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * 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());
}
 
Example 9
Source File: DelayQueueTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * 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) {}
}
 
Example 10
Source File: DelayQueueTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * 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));
        }
    }
}
 
Example 11
Source File: ActivityQueue.java    From exhibitor with Apache License 2.0 3 votes vote down vote up
/**
 * 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);
}