Search in sources :

Example 76 with CancellationException

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

the class ForkJoinTaskTest method testCancelledInvokeSingleton.

/**
 * invoke task throws exception when task cancelled
 */
public void testCancelledInvokeSingleton() {
    @SuppressWarnings("serial") RecursiveAction a = new CheckedRecursiveAction() {

        protected void realCompute() {
            AsyncFib f = new AsyncFib(8);
            assertTrue(f.cancel(true));
            try {
                f.invoke();
                shouldThrow();
            } catch (CancellationException success) {
                checkCancelled(f);
            }
        }
    };
    testInvokeOnPool(singletonPool(), a);
}
Also used : CancellationException(java.util.concurrent.CancellationException) RecursiveAction(java8.util.concurrent.RecursiveAction)

Example 77 with CancellationException

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

the class ForkJoinTaskTest method testCancelledForkTimedGetSingleton.

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

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

Example 78 with CancellationException

use of java.util.concurrent.CancellationException in project robotcode by OutoftheBoxFTC.

the class MonitoredUsbDeviceConnection method controlTransfer.

public int controlTransfer(int requestType, int request, int value, int index, byte[] buffer, int offset, int length, int timeout) throws RobotUsbException {
    synchronized (// concurrency paranoia
    monitor) {
        this.callResult = FtDevice.RC_PARANOIA;
        try {
            if (acquireUsbWriteLock()) {
                try {
                    this.requestType = requestType;
                    this.request = request;
                    this.value = value;
                    this.index = index;
                    this.buffer = buffer;
                    this.offset = offset;
                    this.length = length;
                    this.timeout = timeout;
                    failureType = FailureType.CONTROL_TRANSFER;
                    RobotUsbException timedOutException = monitor.monitor(controlTransferAction, failureAction, msUsbWriteDurationMax, TimeUnit.MILLISECONDS);
                    if (timedOutException != null) {
                        throw timedOutException;
                    }
                } catch (ExecutionException | CancellationException e) {
                    // We should have ruled out all of these cases with the above logic
                    throw RobotUsbUnspecifiedException.createChained(e, "control transfer: internal error: unexpected exception from future");
                } finally {
                    releaseUsbWriteLock();
                }
            } else {
                throw unableToAcquireWriteLockException();
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        } catch (RuntimeException e) {
            throw RobotUsbFTDIException.createChained(e, "runtime exception %s during controlTransfer() of %d bytes on %s", e.getClass().getSimpleName(), length, serialNumber);
        }
        return callResult;
    }
}
Also used : RobotUsbException(org.firstinspires.ftc.robotcore.internal.usb.exception.RobotUsbException) CancellationException(java.util.concurrent.CancellationException) ExecutionException(java.util.concurrent.ExecutionException)

Example 79 with CancellationException

use of java.util.concurrent.CancellationException in project h2database by h2database.

the class LazyFuture method get.

@Override
public T get() throws InterruptedException, ExecutionException {
    switch(state) {
        case S_READY:
            try {
                result = run();
                state = S_DONE;
            } catch (Exception e) {
                error = e;
                if (e instanceof InterruptedException) {
                    throw (InterruptedException) e;
                }
                throw new ExecutionException(e);
            } finally {
                if (state != S_DONE) {
                    state = S_ERROR;
                }
            }
            return result;
        case S_DONE:
            return result;
        case S_ERROR:
            throw new ExecutionException(error);
        case S_CANCELED:
            throw new CancellationException();
        default:
            throw DbException.throwInternalError("" + state);
    }
}
Also used : CancellationException(java.util.concurrent.CancellationException) ExecutionException(java.util.concurrent.ExecutionException) ExecutionException(java.util.concurrent.ExecutionException) CancellationException(java.util.concurrent.CancellationException) DbException(org.h2.message.DbException)

Example 80 with CancellationException

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

the class RealTimeServerConnection method connect.

/**
 * Returns a message once a change has been detected or error occurs. Otherwise this method will continue reconnecting.
 * @return A BoxSimpleMessage with a simple message indicating a change in the user's account.
 */
public BoxSimpleMessage connect() {
    mRetries = 0;
    try {
        BoxIteratorRealTimeServers servers = (BoxIteratorRealTimeServers) mRequest.send();
        mBoxRealTimeServer = servers.get(0);
    } catch (BoxException e) {
        mChangeListener.onException(e, this);
        return null;
    }
    BoxRequestsEvent.LongPollMessageRequest messageRequest = new BoxRequestsEvent.LongPollMessageRequest(mBoxRealTimeServer.getUrl(), mSession);
    messageRequest.setTimeOut(mBoxRealTimeServer.getFieldRetryTimeout().intValue() * 1000);
    boolean shouldRetry = true;
    do {
        BoxFutureTask<BoxSimpleMessage> task = null;
        try {
            task = messageRequest.toTask().addOnCompletedListener(this);
            mExecutor.submit(task);
            BoxResponse<BoxSimpleMessage> response = task.get(mBoxRealTimeServer.getFieldRetryTimeout().intValue(), TimeUnit.SECONDS);
            if (response.isSuccess() && !response.getResult().getMessage().equals(BoxSimpleMessage.MESSAGE_RECONNECT)) {
                return response.getResult();
            }
        } catch (TimeoutException e) {
            if (task != null) {
                try {
                    // if the timeout is coming from the task then cancel the task (as httpurlconnection timeout is unreliable).
                    task.cancel(true);
                } catch (CancellationException e1) {
                }
            }
        } catch (InterruptedException e) {
            mChangeListener.onException(e, this);
        } catch (ExecutionException e) {
            mChangeListener.onException(e, this);
        }
        mRetries++;
        if (mBoxRealTimeServer.getMaxRetries() < mRetries) {
            shouldRetry = false;
        }
    } while (shouldRetry);
    mChangeListener.onException(new BoxException.MaxAttemptsExceeded("Max retries exceeded, ", mRetries), this);
    return null;
}
Also used : BoxException(com.box.androidsdk.content.BoxException) BoxRequestsEvent(com.box.androidsdk.content.requests.BoxRequestsEvent) BoxIteratorRealTimeServers(com.box.androidsdk.content.models.BoxIteratorRealTimeServers) BoxSimpleMessage(com.box.androidsdk.content.models.BoxSimpleMessage) CancellationException(java.util.concurrent.CancellationException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) SocketTimeoutException(java.net.SocketTimeoutException)

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