Search in sources :

Example 66 with CancellationException

use of java.util.concurrent.CancellationException in project java-common-lib by sosy-lab.

the class ProcessExecutor method join.

/**
 * Wait for the process to terminate.
 *
 * @param timelimit Maximum time to wait for process (in milliseconds)
 * @return The exit code of the process.
 * @throws IOException passed from the handle* methods.
 * @throws E passed from the handle* methods.
 * @throws TimeoutException If timeout is hit.
 * @throws InterruptedException If the current thread is interrupted.
 */
public int join(long timelimit) throws IOException, E, TimeoutException, InterruptedException {
    try {
        @Var Integer exitCode = null;
        try {
            if (timelimit > 0) {
                exitCode = processFuture.get(timelimit, TimeUnit.MILLISECONDS);
            } else {
                exitCode = processFuture.get();
            }
        } catch (CancellationException e) {
        // the processFuture has been cancelled, probably because the outFuture or
        // the errFuture threw an exception
        // ignore exception here and call get() on the other futures to get their
        // exceptions
        }
        // wait for reading tasks to finish and to get exceptions
        outFuture.get();
        errFuture.get();
        if (exitCode == null) {
            // Assume this as an interrupt.
            throw new InterruptedException();
        }
        return exitCode;
    } catch (TimeoutException e) {
        logger.log(Level.WARNING, "Killing", name, "due to timeout");
        processFuture.cancel(true);
        throw e;
    } catch (InterruptedException e) {
        logger.log(Level.WARNING, "Killing", name, "due to user interrupt");
        processFuture.cancel(true);
        throw e;
    } catch (ExecutionException e) {
        Throwable t = e.getCause();
        Throwables.propagateIfPossible(t, IOException.class, exceptionClass);
        throw new UnexpectedCheckedException("output handling of external process " + name, t);
    } finally {
        assert processFuture.isDone();
        // needed for memory visibility of the Callables
        Concurrency.waitForTermination(executor);
        try {
            in.close();
        } catch (IOException e) {
        // Not expected to happen because process is already dead anyway.
        // Don't hide any real exception by throwing this one.
        }
        finished = true;
    }
}
Also used : UnexpectedCheckedException(org.sosy_lab.common.Classes.UnexpectedCheckedException) CancellationException(java.util.concurrent.CancellationException) Var(com.google.errorprone.annotations.Var) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 67 with CancellationException

use of java.util.concurrent.CancellationException in project streamsupport by stefan-zobel.

the class ForkJoinPool8Test method testJoinIgnoresInterrupts.

/**
 * join/quietlyJoin of a forked task succeeds in the presence of interrupts
 */
public void testJoinIgnoresInterrupts() {
    @SuppressWarnings("serial") RecursiveAction a = new CheckedRecursiveAction() {

        protected void realCompute() {
            FibAction f = new FibAction(8);
            final Thread currentThread = Thread.currentThread();
            // test join()
            assertSame(f, f.fork());
            currentThread.interrupt();
            assertNull(f.join());
            Thread.interrupted();
            assertEquals(21, f.result);
            checkCompletedNormally(f);
            f = new FibAction(8);
            f.cancel(true);
            assertSame(f, f.fork());
            currentThread.interrupt();
            try {
                f.join();
                shouldThrow();
            } catch (CancellationException success) {
                Thread.interrupted();
                checkCancelled(f);
            }
            f = new FibAction(8);
            f.completeExceptionally(new FJException());
            assertSame(f, f.fork());
            currentThread.interrupt();
            try {
                f.join();
                shouldThrow();
            } catch (FJException success) {
                Thread.interrupted();
                checkCompletedAbnormally(f, success);
            }
            // test quietlyJoin()
            f = new FibAction(8);
            assertSame(f, f.fork());
            currentThread.interrupt();
            f.quietlyJoin();
            Thread.interrupted();
            assertEquals(21, f.result);
            checkCompletedNormally(f);
            f = new FibAction(8);
            f.cancel(true);
            assertSame(f, f.fork());
            currentThread.interrupt();
            f.quietlyJoin();
            Thread.interrupted();
            checkCancelled(f);
            f = new FibAction(8);
            f.completeExceptionally(new FJException());
            assertSame(f, f.fork());
            currentThread.interrupt();
            f.quietlyJoin();
            Thread.interrupted();
            checkCompletedAbnormally(f, f.getException());
        }
    };
    checkInvoke(a);
    a.reinitialize();
    checkInvoke(a);
}
Also used : CancellationException(java.util.concurrent.CancellationException) RecursiveAction(java8.util.concurrent.RecursiveAction)

Example 68 with CancellationException

use of java.util.concurrent.CancellationException in project streamsupport by stefan-zobel.

the class ForkJoinPool8Test method testCancelledForkGet.

/**
 * get of a forked task throws exception when task cancelled
 */
public void testCancelledForkGet() {
    @SuppressWarnings("serial") RecursiveAction a = new CheckedRecursiveAction() {

        protected void realCompute() throws Exception {
            FibAction f = new FibAction(8);
            assertTrue(f.cancel(true));
            assertSame(f, f.fork());
            try {
                f.get();
                shouldThrow();
            } catch (CancellationException success) {
                checkCancelled(f);
            }
        }
    };
    checkInvoke(a);
}
Also used : CancellationException(java.util.concurrent.CancellationException) RecursiveAction(java8.util.concurrent.RecursiveAction)

Example 69 with CancellationException

use of java.util.concurrent.CancellationException in project streamsupport by stefan-zobel.

the class ForkJoinPool8Test method testCancelledForkTimedGet.

/**
 * timed get of a forked task throws exception when task cancelled
 */
public void testCancelledForkTimedGet() {
    @SuppressWarnings("serial") RecursiveAction a = new CheckedRecursiveAction() {

        protected void realCompute() throws Exception {
            FibAction f = new FibAction(8);
            assertTrue(f.cancel(true));
            assertSame(f, f.fork());
            try {
                f.get(LONG_DELAY_MS, MILLISECONDS);
                shouldThrow();
            } catch (CancellationException success) {
                checkCancelled(f);
            }
        }
    };
    checkInvoke(a);
}
Also used : CancellationException(java.util.concurrent.CancellationException) RecursiveAction(java8.util.concurrent.RecursiveAction)

Example 70 with CancellationException

use of java.util.concurrent.CancellationException in project streamsupport by stefan-zobel.

the class RecursiveActionTest method testCancelledForkTimedGet.

/**
 * timed get of a forked task throws exception when task cancelled
 */
public void testCancelledForkTimedGet() {
    @SuppressWarnings("serial") RecursiveAction a = new CheckedRecursiveAction() {

        protected void realCompute() throws Exception {
            FibAction f = new FibAction(8);
            assertTrue(f.cancel(true));
            assertSame(f, f.fork());
            try {
                f.get(LONG_DELAY_MS, MILLISECONDS);
                shouldThrow();
            } catch (CancellationException success) {
                checkCancelled(f);
            }
        }
    };
    testInvokeOnPool(mainPool(), a);
}
Also used : CancellationException(java.util.concurrent.CancellationException) RecursiveAction(java8.util.concurrent.RecursiveAction)

Aggregations

CancellationException (java.util.concurrent.CancellationException)505 ExecutionException (java.util.concurrent.ExecutionException)150 Test (org.junit.Test)91 TimeoutException (java.util.concurrent.TimeoutException)76 ArrayList (java.util.ArrayList)47 CountDownLatch (java.util.concurrent.CountDownLatch)46 Future (java.util.concurrent.Future)46 IOException (java.io.IOException)42 ExecutorService (java.util.concurrent.ExecutorService)30 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)29 CompletableFuture (java.util.concurrent.CompletableFuture)28 Callable (java.util.concurrent.Callable)27 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)27 List (java.util.List)24 Map (java.util.Map)23 HashMap (java.util.HashMap)22 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)22 TimeUnit (java.util.concurrent.TimeUnit)20 CharStream (org.antlr.v4.runtime.CharStream)20 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)20