use of java.util.concurrent.TimeoutException in project h2o-2 by h2oai.
the class ForkJoinTask method get.
/**
* Waits if necessary for at most the given time for the computation
* to complete, and then retrieves its result, if available.
*
* @param timeout the maximum time to wait
* @param unit the time unit of the timeout argument
* @return the computed result
* @throws CancellationException if the computation was cancelled
* @throws ExecutionException if the computation threw an
* exception
* @throws InterruptedException if the current thread is not a
* member of a ForkJoinPool and was interrupted while waiting
* @throws TimeoutException if the wait timed out
*/
public final V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
if (Thread.interrupted())
throw new InterruptedException();
// Messy in part because we measure in nanosecs, but wait in millisecs
int s;
long ns, ms;
if ((s = status) >= 0 && (ns = unit.toNanos(timeout)) > 0L) {
long deadline = System.nanoTime() + ns;
ForkJoinPool p = null;
ForkJoinPool.WorkQueue w = null;
Thread t = Thread.currentThread();
if (t instanceof ForkJoinWorkerThread) {
ForkJoinWorkerThread wt = (ForkJoinWorkerThread) t;
p = wt.pool;
w = wt.workQueue;
// no retries on failure
s = p.helpJoinOnce(w, this);
}
boolean canBlock = false;
boolean interrupted = false;
try {
while ((s = status) >= 0) {
if (w != null && w.runState < 0)
cancelIgnoringExceptions(this);
else if (!canBlock) {
if (p == null || p.tryCompensate(this, null))
canBlock = true;
} else {
if ((ms = TimeUnit.NANOSECONDS.toMillis(ns)) > 0L && U.compareAndSwapInt(this, STATUS, s, s | SIGNAL)) {
synchronized (this) {
if (status >= 0) {
try {
wait(ms);
} catch (InterruptedException ie) {
if (p == null)
interrupted = true;
}
} else
notifyAll();
}
}
if ((s = status) < 0 || interrupted || (ns = deadline - System.nanoTime()) <= 0L)
break;
}
}
} finally {
if (p != null && canBlock)
p.incrementActiveCount();
}
if (interrupted)
throw new InterruptedException();
}
if ((s &= DONE_MASK) != NORMAL) {
Throwable ex;
if (s == CANCELLED)
throw new CancellationException();
if (s != EXCEPTIONAL)
throw new TimeoutException();
if ((ex = getThrowableException()) != null)
throw new ExecutionException(ex);
}
return getRawResult();
}
use of java.util.concurrent.TimeoutException in project hazelcast by hazelcast.
the class ClientExecutorServiceTest method testGetFutureAfterCancel.
@Test(expected = CancellationException.class)
public void testGetFutureAfterCancel() throws InterruptedException, ExecutionException, TimeoutException {
IExecutorService service = client.getExecutorService(randomString());
CancellationAwareTask task = new CancellationAwareTask(Long.MAX_VALUE);
Future future = service.submit(task);
try {
future.get(1, TimeUnit.SECONDS);
} catch (TimeoutException ignored) {
}
future.cancel(true);
future.get();
}
use of java.util.concurrent.TimeoutException in project hazelcast by hazelcast.
the class ClientExecutorServiceTest method testShutdownMultipleTimes.
@Test
public void testShutdownMultipleTimes() throws InterruptedException, ExecutionException, TimeoutException {
final IExecutorService service = client.getExecutorService(randomString());
service.shutdownNow();
service.shutdown();
assertTrueEventually(new AssertTask() {
public void run() throws Exception {
assertTrue(service.isShutdown());
}
});
}
use of java.util.concurrent.TimeoutException in project hazelcast by hazelcast.
the class ClientExecutorServiceTest method testShutdownNow.
@Test
public void testShutdownNow() throws InterruptedException, ExecutionException, TimeoutException {
final IExecutorService service = client.getExecutorService(randomString());
service.shutdownNow();
assertTrueEventually(new AssertTask() {
public void run() throws Exception {
assertTrue(service.isShutdown());
}
});
}
use of java.util.concurrent.TimeoutException in project stetho by facebook.
the class ResponseBodyFileManager method prettyPrintContentWithTimeOut.
private String prettyPrintContentWithTimeOut(AsyncPrettyPrinter asyncPrettyPrinter, InputStream in) throws IOException {
AsyncPrettyPrintingCallable prettyPrintingCallable = new AsyncPrettyPrintingCallable(in, asyncPrettyPrinter);
ExecutorService executorService = AsyncPrettyPrinterExecutorHolder.getExecutorService();
if (executorService == null) {
//last peer is unregistered...
return null;
}
Future<String> future = executorService.submit(prettyPrintingCallable);
try {
return Util.getUninterruptibly(future, PRETTY_PRINT_TIMEOUT_SEC, TimeUnit.SECONDS);
} catch (TimeoutException e) {
future.cancel(true);
return "Time out after " + PRETTY_PRINT_TIMEOUT_SEC + " seconds of attempting to pretty print\n" + Util.readAsUTF8(in);
} catch (ExecutionException e) {
Throwable cause = e.getCause();
ExceptionUtil.propagateIfInstanceOf(cause, IOException.class);
throw ExceptionUtil.propagate(cause);
}
}
Aggregations