/* visible for testing */ boolean doWarmUp() { Future future = null; try { future = threadPoolExecutor.submit(updateTask); future.get(warmUpTimeoutMs, TimeUnit.MILLISECONDS); // block until done or timeout return true; } catch (Exception e) { logger.warn("Best effort warm up failed", e); } finally { if (future != null) { future.cancel(true); } } return false; }
@Override public void runWithTimeout(Runnable runnable, long timeoutDuration, TimeUnit timeoutUnit) throws TimeoutException, InterruptedException { checkNotNull(runnable); checkNotNull(timeoutUnit); checkPositiveTimeout(timeoutDuration); Future<?> future = executor.submit(runnable); try { future.get(timeoutDuration, timeoutUnit); } catch (InterruptedException | TimeoutException e) { future.cancel(true /* mayInterruptIfRunning */); throw e; } catch (ExecutionException e) { wrapAndThrowRuntimeExecutionExceptionOrError(e.getCause()); throw new AssertionError(); } }
final String[] signature = strings; Future<Object> task = executor.submit(new Callable<Object>() { return task.get(timeout, TimeUnit.MILLISECONDS); } catch (ExecutionException e) { if (e.getCause() instanceof MBeanException) { throw new MBeanException(e); } finally { if (!task.isDone()) { task.cancel(true);
Iterator<? extends Callable<T>> it = tasks.iterator(); futures.add(submitAndAddQueueListener(executorService, it.next(), futureQueue)); --ntasks; int active = 1; if (ntasks > 0) { --ntasks; futures.add(submitAndAddQueueListener(executorService, it.next(), futureQueue)); ++active; } else if (active == 0) { --active; try { return f.get(); } catch (ExecutionException eex) { ee = eex; } finally { for (Future<T> f : futures) { f.cancel(true);
Semaphore latch = new Semaphore(0); Metronome metronome = Metronome.sleeper(50, TimeUnit.MILLISECONDS, Clock.SYSTEM); Future<?> result = executorService.submit(() -> { while (!Thread.interrupted()) { for(;;) { List<ReplicationMessage> message = new ArrayList<>(); stream.readPending(x -> message.add(x)); if (message.isEmpty()) { break; result.cancel(true); fail("expected " + expectedMessages + " messages, but read only " + actualMessages.size());
public static void main(String[] args) throws Exception { ExecutorService executor = Executors.newSingleThreadExecutor(); Future<String> future = executor.submit(new Task()); System.out.println("Started.."); System.out.println(future.get(3, TimeUnit.SECONDS)); System.out.println("Finished!"); } catch (TimeoutException e) { future.cancel(true); System.out.println("Terminated!");
if (fork.get().isSucceeded()) { if (!fork.get().commit()) { failedForkIds.add(fork.get().getIndex()); failedForkIds.add(fork.get().getIndex()); if (forkAndFuture.getKey().isPresent() && forkAndFuture.getValue().isPresent()) { try { forkAndFuture.getValue().get().cancel(true); } catch (Throwable t) { LOG.error(String.format("Failed to cancel Fork \"%s\"", forkAndFuture.getKey().get()), t);
Future<?> metricsPoller = threadPool.submit(new Runnable() { @Override public void run() { threadPool.submit(new Runnable() { @Override public void run() { metricsPoller.cancel(true); } catch (InterruptedException ex) { fail("Timeout on all threads writing percentiles"); System.out.println(p.getMean() + " : " + p.getPercentile(50) + " : " + p.getPercentile(75) + " : " + p.getPercentile(90) + " : " + p.getPercentile(95) + " : " + p.getPercentile(99));
public static void main(String[] args) throws InterruptedException { ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1); Runnable r = new Runnable() { @Override public void run() { System.out.println("Hello"); } }; ScheduledFuture<?> scheduledFuture = scheduledExecutorService.scheduleAtFixedRate(r, 1L, 1L, TimeUnit.SECONDS); Thread.sleep(5000L); scheduledFuture.cancel(false); }
private static void checkCombineFutures(List<Future> combineFutures) { for (Future future : combineFutures) { try { if (!future.isDone()) { // Cancel futures if close() for the iterator is called early due to some reason (e.g., test failure) future.cancel(true); } else { future.get(); } } catch (InterruptedException | CancellationException e) { throw new QueryInterruptedException(e); } catch (ExecutionException e) { throw new RuntimeException(e); } } }
@Override public void runUninterruptiblyWithTimeout( Runnable runnable, long timeoutDuration, TimeUnit timeoutUnit) throws TimeoutException { checkNotNull(runnable); checkNotNull(timeoutUnit); checkPositiveTimeout(timeoutDuration); Future<?> future = executor.submit(runnable); try { Uninterruptibles.getUninterruptibly(future, timeoutDuration, timeoutUnit); } catch (TimeoutException e) { future.cancel(true /* mayInterruptIfRunning */); throw e; } catch (ExecutionException e) { wrapAndThrowRuntimeExecutionExceptionOrError(e.getCause()); throw new AssertionError(); } }
Iterator<? extends Callable<T>> it = tasks.iterator(); futures.add(submitAndAddQueueListener(executorService, it.next(), futureQueue)); --ntasks; int active = 1; if (ntasks > 0) { --ntasks; futures.add(submitAndAddQueueListener(executorService, it.next(), futureQueue)); ++active; } else if (active == 0) { --active; try { return f.get(); } catch (ExecutionException eex) { ee = eex; } finally { for (Future<T> f : futures) { f.cancel(true);
System.out.println("Original Numbers: " + Arrays.toString(numbers)); if (--futureNumber >= 1) { Future<ByteBufferStreamable> result2 = completion.take(); completion.submit(new SortingTwoByteCallable(result.get(), result2.get())); ByteBufferStreamable results = finalValue.get(); System.out.println("Sorted values: " + Arrays.toString( results.buffer.array())); System.out.println("Distributed Sort Took: " + Util.printTime(totalDistributed, TimeUnit.NANOSECONDS)); queue.add(executor.submit(runner)); System.out.println("Started Consumer - running " + queue.size() + " consumers"); queue.remove().cancel(true); System.out.println("Stopped Consumer - running " + queue.size() + " consumers");
Future<Long> futureResult = service.execute(myCallable); Long result = null; try{ result = futureResult.get(5000, TimeUnit.MILLISECONDS); }catch(TimeoutException e){ System.out.println("Time out after 5 seconds"); futureResult.cancel(true); }catch(InterruptedException ie){ System.out.println("Error: Interrupted"); }catch(ExecutionException ee){ System.out.println("Error: Execution interrupted"); }
@CanIgnoreReturnValue @Override public <T> T callWithTimeout(Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit) throws TimeoutException, InterruptedException, ExecutionException { checkNotNull(callable); checkNotNull(timeoutUnit); checkPositiveTimeout(timeoutDuration); Future<T> future = executor.submit(callable); try { return future.get(timeoutDuration, timeoutUnit); } catch (InterruptedException | TimeoutException e) { future.cancel(true /* mayInterruptIfRunning */); throw e; } catch (ExecutionException e) { wrapAndThrowExecutionExceptionOrError(e.getCause()); throw new AssertionError(); } }
import static java.util.concurrent.TimeUnit.*; class BeeperControl { private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); public void beepForAnHour() { final Runnable beeper = new Runnable() { public void run() { System.out.println("beep"); } }; final ScheduledFuture<?> beeperHandle = scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS); scheduler.schedule(new Runnable() { public void run() { beeperHandle.cancel(true); } }, 60 * 60, SECONDS); } }