Search in sources :

Example 11 with CancellationException

use of java.util.concurrent.CancellationException in project guava by google.

the class FuturesTest method testTransformAsync_interruptPropagatesToTransformingThread.

// threads
@GwtIncompatible
public void testTransformAsync_interruptPropagatesToTransformingThread() throws Exception {
    SettableFuture<String> input = SettableFuture.create();
    final CountDownLatch inFunction = new CountDownLatch(1);
    final CountDownLatch shouldCompleteFunction = new CountDownLatch(1);
    final CountDownLatch gotException = new CountDownLatch(1);
    AsyncFunction<String, String> function = new AsyncFunction<String, String>() {

        @Override
        public ListenableFuture<String> apply(String s) throws Exception {
            inFunction.countDown();
            try {
                shouldCompleteFunction.await();
            } catch (InterruptedException expected) {
                gotException.countDown();
                throw expected;
            }
            return immediateFuture("a");
        }
    };
    ListenableFuture<String> futureResult = transformAsync(input, function, newSingleThreadExecutor());
    input.set("value");
    inFunction.await();
    futureResult.cancel(true);
    shouldCompleteFunction.countDown();
    try {
        futureResult.get();
        fail();
    } catch (CancellationException expected) {
    }
    // TODO(cpovirk): implement interruption, updating this test:
    // https://github.com/google/guava/issues/1989
    assertEquals(1, gotException.getCount());
// gotException.await();
}
Also used : CancellationException(java.util.concurrent.CancellationException) CountDownLatch(java.util.concurrent.CountDownLatch) GwtIncompatible(com.google.common.annotations.GwtIncompatible)

Example 12 with CancellationException

use of java.util.concurrent.CancellationException in project guava by google.

the class FuturesTest method testImmediateFailedFuture_cancellationException.

public void testImmediateFailedFuture_cancellationException() throws Exception {
    CancellationException exception = new CancellationException();
    ListenableFuture<String> future = immediateFailedFuture(exception);
    assertFalse(future.isCancelled());
    try {
        getDone(future);
        fail();
    } catch (ExecutionException expected) {
        assertSame(exception, expected.getCause());
    }
    try {
        getDoneFromTimeoutOverload(future);
        fail();
    } catch (ExecutionException expected) {
        assertSame(exception, expected.getCause());
    }
}
Also used : CancellationException(java.util.concurrent.CancellationException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ExecutionException(java.util.concurrent.ExecutionException)

Example 13 with CancellationException

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

the class DistributionInstaller method withAsyncDownload.

private void withAsyncDownload(final URI address, final File destination, final OperationDescriptor operationDescriptor) throws Throwable {
    currentListener.set(buildProgressListener);
    try {
        // Start the download in another thread and wait for the result
        Thread thread = new Thread("Distribution download") {

            @Override
            public void run() {
                try {
                    new Download(new Logger(false), new ForwardingDownloadProgressListener(operationDescriptor), APP_NAME, GradleVersion.current().getVersion()).download(address, destination);
                } catch (Throwable t) {
                    synchronized (lock) {
                        failure = t;
                    }
                } finally {
                    synchronized (lock) {
                        completed = true;
                        lock.notifyAll();
                    }
                }
            }
        };
        thread.setDaemon(true);
        thread.start();
        synchronized (lock) {
            while (!completed && !cancelled) {
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    throw UncheckedException.throwAsUncheckedException(e);
                }
            }
            if (failure != null) {
                throw failure;
            }
            if (cancelled) {
                // When cancelled, try to stop the download thread but don't attempt to wait for it to complete
                // Could possibly loop here for a while trying to force the thread to exit
                thread.interrupt();
                throw new CancellationException();
            }
        }
    } finally {
        // The download thread may still be running. Ignore any further status events from it
        currentListener.set(NO_OP);
    }
}
Also used : CancellationException(java.util.concurrent.CancellationException) Logger(org.gradle.wrapper.Logger) ProgressLogger(org.gradle.internal.logging.progress.ProgressLogger) IDownload(org.gradle.wrapper.IDownload) Download(org.gradle.wrapper.Download)

Example 14 with CancellationException

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

the class StartProcessingJobOperation method run.

@Override
public void run() throws Exception {
    MapReduceService mapReduceService = getService();
    if (mapReduceService.unregisterJobSupervisorCancellation(name, jobId)) {
        // Supervisor was cancelled prior to creation
        AbstractJobTracker jobTracker = (AbstractJobTracker) mapReduceService.getJobTracker(name);
        TrackableJobFuture future = jobTracker.unregisterTrackableJob(jobId);
        if (future != null) {
            Exception exception = new CancellationException("Operation was cancelled by the user");
            future.setResult(exception);
        }
        return;
    }
    JobSupervisor supervisor = mapReduceService.getJobSupervisor(name, jobId);
    if (supervisor == null) {
        return;
    }
    // Create actual mapping operation
    MappingPhase mappingPhase = new KeyValueSourceMappingPhase(keys, predicate);
    supervisor.startTasks(mappingPhase);
}
Also used : MappingPhase(com.hazelcast.mapreduce.impl.task.MappingPhase) KeyValueSourceMappingPhase(com.hazelcast.mapreduce.impl.task.KeyValueSourceMappingPhase) TrackableJobFuture(com.hazelcast.mapreduce.impl.task.TrackableJobFuture) KeyValueSourceMappingPhase(com.hazelcast.mapreduce.impl.task.KeyValueSourceMappingPhase) CancellationException(java.util.concurrent.CancellationException) MapReduceService(com.hazelcast.mapreduce.impl.MapReduceService) AbstractJobTracker(com.hazelcast.mapreduce.impl.AbstractJobTracker) CancellationException(java.util.concurrent.CancellationException) IOException(java.io.IOException) JobSupervisor(com.hazelcast.mapreduce.impl.task.JobSupervisor)

Example 15 with CancellationException

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

the class KeyValueJobOperation method run.

@Override
public void run() throws Exception {
    MapReduceService mapReduceService = getService();
    Address jobOwner = getCallerAddress();
    if (jobOwner == null) {
        jobOwner = getNodeEngine().getThisAddress();
    }
    // Inject managed context
    injectManagedContext(mapper, combinerFactory, reducerFactory, keyValueSource);
    // Build immutable configuration
    JobTaskConfiguration config = new JobTaskConfiguration(jobOwner, getNodeEngine(), chunkSize, name, jobId, mapper, combinerFactory, reducerFactory, keyValueSource, communicateStats, topologyChangedStrategy);
    JobSupervisor supervisor = mapReduceService.createJobSupervisor(config);
    if (supervisor == null) {
        // Supervisor was cancelled prior to creation
        AbstractJobTracker jobTracker = (AbstractJobTracker) mapReduceService.getJobTracker(name);
        TrackableJobFuture future = jobTracker.unregisterTrackableJob(jobId);
        if (future != null) {
            Exception exception = new CancellationException("Operation was cancelled by the user");
            future.setResult(exception);
        }
    }
}
Also used : TrackableJobFuture(com.hazelcast.mapreduce.impl.task.TrackableJobFuture) Address(com.hazelcast.nio.Address) CancellationException(java.util.concurrent.CancellationException) MapReduceService(com.hazelcast.mapreduce.impl.MapReduceService) AbstractJobTracker(com.hazelcast.mapreduce.impl.AbstractJobTracker) JobTaskConfiguration(com.hazelcast.mapreduce.impl.task.JobTaskConfiguration) JobSupervisor(com.hazelcast.mapreduce.impl.task.JobSupervisor) CancellationException(java.util.concurrent.CancellationException) IOException(java.io.IOException)

Aggregations

CancellationException (java.util.concurrent.CancellationException)196 ExecutionException (java.util.concurrent.ExecutionException)88 TimeoutException (java.util.concurrent.TimeoutException)50 Test (org.junit.Test)34 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)25 IOException (java.io.IOException)24 CountDownLatch (java.util.concurrent.CountDownLatch)23 Future (java.util.concurrent.Future)23 ArrayList (java.util.ArrayList)18 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)17 Callable (java.util.concurrent.Callable)16 ExecutorService (java.util.concurrent.ExecutorService)13 File (java.io.File)11 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 InvocationTargetException (java.lang.reflect.InvocationTargetException)7 List (java.util.List)7 CompletableFuture (java.util.concurrent.CompletableFuture)7 FutureTask (java.util.concurrent.FutureTask)7 Handler (android.os.Handler)6 GwtIncompatible (com.google.common.annotations.GwtIncompatible)6