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();
}
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());
}
}
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);
}
}
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);
}
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);
}
}
}
Aggregations