下面列出了java.util.concurrent.DelayQueue#put ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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;
}
}
}
/**
* 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());
}
/**
* 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);
}
/**
* 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());
}
/**
* 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);
}