@Override public ListenableScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) { Preconditions.checkNotNull(command, "command must not be null"); Preconditions.checkNotNull(unit, "unit must not be null!"); return schedule(java.util.concurrent.Executors.callable(command), delay, unit); }
/** * @throws RejectedExecutionException {@inheritDoc} * @throws NullPointerException {@inheritDoc} */ public <T> Future<T> submit(Runnable task, T result) { return schedule(Executors.callable(task, result), 0, NANOSECONDS); }
/** * Wraps a {@code Runnable} for submission to the underlying executor. The default implementation * delegates to {@link #wrapTask(Callable)}. */ protected Runnable wrapTask(Runnable command) { final Callable<Object> wrapped = wrapTask(Executors.callable(command, null)); return new Runnable() { @Override public void run() { try { wrapped.call(); } catch (Exception e) { throwIfUnchecked(e); throw new RuntimeException(e); } } }; }
ExecutorService es = Executors.newFixedThreadPool(2); List<Callable<Object>> todo = new ArrayList<Callable<Object>>(singleTable.size()); for (DataTable singleTable: uniquePhrases) { todo.add(Executors.callable(new ComputeDTask(singleTable))); } List<Future<Object>> answers = es.invokeAll(todo);
/** * 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)} */ static <V> TrustedListenableFutureTask<V> create(Runnable runnable, @Nullable V result) { return new TrustedListenableFutureTask<V>(Executors.callable(runnable, result)); }
/** * Wraps a {@code Runnable} for submission to the underlying executor. The default implementation * delegates to {@link #wrapTask(Callable)}. */ protected Runnable wrapTask(Runnable command) { final Callable<Object> wrapped = wrapTask(Executors.callable(command, null)); return new Runnable() { @Override public void run() { try { wrapped.call(); } catch (Exception e) { throwIfUnchecked(e); throw new RuntimeException(e); } } }; }
/** * Wraps a {@code Runnable} for submission to the underlying executor. The default implementation * delegates to {@link #wrapTask(Callable)}. */ protected Runnable wrapTask(Runnable command) { final Callable<Object> wrapped = wrapTask(Executors.callable(command, null)); return new Runnable() { @Override public void run() { try { wrapped.call(); } catch (Exception e) { throwIfUnchecked(e); throw new RuntimeException(e); } } }; }
@Override public <T> Future<T> submit(Runnable task, T result) { return submit(Executors.callable(task, result)); }
@Override public Future<?> submit(Runnable task) { return submit(Executors.callable(task)); }
/** * <p> * A convenience method for transactions that don't require a return * value. Simply wraps the <code>transactor</code> using {@link * Executors#callable} and passes that to {@link * #transact(Channel,Callable)}. * </p> * @see #transact(Channel,Callable) * @see Executors#callable(Runnable) */ public static void transact(Channel channel, Runnable transactor) throws ChannelException { transact(channel, Executors.callable(transactor)); }
/** * 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)} */ static <V> TrustedListenableFutureTask<V> create(Runnable runnable, @NullableDecl V result) { return new TrustedListenableFutureTask<V>(Executors.callable(runnable, result)); }
/** * 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)} */ static <V> TrustedListenableFutureTask<V> create(Runnable runnable, @NullableDecl V result) { return new TrustedListenableFutureTask<V>(Executors.callable(runnable, result)); }
@Override public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) { ObjectUtil.checkNotNull(command, "command"); ObjectUtil.checkNotNull(unit, "unit"); if (initialDelay < 0) { throw new IllegalArgumentException( String.format("initialDelay: %d (expected: >= 0)", initialDelay)); } if (period <= 0) { throw new IllegalArgumentException( String.format("period: %d (expected: > 0)", period)); } validateScheduled0(initialDelay, unit); validateScheduled0(period, unit); return schedule(new ScheduledFutureTask<Void>( this, Executors.<Void>callable(command, null), ScheduledFutureTask.deadlineNanos(unit.toNanos(initialDelay)), unit.toNanos(period))); }
@Override public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) { ObjectUtil.checkNotNull(command, "command"); ObjectUtil.checkNotNull(unit, "unit"); if (initialDelay < 0) { throw new IllegalArgumentException( String.format("initialDelay: %d (expected: >= 0)", initialDelay)); } if (delay <= 0) { throw new IllegalArgumentException( String.format("delay: %d (expected: > 0)", delay)); } validateScheduled0(initialDelay, unit); validateScheduled0(delay, unit); return schedule(new ScheduledFutureTask<Void>( this, Executors.<Void>callable(command, null), ScheduledFutureTask.deadlineNanos(unit.toNanos(initialDelay)), -unit.toNanos(delay))); }
/** * 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)} */ static <V> TrustedListenableFutureTask<V> create(Runnable runnable, @NullableDecl V result) { return new TrustedListenableFutureTask<V>(Executors.callable(runnable, result)); }
@Override public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) { ObjectUtil.checkNotNull(command, "command"); ObjectUtil.checkNotNull(unit, "unit"); if (initialDelay < 0) { throw new IllegalArgumentException( String.format("initialDelay: %d (expected: >= 0)", initialDelay)); } if (period <= 0) { throw new IllegalArgumentException( String.format("period: %d (expected: > 0)", period)); } validateScheduled0(initialDelay, unit); validateScheduled0(period, unit); return schedule(new ScheduledFutureTask<Void>( this, Executors.<Void>callable(command, null), ScheduledFutureTask.deadlineNanos(unit.toNanos(initialDelay)), unit.toNanos(period))); }
@Override public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) { ObjectUtil.checkNotNull(command, "command"); ObjectUtil.checkNotNull(unit, "unit"); if (initialDelay < 0) { throw new IllegalArgumentException( String.format("initialDelay: %d (expected: >= 0)", initialDelay)); } if (delay <= 0) { throw new IllegalArgumentException( String.format("delay: %d (expected: > 0)", delay)); } validateScheduled0(initialDelay, unit); validateScheduled0(delay, unit); return schedule(new ScheduledFutureTask<Void>( this, Executors.<Void>callable(command, null), ScheduledFutureTask.deadlineNanos(unit.toNanos(initialDelay)), -unit.toNanos(delay))); }
/** * Executes a task, on N threads, all starting at the same time. * * @param nThreads the number of threads to execute * @param task the task to execute in each thread * @return the execution time for all threads to complete, in nanoseconds */ public static long timeTasks(int nThreads, Runnable task) { return timeTasks(nThreads, Executors.callable(task)).executionTime(); }
allTasks.add(cancelRunnable); allTasks.add(setFutureCompleteSucessFullyRunnable); allTasks.add(Executors.callable(collectResultsRunnable)); assertEquals(allTasks.size() + 1, barrier.getParties()); // sanity check for (int i = 0; i < 1000; i++) {
public void testSchedule() { MockExecutor mock = new MockExecutor(); TestExecutor testExecutor = new TestExecutor(mock); @SuppressWarnings("unused") // go/futurereturn-lsc Future<?> possiblyIgnoredError = testExecutor.schedule(DO_NOTHING, 10, TimeUnit.MINUTES); mock.assertLastMethodCalled("scheduleRunnable", 10, TimeUnit.MINUTES); @SuppressWarnings("unused") // go/futurereturn-lsc Future<?> possiblyIgnoredError1 = testExecutor.schedule(Executors.callable(DO_NOTHING), 5, TimeUnit.SECONDS); mock.assertLastMethodCalled("scheduleCallable", 5, TimeUnit.SECONDS); }