@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)); }
/** * @param buf Byte buffer to unmarshal. * @return Unmarshalled object. * @throws IgniteCheckedException Thrown if any exception occurs while unmarshalling. */ @SuppressWarnings({"RedundantTypeArguments"}) protected static <T> T unmarshal(final byte[] buf) throws IgniteCheckedException { RunnableFuture<T> f = new FutureTask<>(new Callable<T>() { @Override public T call() throws IgniteCheckedException { return marsh.<T>unmarshal(buf, Thread.currentThread().getContextClassLoader()); } }); // Any deserialization has to be executed under a thread, that contains the Ignite instance name. new IgniteThread(igniteInstanceName, "unmarshal-thread", f).start(); try { return f.get(); } catch (Exception e) { if (e.getCause() instanceof IgniteCheckedException) throw (IgniteCheckedException)e.getCause(); fail(e.getCause().getMessage()); } return null; }
src.run();
@Override public void shutdownExecutor() { List<Runnable> remainingTasks = concurrentExecutor.shutdownNow(); for (Runnable task : remainingTasks) { if (task instanceof RunnableFuture) { ((RunnableFuture<?>) task).cancel(true); } } }
@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 ); }
@Override public boolean isDone() { return cancelDelegate.isDone(); } }
@Override public boolean isCancelled() { return cancelDelegate.isCancelled(); }
value = future.get(); gotValue = true;
@Override public void run() { assertTrue(hasStarted == false); hasStarted = true; future.run(); }
@Override public boolean isDone() { return task.isDone(); }
@Override public boolean isCancelled() { return result.isCancelled(); }
/** * Perform the processing of the given {@link RunnableFuture}. * Can be overridden to add extra logging, timing, etc. * * @param input to process. */ protected void processInput(RunnableFuture<?> input) { try { input.run(); input.get(); } catch (Exception e) { LOG.warn("Execution threw an exception.", e); } } }