private void push() { if (DELAY_SECONDS < 0) return; synchronized (lock) { // Can be done or canceled in case of a bug or external intervention - do not allow it to hang there forever if (nextSave != null && !(nextSave.isDone() || nextSave.isCancelled())) return; nextSave = Timer.get().schedule(this, DELAY_SECONDS, TimeUnit.SECONDS); } }
public void cacelAllFutures() { for (Future future : futures) { if (!future.isDone() && !future.isCancelled()) { future.cancel(true); } } }
@Override public void stop() { for (Future<?> future: this.expirationFutures.values()) { future.cancel(true); } for (Future<?> future: this.expirationFutures.values()) { if (!future.isCancelled() && !future.isDone()) { try { future.get(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } catch (ExecutionException e) { // Ignore } } } for(Map.Entry<K, Entry<V>> entry : entries.entrySet()) { this.factory.destroyInstance(entry.getValue().getValue()); } this.expirationFutures.clear(); this.entries.clear(); }
/** * A task that sleeps for a second, then returns 1 **/ public static class MyCallable implements Callable<Integer> { @Override public Integer call() throws Exception { Thread.sleep(1000); return 1; } } public static void main(String[] args) throws Exception{ ExecutorService exec = Executors.newSingleThreadExecutor(); Future<Integer> f = exec.submit(new MyCallable()); System.out.println(f.isDone()); //False System.out.println(f.get()); //Waits until the task is done, then prints 1 }
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 run() { boolean failures = false; INSTALLING: while (true) { try { updateCenter.persistInstallStatus(); Thread.sleep(500); failures = false; for (Future<UpdateCenter.UpdateCenterJob> jobFuture : installJobs) { if(!jobFuture.isDone() && !jobFuture.isCancelled()) { continue INSTALLING; } UpdateCenter.UpdateCenterJob job = jobFuture.get(); if(job instanceof InstallationJob && ((InstallationJob)job).status instanceof DownloadJob.Failure) { failures = true; } } } catch (Exception e) { LOGGER.log(WARNING, "Unexpected error while waiting for initial plugin set to install.", e); } break; } updateCenter.persistInstallStatus(); if(!failures) { try (ACLContext acl = ACL.as(currentAuth)) { InstallUtil.proceedToNextStateFrom(InstallState.INITIAL_PLUGINS_INSTALLING); } } } }.start();
/** * Submit a fetch task if remainingBytes is smaller than prefetchTriggerBytes. */ private void fetchIfNeeded(long remainingBytes) { if ((fetchFutures.isEmpty() || fetchFutures.peekLast().isDone()) && remainingBytes <= prefetchConfig.getPrefetchTriggerBytes()) { Future<Void> fetchFuture = fetchExecutor.submit(() -> { fetch(); return null; }); fetchFutures.add(fetchFuture); } }
return src.cancel(mayInterruptIfRunning); return src.isCancelled(); return src.isDone(); return src.get(); return src.get();
public boolean performInteractions(@NonNull final File reportFile) { final ExecutorService executorService = Executors.newCachedThreadPool(); final List<Future<Boolean>> futures = new ArrayList<>(); for (final ReportInteraction reportInteraction : reportInteractions) { futures.add(executorService.submit(() -> { if (ACRA.DEV_LOGGING) ACRA.log.d(ACRA.LOG_TAG, "Calling ReportInteraction of class " + reportInteraction.getClass().getName()); return reportInteraction.performInteraction(context, config, reportFile); })); } boolean sendReports = true; for (Future<Boolean> future : futures) { while (!future.isDone()) { try { sendReports &= future.get(); } catch (InterruptedException ignored) { } catch (ExecutionException e) { //ReportInteraction crashed, so ignore it break; } } } return sendReports; } }
@Override public void close() { for (Future<?> future: this.expirationFutures.values()) { future.cancel(false); } for (Future<?> future: this.expirationFutures.values()) { if (!future.isDone()) { try { future.get(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } catch (ExecutionException e) { // Ignore } } } this.expirationFutures.clear(); }
if (futureOrLong instanceof Future) { Future future = (Future) futureOrLong; if (future.isDone()) { if (future.isCancelled()) { it.remove(); } else { future.get(0, TimeUnit.SECONDS);
@Override public void run() { try { Future<?> future = executor.submit( new Callable<Void>() { @Override public Void call() throws Exception { // WAIT #1 barrier.await(1, TimeUnit.SECONDS); // WAIT #2 barrier.await(1, TimeUnit.SECONDS); assertTrue(executor.isShutdown()); assertFalse(executor.isTerminated()); // WAIT #3 barrier.await(1, TimeUnit.SECONDS); return null; } }); assertTrue(future.isDone()); assertTrue(executor.isShutdown()); assertTrue(executor.isTerminated()); } catch (Throwable t) { throwableFromOtherThread.set(t); } } });
private void clearCompletedFutures() { futures.removeIf(future -> future.isCancelled() || future.isDone()); }
private void cancelExecutePoolThread(Future<T> future) { int retryCount = 0; while(retryCount < this.maxTaskCancelRetryCount && !future.isDone()) { LOG.info("Task is still executing the job request. Cancelling it with retry count: " + retryCount); if (future.cancel(true)) { /* * Cancelled the job request and return to client. */ LOG.info("Cancel job request issued successfully."); return; } retryCount++; try { Thread.sleep(this.maxTaskCancelRetryWaitTimeInMs); } catch (InterruptedException e) { /* * Nothing to do. Just retry. */ } } LOG.warn("Failed to cancel the job. isCancelled: " + future.isCancelled() + " Retry count: " + retryCount); }
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);
@Override public boolean isServing() { try { Future<?> masterServingCheckFuture = mExecutorService.submit( new MasterServingCheckRunnable()); if (mProcessCheck) { Future<?> processCheckFuture = mExecutorService.submit( new ProcessCheckRunnable(mAlluxioMasterName)); CommonUtils.sleepMs(Constants.SECOND_MS); while (!masterServingCheckFuture.isDone()) { if (processCheckFuture.isDone()) { throw new IllegalStateException("One or more master processes are not running"); } CommonUtils.sleepMs(Constants.SECOND_MS); } CommonUtils.sleepMs(Constants.SECOND_MS); LOG.debug("Checking the master processes one more time..."); return !processCheckFuture.isDone(); } else { masterServingCheckFuture.get(); return true; } } catch (Exception e) { LOG.error("Exception thrown in master health check client {}", e); } finally { mExecutorService.shutdown(); } return false; } }