コード例 #1
0
  /** @throws Exception If failed. */
  public void testPoll() throws Exception {
    ConcurrentLinkedDeque8<Integer> deque = new ConcurrentLinkedDeque8<>();

    deque.add(1);
    deque.add(2);
    deque.add(3);

    assert !deque.isEmpty();

    checkSize(deque, 3);

    // Poll 1.
    assertEquals(Integer.valueOf(1), deque.poll());

    assert !deque.isEmpty();

    checkSize(deque, 2);

    // Poll 2.
    assertEquals(Integer.valueOf(2), deque.poll());

    assert !deque.isEmpty();

    checkSize(deque, 1);

    // Poll 3.
    assertEquals(Integer.valueOf(3), deque.poll());

    assert deque.isEmpty();

    checkSize(deque, 0);
  }
コード例 #2
0
  /**
   * Method cleans up all events that either outnumber queue size or exceeds time-to-live value. It
   * does none if someone else cleans up queue (lock is locked) or if there are queue readers
   * (readersNum > 0).
   */
  private void cleanupQueue() {
    long now = U.currentTimeMillis();

    long queueOversize = evts.sizex() - expireCnt;

    for (int i = 0; i < queueOversize && evts.sizex() > expireCnt; i++) {
      GridEvent expired = evts.poll();

      if (log.isDebugEnabled()) log.debug("Event expired by count: " + expired);
    }

    while (true) {
      ConcurrentLinkedDeque8.Node<GridEvent> node = evts.peekx();

      if (node == null) // Queue is empty.
      break;

      GridEvent evt = node.item();

      if (evt == null) // Competing with another thread.
      continue;

      if (now - evt.timestamp() < expireAgeMs) break;

      if (evts.unlinkx(node) && log.isDebugEnabled())
        log.debug("Event expired by age: " + node.item());
    }
  }
コード例 #3
0
  public void testEmptyDeque() {
    ConcurrentLinkedDeque8<Integer> deque = new ConcurrentLinkedDeque8<>();

    assert deque.poll() == null;
    assert deque.pollFirst() == null;
    assert deque.pollLast() == null;

    assert deque.peek() == null;
    assert deque.peekFirst() == null;
    assert deque.peekLast() == null;

    checkSize(deque, 0);
  }