List of usage examples for java.util.concurrent Future cancel
boolean cancel(boolean mayInterruptIfRunning);
From source file:Main.java
private static boolean cancel(final Future<?> future) { return future.isDone() || future.isCancelled() || future.cancel(true); }
From source file:Main.java
public static void cancelTask(Future<?> task, boolean interrupt) { if (task != null && !task.isDone()) { task.cancel(interrupt); }/*from w w w . ja v a2 s. c o m*/ }
From source file:Main.java
public static boolean cancel(final Future<?> future, final boolean mayInterruptIfRunning) { return (future != null) && future.cancel(mayInterruptIfRunning); }
From source file:Main.java
private static <T> T waitForCallableResult(Future<T> future) throws Exception { try {/*from w ww . ja v a 2s . c o m*/ return future.get(); } finally { future.cancel(true); } }
From source file:org.openhab.binding.neeo.internal.NeeoUtil.java
/** * Cancels the specified {@link Future}/*from w w w. j a v a 2 s.c om*/ * * @param future a possibly null future. If null, no action is done */ public static void cancel(@Nullable Future<?> future) { if (future != null) { future.cancel(true); } }
From source file:Main.java
private static <T> T readCallableResult(Future<T> future) throws Exception { try {//from ww w .j a v a 2s . c o m return future.get(60, TimeUnit.SECONDS); } catch (TimeoutException e) { return null; } finally { future.cancel(true); } }
From source file:org.apache.hadoop.hbase.io.hfile.PrefetchExecutor.java
public static void cancel(Path path) { Future<?> future = prefetchFutures.get(path); if (future != null) { // ok to race with other cancellation attempts future.cancel(true); prefetchFutures.remove(path);//from w ww . jav a 2s. co m if (LOG.isDebugEnabled()) { LOG.debug("Prefetch cancelled for " + path); } } }
From source file:Main.java
public static <T> T executeWithTimeout(Callable<T> task, int milliseconds) { ExecutorService executor = Executors.newCachedThreadPool(); Future<T> future = executor.submit(task); T result;//from w w w. j a v a 2s.com try { result = future.get(milliseconds, TimeUnit.MILLISECONDS); } catch (Exception e) { //handle timeout and other exceptions e.printStackTrace(); result = null; } finally { future.cancel(true); } return result; }
From source file:com.codeabovelab.dm.cluman.job.AbstractJobInstance.java
/** * Test that arg is not null and call {@link Future#cancel(boolean)} with true param. * @param future/*from w ww . j av a 2 s . c o m*/ */ protected static void cancel(Future<?> future) { if (future != null) { future.cancel(true); } }
From source file:Main.java
/** * Simple helper method to have timed run method (that timeouts if runs too * long). Good for long calculations wich has time limit. * //from w w w .j a v a2 s . c o m * @param <V> * @param c Callable with calculation logic. * @param timeout Time after calculation times out. * @param timeUnit Time metric (seconds, minutes etc). * * @return Calculation result if calculation finished. * * @throws Throwable Exception that might be thrown in calculation. */ public static <V> V timedRun(Callable<V> c, long timeout, TimeUnit timeUnit) throws Throwable { Future<V> task = taskExec.submit(c); try { return task.get(timeout, timeUnit); } catch (ExecutionException e) { throw e.getCause(); } catch (TimeoutException e) { // throw exception if need to know that timeout occured // or leave empty if null can be returned on timeout } finally { task.cancel(true); } return null; }