Search in sources :

Example 46 with CancellationException

use of java.util.concurrent.CancellationException in project jdk8u_jdk by JetBrains.

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 {
    int s;
    long nanos = unit.toNanos(timeout);
    if (Thread.interrupted())
        throw new InterruptedException();
    if ((s = status) >= 0 && nanos > 0L) {
        long d = System.nanoTime() + nanos;
        // avoid 0
        long deadline = (d == 0L) ? 1L : d;
        Thread t = Thread.currentThread();
        if (t instanceof ForkJoinWorkerThread) {
            ForkJoinWorkerThread wt = (ForkJoinWorkerThread) t;
            s = wt.pool.awaitJoin(wt.workQueue, this, deadline);
        } else if ((s = ((this instanceof CountedCompleter) ? ForkJoinPool.common.externalHelpComplete((CountedCompleter<?>) this, 0) : ForkJoinPool.common.tryExternalUnpush(this) ? doExec() : 0)) >= 0) {
            // measure in nanosecs, but wait in millisecs
            long ns, ms;
            while ((s = status) >= 0 && (ns = deadline - System.nanoTime()) > 0L) {
                if ((ms = TimeUnit.NANOSECONDS.toMillis(ns)) > 0L && U.compareAndSwapInt(this, STATUS, s, s | SIGNAL)) {
                    synchronized (this) {
                        if (status >= 0)
                            // OK to throw InterruptedException
                            wait(ms);
                        else
                            notifyAll();
                    }
                }
            }
        }
    }
    if (s >= 0)
        s = status;
    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 47 with CancellationException

use of java.util.concurrent.CancellationException in project jdk8u_jdk by JetBrains.

the class ForkJoinTask method get.

/**
     * Waits if necessary for the computation to complete, and then
     * retrieves its result.
     *
     * @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
     */
public final V get() throws InterruptedException, ExecutionException {
    int s = (Thread.currentThread() instanceof ForkJoinWorkerThread) ? doJoin() : externalInterruptibleAwaitDone();
    Throwable ex;
    if ((s &= DONE_MASK) == CANCELLED)
        throw new CancellationException();
    if (s == EXCEPTIONAL && (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)

Example 48 with CancellationException

use of java.util.concurrent.CancellationException in project android_frameworks_base by AOSPA.

the class ViewDebug method callMethodOnAppropriateTheadBlocking.

private static Object callMethodOnAppropriateTheadBlocking(final Method method, final Object object) throws IllegalAccessException, InvocationTargetException, TimeoutException {
    if (!(object instanceof View)) {
        return method.invoke(object, (Object[]) null);
    }
    final View view = (View) object;
    Callable<Object> callable = new Callable<Object>() {

        @Override
        public Object call() throws IllegalAccessException, InvocationTargetException {
            return method.invoke(view, (Object[]) null);
        }
    };
    FutureTask<Object> future = new FutureTask<Object>(callable);
    // Try to use the handler provided by the view
    Handler handler = view.getHandler();
    // Fall back on using the main thread
    if (handler == null) {
        handler = new Handler(android.os.Looper.getMainLooper());
    }
    handler.post(future);
    while (true) {
        try {
            return future.get(CAPTURE_TIMEOUT, java.util.concurrent.TimeUnit.MILLISECONDS);
        } catch (ExecutionException e) {
            Throwable t = e.getCause();
            if (t instanceof IllegalAccessException) {
                throw (IllegalAccessException) t;
            }
            if (t instanceof InvocationTargetException) {
                throw (InvocationTargetException) t;
            }
            throw new RuntimeException("Unexpected exception", t);
        } catch (InterruptedException e) {
        // Call get again
        } catch (CancellationException e) {
            throw new RuntimeException("Unexpected cancellation exception", e);
        }
    }
}
Also used : Handler(android.os.Handler) Callable(java.util.concurrent.Callable) InvocationTargetException(java.lang.reflect.InvocationTargetException) FutureTask(java.util.concurrent.FutureTask) CancellationException(java.util.concurrent.CancellationException) AccessibleObject(java.lang.reflect.AccessibleObject) ExecutionException(java.util.concurrent.ExecutionException)

Example 49 with CancellationException

use of java.util.concurrent.CancellationException in project box-android-sdk by box.

the class BoxFutureTask method done.

@Override
protected synchronized void done() {
    BoxResponse<E> response = null;
    Exception ex = null;
    try {
        response = this.get();
    } catch (InterruptedException e) {
        ex = e;
    } catch (ExecutionException e) {
        ex = e;
    } catch (CancellationException e) {
        ex = e;
    }
    if (ex != null) {
        response = new BoxResponse<E>(null, new BoxException("Unable to retrieve response from FutureTask.", ex), mRequest);
    }
    ArrayList<OnCompletedListener<E>> listener = mCompletedListeners;
    for (OnCompletedListener<E> l : listener) {
        l.onCompleted(response);
    }
}
Also used : CancellationException(java.util.concurrent.CancellationException) ExecutionException(java.util.concurrent.ExecutionException) ExecutionException(java.util.concurrent.ExecutionException) CancellationException(java.util.concurrent.CancellationException)

Example 50 with CancellationException

use of java.util.concurrent.CancellationException in project eclipse.platform.text by eclipse.

the class ClassReferenceCodeMining method doResolve.

@Override
protected CompletableFuture<Void> doResolve(ITextViewer viewer, IProgressMonitor monitor) {
    return CompletableFuture.runAsync(() -> {
        IDocument document = viewer.getDocument();
        String className = super.getClassName();
        try {
            int wait = Integer.parseInt(className);
            try {
                for (int i = 0; i < wait; i++) {
                    monitor.isCanceled();
                    synchronized (lock) {
                        lock.wait(1000);
                    }
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        } catch (NumberFormatException e) {
        } catch (CancellationException e) {
            e.printStackTrace();
            throw e;
        }
        int refCount = 0;
        int lineCount = document.getNumberOfLines();
        for (int i = 0; i < lineCount; i++) {
            // check if request was canceled.
            monitor.isCanceled();
            String line = getLineText(document, i);
            refCount += line.contains("new " + className) ? 1 : 0;
        }
        super.setLabel(refCount + " references");
    });
}
Also used : CancellationException(java.util.concurrent.CancellationException) IDocument(org.eclipse.jface.text.IDocument)

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