Search in sources :

Example 56 with TimeoutException

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();
}
Also used : CancellationException(java.util.concurrent.CancellationException) ExecutionException(java.util.concurrent.ExecutionException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 57 with TimeoutException

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();
}
Also used : Future(java.util.concurrent.Future) IExecutorService(com.hazelcast.core.IExecutorService) CancellationAwareTask(com.hazelcast.client.executor.tasks.CancellationAwareTask) TimeoutException(java.util.concurrent.TimeoutException) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 58 with TimeoutException

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());
        }
    });
}
Also used : AssertTask(com.hazelcast.test.AssertTask) IExecutorService(com.hazelcast.core.IExecutorService) TimeoutException(java.util.concurrent.TimeoutException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) CancellationException(java.util.concurrent.CancellationException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 59 with TimeoutException

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());
        }
    });
}
Also used : AssertTask(com.hazelcast.test.AssertTask) IExecutorService(com.hazelcast.core.IExecutorService) TimeoutException(java.util.concurrent.TimeoutException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) CancellationException(java.util.concurrent.CancellationException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 60 with TimeoutException

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);
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

TimeoutException (java.util.concurrent.TimeoutException)788 ExecutionException (java.util.concurrent.ExecutionException)249 IOException (java.io.IOException)184 Test (org.junit.Test)149 ArrayList (java.util.ArrayList)75 CountDownLatch (java.util.concurrent.CountDownLatch)73 ExecutorService (java.util.concurrent.ExecutorService)71 Future (java.util.concurrent.Future)54 CancellationException (java.util.concurrent.CancellationException)44 Test (org.testng.annotations.Test)44 List (java.util.List)39 HashMap (java.util.HashMap)38 Map (java.util.Map)38 File (java.io.File)36 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)36 TimeUnit (java.util.concurrent.TimeUnit)34 AtomicReference (java.util.concurrent.atomic.AtomicReference)26 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)22 URI (java.net.URI)21 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)21