Refine search
@Override public long getDelay(TimeUnit unit) { return future.getDelay(unit); }
@Override public boolean isPeriodic() { return future.isPeriodic(); }
@Override public int compareTo(Delayed o) { return future.compareTo(o); } }
boolean isAllTasksCancelledOrDone() { for(RunnableScheduledFuture<?> task: tasks) { if (!task.isCancelled() && !task.isDone()) { return false; } } return true; }
/** * Requeues a periodic task unless current run state precludes it. * Same idea as delayedExecute except drops task rather than rejecting. * * @param task the task */ void reExecutePeriodic(RunnableScheduledFuture<?> task) { if (canRunInCurrentRunState(true)) { super.getQueue().add(task); if (!canRunInCurrentRunState(true) && remove(task)) task.cancel(false); else ensurePrestart(); } }
/** * Main execution method for delayed or periodic tasks. If pool * is shut down, rejects the task. Otherwise adds task to queue * and starts a thread, if necessary, to run it. (We cannot * prestart the thread to run the task because the task (probably) * shouldn't be run yet.) If the pool is shut down while the task * is being added, cancel and remove it if required by state and * run-after-shutdown parameters. * * @param task the task */ private void delayedExecute(RunnableScheduledFuture<?> task) { if (isShutdown()) reject(task); else { super.getQueue().add(task); if (isShutdown() && !canRunInCurrentRunState(task.isPeriodic()) && remove(task)) task.cancel(false); else ensurePrestart(); } }
getContinueExistingPeriodicTasksAfterShutdownPolicy(); if (!keepDelayed && !keepPeriodic) { for (Object e : q.toArray()) if (e instanceof RunnableScheduledFuture<?>) ((RunnableScheduledFuture<?>) e).cancel(false); q.clear(); for (Object e : q.toArray()) { if (e instanceof RunnableScheduledFuture) { RunnableScheduledFuture<?> t = (RunnableScheduledFuture<?>)e; if ((t.isPeriodic() ? !keepPeriodic : !keepDelayed) || t.isCancelled()) { // also remove if already cancelled if (q.remove(t)) t.cancel(false);
@Test public void test() throws InterruptedException, ExecutionException, TimeoutException { CancelableRunnable cancelableRunnable = Mockito.mock(CancelableRunnable.class); RunnableScheduledFuture futureTask = Mockito.mock(RunnableScheduledFuture.class); CancelableScheduledFuture<Object> future = new CancelableScheduledFuture<Object>(cancelableRunnable, futureTask); future.cancel(false); Mockito.verify(futureTask, Mockito.times(1)).cancel(false); future.run(); Mockito.verify(futureTask, Mockito.times(1)).run(); future.cancel(true); Mockito.verify(cancelableRunnable, Mockito.times(1)).cancel(); Mockito.verify(futureTask, Mockito.times(1)).cancel(true); future.isDone(); Mockito.verify(futureTask, Mockito.times(1)).isDone(); future.isPeriodic(); Mockito.verify(futureTask, Mockito.times(1)).isPeriodic(); future.get(); Mockito.verify(futureTask, Mockito.times(1)).get(); future.get(100L, TimeUnit.MICROSECONDS); Mockito.verify(futureTask, Mockito.times(1)).get(100L, TimeUnit.MICROSECONDS); future.getDelay(TimeUnit.HOURS); Mockito.verify(futureTask, Mockito.times(1)).getDelay(TimeUnit.HOURS); CancelableScheduledFuture<Object> future2 = new CancelableScheduledFuture<Object>(cancelableRunnable, futureTask); assertTrue(future.equals(future)); assertTrue(future.equals(future2)); assertEquals(future.hashCode(), future.hashCode()); assertEquals(future.hashCode(), future2.hashCode()); }
@Override public void run() { wrapped.run(); }
@Override public boolean isCancelled() { return wrapped.isCancelled(); }
@Override public void canceled() { if (ProcessScheduledFuture.this.task.isCancelled()) { return; } ProcessScheduledFuture.this.task.cancel(true); } };
@Override public boolean cancel(boolean mayInterruptIfRunning) { return wrapped.cancel(mayInterruptIfRunning); }
@Override public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { return wrapped.get(timeout, unit); } }
@Override public boolean isDone() { return wrapped.isDone(); }
/** * Requeues a periodic task unless current run state precludes it. * Same idea as delayedExecute except drops task rather than rejecting. * * @param task the task */ void reExecutePeriodic(RunnableScheduledFuture<?> task) { if (canRunInCurrentRunState(true)) { super.getQueue().add(task); if (!canRunInCurrentRunState(true) && remove(task)) task.cancel(false); else ensurePrestart(); } }
@Override public void run() { futureTask.run(); }
@Override public boolean isCancelled() { return delegate.isCancelled(); }
@Override public void canceled() { if (ProcessScheduledFuture.this.task.isCancelled()) { return; } ProcessScheduledFuture.this.task.cancel(true); } };
@Override public boolean cancel(boolean interrupt) { Future<?> currentExecution = execution; return ((currentExecution == null || currentExecution.cancel(interrupt)) && delegate.cancel(interrupt)); }
@Override public V get() throws InterruptedException, ExecutionException { return wrapped.get(); }