@Override public ListenableFuture<V> reload(final K key, final V oldValue) throws Exception { ListenableFutureTask<V> task = ListenableFutureTask.create( new Callable<V>() { @Override public V call() throws Exception { return loader.reload(key, oldValue).get(); } }); executor.execute(task); return task; }
/** * Creates a {@code ListenableFutureTask} that will upon running, execute the given {@code * Callable}. * * @param callable the callable task * @since 10.0 */ public static <V> ListenableFutureTask<V> create(Callable<V> callable) { return new ListenableFutureTask<V>(callable); }
@Override public void addListener(Runnable listener, Executor executor) { delegate.addListener(listener, executor); }
public void testListenerCalledOnException() throws Exception { throwException = true; // Start up the task and unblock the latch to finish the task. exec.execute(task); runLatch.await(); taskLatch.countDown(); try { task.get(5, TimeUnit.SECONDS); fail("Should have propagated the failure."); } catch (ExecutionException e) { assertEquals(IllegalStateException.class, e.getCause().getClass()); } assertTrue(listenerLatch.await(5, TimeUnit.SECONDS)); assertTrue(task.isDone()); assertFalse(task.isCancelled()); }
@Override public <T, M extends MutableTaskMonitor<T>> M submit(@Nonnull MonitoredCallable<T, M> task) { maybeCleanup(); checkState(!shutdown, "The executor has already been shut down"); // create a future task so we can inject a future into the TaskMonitor before starting the task ListenableFutureTask<T> futureTask = ListenableFutureTask.create(task); // set the monitor so the task can feed back it's progress final M monitor = task.getMonitor(); initMonitor(monitor, futureTask); // mark the monitor as having completed when the task completes futureTask.addListener(new Runnable() { @Override public void run() { ExpiringRef<MutableTaskMonitor<?>> ref = monitors.get(monitor.getTaskId()); if (ref != null) { ref.expireIn(expiryIntervalNanos, TimeUnit.NANOSECONDS); } } }, MoreExecutors.sameThreadExecutor()); // start the task executorService.submit(futureTask); return monitor; }
@Override public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { return delegate.get(timeout, unit); }
public void testListenerDoesNotRunUntilTaskCompletes() throws Exception { // Test default state of not started. assertEquals(1, listenerLatch.getCount()); assertFalse(task.isDone()); assertFalse(task.isCancelled()); // Start the task to put it in the RUNNING state. Have to use a separate // thread because the task will block on the task latch after unblocking // the run latch. exec.execute(task); runLatch.await(); assertEquals(1, listenerLatch.getCount()); assertFalse(task.isDone()); assertFalse(task.isCancelled()); // Finish the task by unblocking the task latch. Then wait for the // listener to be called by blocking on the listener latch. taskLatch.countDown(); assertEquals(25, task.get().intValue()); assertTrue(listenerLatch.await(5, TimeUnit.SECONDS)); assertTrue(task.isDone()); assertFalse(task.isCancelled()); }
@Override public V get() throws InterruptedException, ExecutionException { return delegate.get(); }
@Override public ListenableFuture<V> reload(final K key, final V oldValue) throws Exception { ListenableFutureTask<V> task = ListenableFutureTask.create( new Callable<V>() { @Override public V call() throws Exception { return loader.reload(key, oldValue).get(); } }); executor.execute(task); return task; }
/** * Creates a {@code ListenableFutureTask} that will upon running, execute the given {@code * Callable}. * * @param callable the callable task * @since 10.0 */ public static <V> ListenableFutureTask<V> create(Callable<V> callable) { return new ListenableFutureTask<V>(callable); }
public void testListenerCalledOnException() throws Exception { throwException = true; // Start up the task and unblock the latch to finish the task. exec.execute(task); runLatch.await(); taskLatch.countDown(); try { task.get(5, TimeUnit.SECONDS); fail("Should have propagated the failure."); } catch (ExecutionException e) { assertEquals(IllegalStateException.class, e.getCause().getClass()); } assertTrue(listenerLatch.await(5, TimeUnit.SECONDS)); assertTrue(task.isDone()); assertFalse(task.isCancelled()); }
@Override protected void setUp() throws Exception { super.setUp(); exec = Executors.newCachedThreadPool(); task.addListener( new Runnable() { @Override public void run() { listenerLatch.countDown(); } }, directExecutor()); }
@Override public V get() throws InterruptedException, ExecutionException { return delegate.get(); }
@Override public ListenableFuture<V> reload(final K key, final V oldValue) throws Exception { ListenableFutureTask<V> task = ListenableFutureTask.create( new Callable<V>() { @Override public V call() throws Exception { return loader.reload(key, oldValue).get(); } }); executor.execute(task); return task; }
/** * Creates a {@code ListenableFutureTask} that will upon running, execute the given {@code * Callable}. * * @param callable the callable task * @since 10.0 */ public static <V> ListenableFutureTask<V> create(Callable<V> callable) { return new ListenableFutureTask<V>(callable); }
public void testListenerDoesNotRunUntilTaskCompletes() throws Exception { // Test default state of not started. assertEquals(1, listenerLatch.getCount()); assertFalse(task.isDone()); assertFalse(task.isCancelled()); // Start the task to put it in the RUNNING state. Have to use a separate // thread because the task will block on the task latch after unblocking // the run latch. exec.execute(task); runLatch.await(); assertEquals(1, listenerLatch.getCount()); assertFalse(task.isDone()); assertFalse(task.isCancelled()); // Finish the task by unblocking the task latch. Then wait for the // listener to be called by blocking on the listener latch. taskLatch.countDown(); assertEquals(25, task.get().intValue()); assertTrue(listenerLatch.await(5, TimeUnit.SECONDS)); assertTrue(task.isDone()); assertFalse(task.isCancelled()); }
resourceManager.getExecutorService(queryRequest, request.getSchedulerGroup()); final ListenableFutureTask<byte[]> queryFutureTask = createQueryFutureTask(queryRequest, executor); queryFutureTask.addListener(new Runnable() { @Override public void run() {
@Override public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { return delegate.get(timeout, unit); }
public ListenableFuture<?> acquireRequestPermit() { long delayNanos = backoff.getBackoffDelayNanos(); if (delayNanos == 0) { return Futures.immediateFuture(null); } ListenableFutureTask<Object> futureTask = ListenableFutureTask.create(() -> null); scheduledExecutor.schedule(futureTask, delayNanos, NANOSECONDS); return futureTask; }
/** * Creates a {@code ListenableFutureTask} that will upon running, execute the given {@code * Runnable}, and arrange that {@code get} will return the given result on successful completion. * * @param runnable the runnable task * @param result the result to return on successful completion. If you don't need a particular * result, consider using constructions of the form: {@code ListenableFuture<?> f = * ListenableFutureTask.create(runnable, null)} * @since 10.0 */ public static <V> ListenableFutureTask<V> create(Runnable runnable, @Nullable V result) { return new ListenableFutureTask<V>(runnable, result); }