@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(); } }
List<Future<T>> futures = Lists.newArrayListWithCapacity(ntasks); BlockingQueue<Future<T>> futureQueue = Queues.newLinkedBlockingQueue(); long timeoutNanos = unit.toNanos(timeout); f = futureQueue.poll(timeoutNanos, TimeUnit.NANOSECONDS); if (f == null) { throw new TimeoutException(); --active; try { return f.get(); } catch (ExecutionException eex) { ee = eex; } catch (RuntimeException rex) { ee = new ExecutionException(rex); ee = new ExecutionException(null); f.cancel(true);
private SessionFactory getSessionFactory() { try { return this.sessionFactoryFuture.get(); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); throw new IllegalStateException("Interrupted during initialization of Hibernate SessionFactory", ex); } catch (ExecutionException ex) { Throwable cause = ex.getCause(); if (cause instanceof HibernateException) { // Rethrow a provider configuration exception (possibly with a nested cause) directly throw (HibernateException) cause; } throw new IllegalStateException("Failed to asynchronously initialize Hibernate SessionFactory: " + ex.getMessage(), cause); } } }
@Override public ClientHttpResponse execute() throws IOException { try { return executeAsync().get(); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); throw new IOException("Interrupted during request execution", ex); } catch (ExecutionException ex) { if (ex.getCause() instanceof IOException) { throw (IOException) ex.getCause(); } else { throw new IOException(ex.getMessage(), ex.getCause()); } } }
private void tailResponse(final String method, final Response response, final ProgressHandler handler, final WebTarget resource) throws DockerException, InterruptedException { final ExecutorService executor = Executors.newSingleThreadExecutor(); try { final ProgressStream stream = response.readEntity(ProgressStream.class); final Future<?> future = executor.submit( new ResponseTailReader(stream, handler, method, resource)); future.get(); } catch (ExecutionException e) { final Throwable cause = e.getCause(); if (cause instanceof DockerException) { throw (DockerException)cause; } else { throw new DockerException(cause); } } finally { executor.shutdownNow(); try { response.close(); } catch (ProcessingException e) { // ignore, thrown by jnr-unixsocket when httpcomponent try to read after close // the socket is closed before this exception } } }
@Test public void isnt_inheritable() throws Exception { ExecutorService service = Executors.newCachedThreadPool(); try (Scope scope = currentTraceContext.newScope(context)) { assertThat(service.submit(() -> { verifyImplicitContext(null); return currentTraceContext.get(); }).get()).isNull(); } catch (ExecutionException e) { if (e.getCause() instanceof Error) throw (Error) e.getCause(); throw (Exception) e.getCause(); } assertThat(service.submit(currentTraceContext::get).get()) .isNull(); verifyImplicitContext(null); service.shutdownNow(); }
ExecutorService es = Executors.newFixedThreadPool(CONCURRENCY); maxIterations, idAuthority, ids); uids.add(idAuthority.getUniqueID()); futures.add(es.submit(stressRunnable)); f.get(); } catch (ExecutionException e) { throw e.getCause(); es.shutdownNow();
@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(); } }
ExecutorService pool = Executors.newFixedThreadPool(callables.size()); try{ List<Future<CommandProcessorResponse>> futures = pool.invokeAll(callables); responseList.add(future.get()); (ex.getCause() instanceof CommandProcessorResponse) ? new CommandProcessorResponse(ErrorMsg.COMPILE_LOCK_TIMED_OUT.getErrorCode()) : new CommandProcessorResponse(CONCURRENT_COMPILATION)); pool.shutdown();
@Override public C get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { if (!_latch.await(timeout,unit)) throw new TimeoutException(); if (_cause==COMPLETED) return _result; if (_cause instanceof TimeoutException) throw (TimeoutException)_cause; if (_cause instanceof CancellationException) throw (CancellationException) new CancellationException().initCause(_cause); throw new ExecutionException(_cause); }
@Test(/* timeout = 5000, */expected = IndexOutOfBoundsException.class) public void testExceptionWithMoreThanOneElement() throws Throwable { Observable<String> obs = Observable.just("one", "two"); Future<String> f = obs.toFuture(); try { // we expect an exception since there are more than 1 element f.get(); fail("Should have thrown!"); } catch (ExecutionException e) { throw e.getCause(); } }
ExecutorService executor = newSingleThreadExecutor(); Future<V> waiter = executor.submit( new Callable<V>() { @Override return getUninterruptibly(waiter, timeout, unit); } catch (ExecutionException e) { propagateIfInstanceOf(e.getCause(), ExecutionException.class); propagateIfInstanceOf(e.getCause(), CancellationException.class); throw failureWithCause(e, "Unexpected exception"); } finally { executor.shutdownNow();
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()); }
@Test public void test() throws InterruptedException, ExecutionException { SLEEP_MS = 1000; long startNs = System.nanoTime(); try { LOCATOR.getRegionLocation(TABLE_NAME, EMPTY_START_ROW, RegionLocateType.CURRENT, TimeUnit.MILLISECONDS.toNanos(500)).get(); fail(); } catch (ExecutionException e) { e.printStackTrace(); assertThat(e.getCause(), instanceOf(TimeoutIOException.class)); } long costMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs); assertTrue(costMs >= 500); assertTrue(costMs < 1000); // wait for the background task finish Thread.sleep(2000); // Now the location should be in cache, so we will not visit meta again. HRegionLocation loc = LOCATOR.getRegionLocation(TABLE_NAME, EMPTY_START_ROW, RegionLocateType.CURRENT, TimeUnit.MILLISECONDS.toNanos(500)).get(); assertEquals(loc.getServerName(), TEST_UTIL.getHBaseCluster().getRegionServer(0).getServerName()); } }
context.setFuture(null); return future.get(timeout, TimeUnit.MILLISECONDS); } catch (TimeoutException ex) { if (!future.isDone()) { throw new SofaTimeOutException("Future is not done when timeout.", ex); } else { throw new SofaTimeOutException(ex.getMessage(), ex); Throwable cause = ex.getCause(); if (cause instanceof SofaRpcException) { throw (SofaRpcException) cause;
@Override public void run() { _started = true; _latch.countDown(); try { _future.get(); _done = true; } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ExecutionException e) { // TODO Auto-generated catch block e.printStackTrace(); } _latch2.countDown(); }
private PartETag awaitPendingPartUploadToComplete(CompletableFuture<PartETag> upload) throws IOException { final PartETag completedUploadEtag; try { completedUploadEtag = upload.get(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new IOException("Interrupted while waiting for part uploads to complete"); } catch (ExecutionException e) { throw new IOException("Uploading parts failed", e.getCause()); } return completedUploadEtag; }
@Override public void train(Dataset dataset) { params.svm_type = getLibsvmType(); svm_problem problem = toSvmProblem(dataset); ExecutorService service = newCachedThreadPool(threadsNamed("libsvm-trainer-" + System.identityHashCode(this) + "-%s")); try { TimeLimiter limiter = SimpleTimeLimiter.create(service); //TODO: this time limit should be configurable model = limiter.callWithTimeout(getTrainingFunction(problem, params), 1, TimeUnit.HOURS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new RuntimeException(e); } catch (ExecutionException e) { Throwable cause = e.getCause(); if (cause != null) { throwIfUnchecked(cause); throw new RuntimeException(cause); } } catch (Exception e) { throwIfUnchecked(e); throw new RuntimeException(e); } finally { service.shutdownNow(); } }
@Override public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { if (latch.await(timeout, unit)) { if (error != null) { throw new ExecutionException(error); } return result; } else { throw new TimeoutException(); } }