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

The following examples show how to use java.util.concurrent.DelayQueue#add() . 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: 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 2
Source File: Stress.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Throwable {

        final DelayQueue<Delayed> q = new DelayQueue<>();
        final long t0 = System.nanoTime();
        for (long i = 0; i < 1000; i++) {
            final long expiry = t0 + i*10L*1000L*1000L;
            q.add(new Delayed() {
                    public long getDelay(TimeUnit unit) {
                        return unit.convert(expiry - System.nanoTime(),
                                            NANOSECONDS);
                    }
                    public int compareTo(Delayed x) {
                        long d = getDelay(NANOSECONDS)
                            - x.getDelay(NANOSECONDS);
                        return d < 0 ? -1 : d > 0 ? 1 : 0; }});
        }

        for (int i = 0; i < 300; i++)
            new Thread() { public void run() {
                try {
                    while (!q.isEmpty())
                        q.poll(10L, TimeUnit.SECONDS);
                } catch (Throwable t) { t.printStackTrace(); }
            }}.start();
    }
 
Example 3
Source File: DelayQueueTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * Delayed actions do not occur until their delay elapses
 */
public void testDelay() throws InterruptedException {
    DelayQueue<NanoDelay> q = new DelayQueue<NanoDelay>();
    for (int i = 0; i < SIZE; ++i)
        q.add(new NanoDelay(1000000L * (SIZE - i)));

    long last = 0;
    for (int i = 0; i < SIZE; ++i) {
        NanoDelay e = q.take();
        long tt = e.getTriggerTime();
        assertTrue(System.nanoTime() - tt >= 0);
        if (i != 0)
            assertTrue(tt >= last);
        last = tt;
    }
    assertTrue(q.isEmpty());
}
 
Example 4
Source File: DelayQueueTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * drainTo(c) empties queue into another collection c
 */
public void testDrainTo() {
    DelayQueue q = new DelayQueue();
    PDelay[] elems = new PDelay[SIZE];
    for (int i = 0; i < SIZE; ++i) {
        elems[i] = new PDelay(i);
        q.add(elems[i]);
    }
    ArrayList l = new ArrayList();
    q.drainTo(l);
    assertEquals(0, q.size());
    for (int i = 0; i < SIZE; ++i)
        assertEquals(elems[i], l.get(i));
    q.add(elems[0]);
    q.add(elems[1]);
    assertFalse(q.isEmpty());
    assertTrue(q.contains(elems[0]));
    assertTrue(q.contains(elems[1]));
    l.clear();
    q.drainTo(l);
    assertEquals(0, q.size());
    assertEquals(2, l.size());
    for (int i = 0; i < 2; ++i)
        assertEquals(elems[i], l.get(i));
}
 
Example 5
Source File: DelayQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Delayed actions do not occur until their delay elapses
 */
public void testDelay() throws InterruptedException {
    DelayQueue<NanoDelay> q = new DelayQueue<>();
    for (int i = 0; i < SIZE; ++i)
        q.add(new NanoDelay(1000000L * (SIZE - i)));

    long last = 0;
    for (int i = 0; i < SIZE; ++i) {
        NanoDelay e = q.take();
        long tt = e.getTriggerTime();
        assertTrue(System.nanoTime() - tt >= 0);
        if (i != 0)
            assertTrue(tt >= last);
        last = tt;
    }
    assertTrue(q.isEmpty());
}
 
Example 6
Source File: DelayQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * drainTo(c) empties queue into another collection c
 */
public void testDrainTo() {
    DelayQueue q = new DelayQueue();
    PDelay[] elems = new PDelay[SIZE];
    for (int i = 0; i < SIZE; ++i) {
        elems[i] = new PDelay(i);
        q.add(elems[i]);
    }
    ArrayList l = new ArrayList();
    q.drainTo(l);
    assertEquals(0, q.size());
    for (int i = 0; i < SIZE; ++i)
        assertEquals(elems[i], l.get(i));
    q.add(elems[0]);
    q.add(elems[1]);
    assertFalse(q.isEmpty());
    assertTrue(q.contains(elems[0]));
    assertTrue(q.contains(elems[1]));
    l.clear();
    q.drainTo(l);
    assertEquals(0, q.size());
    assertEquals(2, l.size());
    for (int i = 0; i < 2; ++i)
        assertEquals(elems[i], l.get(i));
}
 
Example 7
Source File: DelayQueueTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * iterator.remove removes current element
 */
public void testIteratorRemove() {
    final DelayQueue q = new DelayQueue();
    q.add(new PDelay(2));
    q.add(new PDelay(1));
    q.add(new PDelay(3));
    Iterator it = q.iterator();
    it.next();
    it.remove();
    it = q.iterator();
    assertEquals(new PDelay(2), it.next());
    assertEquals(new PDelay(3), it.next());
    assertFalse(it.hasNext());
}
 
Example 8
Source File: TxGroupRequestor.java    From nuls-v2 with MIT License 5 votes vote down vote up
public static void addTask(int chainId, String hash, TxGroupTask task) {
    NulsLogger logger = ContextManager.getContext(chainId).getLogger();
    DelayQueue<TxGroupTask> txGroupTasks = map.get(chainId).get(hash);
    if (txGroupTasks == null) {
        txGroupTasks = new DelayQueue<>();
        map.get(chainId).put(hash, txGroupTasks);
    }
    boolean add = txGroupTasks.add(task);
    logger.debug("TxGroupRequestor add TxGroupTask, hash-" + hash + ", task-" + task + ", result-" + add);
}
 
Example 9
Source File: DelayQueueTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * containsAll(c) is true when c contains a subset of elements
 */
public void testContainsAll() {
    DelayQueue q = populatedQueue(SIZE);
    DelayQueue p = new DelayQueue();
    for (int i = 0; i < SIZE; ++i) {
        assertTrue(q.containsAll(p));
        assertFalse(p.containsAll(q));
        p.add(new PDelay(i));
    }
    assertTrue(p.containsAll(q));
}
 
Example 10
Source File: DelayQueueTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * clear removes all elements
 */
public void testClear() {
    DelayQueue q = populatedQueue(SIZE);
    q.clear();
    assertTrue(q.isEmpty());
    assertEquals(0, q.size());
    assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
    PDelay x = new PDelay(1);
    q.add(x);
    assertFalse(q.isEmpty());
    assertTrue(q.contains(x));
    q.clear();
    assertTrue(q.isEmpty());
}
 
Example 11
Source File: DelayQueueTest.java    From jdk-source-analysis with Apache License 2.0 5 votes vote down vote up
@Test
public void test() throws InterruptedException {
    DelayQueue<IntDelay> delayQueue = new DelayQueue<>();
    for (int i = 0; i < 10; i++) {
        delayQueue.add(new IntDelay(i));
    }
    while (!delayQueue.isEmpty()) {
        IntDelay delay = delayQueue.take();
        if (Objects.nonNull(delay)) {
            System.out.println(delay.num);
        }
    }
}
 
Example 12
Source File: DelayQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * containsAll(c) is true when c contains a subset of elements
 */
public void testContainsAll() {
    DelayQueue q = populatedQueue(SIZE);
    DelayQueue p = new DelayQueue();
    for (int i = 0; i < SIZE; ++i) {
        assertTrue(q.containsAll(p));
        assertFalse(p.containsAll(q));
        p.add(new PDelay(i));
    }
    assertTrue(p.containsAll(q));
}
 
Example 13
Source File: DelayQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * clear removes all elements
 */
public void testClear() {
    DelayQueue q = populatedQueue(SIZE);
    q.clear();
    assertTrue(q.isEmpty());
    assertEquals(0, q.size());
    assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
    PDelay x = new PDelay(1);
    q.add(x);
    assertFalse(q.isEmpty());
    assertTrue(q.contains(x));
    q.clear();
    assertTrue(q.isEmpty());
}
 
Example 14
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 15
Source File: PollUnexpired.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static void realMain(String[] args) throws Throwable {
    DelayQueue<Godot> q = new DelayQueue<>();
    for (int i = 0; i < 3; i++) {
        equal(q.size(), i);
        equal(q.poll(), null);
        equal(q.size(), i);
        equal(q.poll(100, TimeUnit.MILLISECONDS), null);
        equal(q.size(), i);
        q.add(new Godot());
    }
}
 
Example 16
Source File: DelayQueueTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * peek of a non-empty queue returns non-null even if not expired
 */
public void testPeekDelayed() {
    DelayQueue q = new DelayQueue();
    q.add(new NanoDelay(Long.MAX_VALUE));
    assertNotNull(q.peek());
}
 
Example 17
Source File: DelayQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * poll of a non-empty queue returns null if no expired elements.
 */
public void testPollDelayed() {
    DelayQueue q = new DelayQueue();
    q.add(new NanoDelay(Long.MAX_VALUE));
    assertNull(q.poll());
}
 
Example 18
Source File: DelayQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * peek of a non-empty queue returns non-null even if not expired
 */
public void testPeekDelayed() {
    DelayQueue q = new DelayQueue();
    q.add(new NanoDelay(Long.MAX_VALUE));
    assertNotNull(q.peek());
}
 
Example 19
Source File: DelayQueueTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * timed poll of a non-empty queue returns null if no expired elements.
 */
public void testTimedPollDelayed() throws InterruptedException {
    DelayQueue q = new DelayQueue();
    q.add(new NanoDelay(LONG_DELAY_MS * 1000000L));
    assertNull(q.poll(timeoutMillis(), MILLISECONDS));
}
 
Example 20
Source File: DelayQueueTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * poll of a non-empty queue returns null if no expired elements.
 */
public void testPollDelayed() {
    DelayQueue q = new DelayQueue();
    q.add(new NanoDelay(Long.MAX_VALUE));
    assertNull(q.poll());
}