@Override public void shutdownExecutor() { List<Runnable> remainingTasks = concurrentExecutor.shutdownNow(); for (Runnable task : remainingTasks) { if (task instanceof RunnableFuture) { ((RunnableFuture<?>) task).cancel(true); } } }
@Test public void testCancelButRunToCompletion() throws Exception { final CountDownLatch started = new CountDownLatch(1); final CountDownLatch hang = new CountDownLatch(1); RunnableFuture<Integer> interruptible = new InterruptibleFuture<Integer>() { @Override public Integer call() { try { started.countDown(); hang.await(); return 0; } catch (InterruptedException e) { return 1; } } }; executor.execute(interruptible); started.await(); interruptible.cancel(true); assertThat(interruptible.get(), is(1)); }
@Test public void testCancelTrueBeforeStart() throws Exception { RunnableFuture<Integer> interruptible = getInterruptible(); interruptible.cancel(true); executor.execute(interruptible); expectedException.expect(CancellationException.class); interruptible.get(); }
@Test public void testCompleteAndThenCancel() throws Exception { RunnableFuture<Integer> interruptible = new InterruptibleFuture<Integer>() { @Override public Integer call() throws InterruptedException { if (Thread.interrupted()) { throw new InterruptedException(); } return 1; } }; executor.execute(interruptible); assertThat(interruptible.get(), is(1)); interruptible.cancel(true); assertThat(interruptible.get(), is(1)); }
@Test public void testCancelFalseBeforeStart() throws Exception { RunnableFuture<Integer> interruptible = getInterruptible(); interruptible.cancel(false); executor.execute(interruptible); expectedException.expect(CancellationException.class); interruptible.get(); }
@Test public void testCancelAfterRunning() throws Exception { RunnableFuture<Integer> interruptible = getInterruptible(); interruptible.run(); interruptible.cancel(true); assertThat(interruptible.get(), is(1)); }
@Test public void testCancelFalseNoStart() throws Exception { RunnableFuture<Integer> interruptible = getInterruptible(); interruptible.cancel(false); expectedException.expect(CancellationException.class); interruptible.get(); }
@Test public void testCancelTrueNoStart() throws Exception { RunnableFuture<Integer> interruptible = getInterruptible(); interruptible.cancel(true); expectedException.expect(CancellationException.class); interruptible.get(); }
@Override public boolean cancel(boolean mayInterruptIfRunning) { return result.cancel(false); } @Override
@Override public boolean cancel(boolean mayInterruptIfRunning) { return cancelDelegate.cancel(mayInterruptIfRunning); }
public boolean cancel(boolean mayInterruptIfRunning) { return src.cancel(mayInterruptIfRunning); }
@Override public List<Runnable> shutdownNow() { for ( RunnableFuture<?> runnableFuture : priorityTasks ) { runnableFuture.cancel( true ); } priorityTasks.clear(); return super.shutdownNow(); }
@Override public List<Runnable> shutdownNow() { if (!shutdown.compareAndSet(false, true)) { return Collections.emptyList(); } List<Runnable> tasks = new LinkedList<Runnable>(); taskQ.drainTo(tasks); for (Runnable task : tasks) { if (task instanceof RunnableFuture) { ((RunnableFuture) task).cancel(false); } } return tasks; }
public int abortTasks() { AtomicInteger count = new AtomicInteger(); tasks.forEach((key, value) -> { key.cancel(true); count.incrementAndGet(); }); tasks.clear(); return count.get(); } }
@Override public List<Runnable> shutdownNow() { if (!shutdown.compareAndSet(false, true)) { return Collections.emptyList(); } List<Runnable> tasks = new LinkedList<Runnable>(); taskQ.drainTo(tasks); for (Runnable task : tasks) { if (task instanceof RunnableFuture) { ((RunnableFuture) task).cancel(false); } } return tasks; }
@Override public boolean cancel(boolean mayInterruptIfRunning) { if (task.cancel(mayInterruptIfRunning)) { onError(new CancellationException()); cancelOrigins(mayInterruptIfRunning); return true; } else { return false; } }
@Override public synchronized void stop() { for (Runnable runnable : executor.getQueue()) { ((RunnableFuture<?>) runnable).cancel(false); } executor.purge(); }
@Override protected void beforeExecute(Thread thread, Runnable lowPriorityTask) { // Run all high priority tasks in queue first, then low priority for ( RunnableFuture<?> priorityTask; ( priorityTask = priorityTasks.poll() ) != null; ) { if ( isShutdown() ) { priorityTask.cancel( false ); } else { priorityTask.run(); } } super.beforeExecute( thread, lowPriorityTask ); }