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

The following examples show how to use java.util.concurrent.DelayQueue#put() . 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: Scheduler.java    From spectator with Apache License 2.0 6 votes vote down vote up
/**
 * Execute the task and if reschedule another execution.
 *
 * @param queue
 *     Queue for the pool. This task will be added to the queue to schedule
 *     future executions.
 * @param stats
 *     Handle to stats that should be updated based on the execution of the
 *     task.
 */
@SuppressWarnings("PMD.AvoidCatchingThrowable")
void runAndReschedule(DelayQueue<DelayedTask> queue, Stats stats) {
  thread = Thread.currentThread();
  boolean scheduleAgain = options.schedulingPolicy != Policy.RUN_ONCE;
  try {
    if (!isDone()) {
      task.run();
    }
  } catch (Throwable t) {
    // This catches Throwable because we cannot control the task and thus cannot
    // ensure it is well behaved with respect to exceptions.
    LOGGER.warn("task execution failed", t);
    stats.incrementUncaught(t);
    scheduleAgain = !options.stopOnFailure;
  } finally {
    thread = null;
    if (scheduleAgain && !isDone()) {
      updateNextExecutionTime(stats.skipped());
      queue.put(this);
    } else {
      cancelled = true;
    }
  }
}
 
Example 2
Source File: DelayQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * all elements successfully put are contained
 */
public void testPut() {
    DelayQueue q = new DelayQueue();
    for (int i = 0; i < SIZE; ++i) {
        PDelay x = new PDelay(i);
        q.put(x);
        assertTrue(q.contains(x));
    }
    assertEquals(SIZE, q.size());
}
 
Example 3
Source File: DelayQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * drainTo empties queue
 */
public void testDrainToWithActivePut() throws InterruptedException {
    final DelayQueue q = populatedQueue(SIZE);
    Thread t = new Thread(new CheckedRunnable() {
        public void realRun() {
            q.put(new PDelay(SIZE + 1));
        }});

    t.start();
    ArrayList l = new ArrayList();
    q.drainTo(l);
    assertTrue(l.size() >= SIZE);
    t.join();
    assertTrue(q.size() + l.size() >= SIZE);
}
 
Example 4
Source File: DelayQueueTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * all elements successfully put are contained
 */
public void testPut() {
    DelayQueue q = new DelayQueue();
    for (int i = 0; i < SIZE; ++i) {
        PDelay x = new PDelay(i);
        q.put(x);
        assertTrue(q.contains(x));
    }
    assertEquals(SIZE, q.size());
}
 
Example 5
Source File: DelayQueueTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * drainTo empties queue
 */
public void testDrainToWithActivePut() throws InterruptedException {
    final DelayQueue q = populatedQueue(SIZE);
    Thread t = new Thread(new CheckedRunnable() {
        public void realRun() {
            q.put(new PDelay(SIZE + 1));
        }});

    t.start();
    ArrayList l = new ArrayList();
    q.drainTo(l);
    assertTrue(l.size() >= SIZE);
    t.join();
    assertTrue(q.size() + l.size() >= SIZE);
}