Comments (4)
It's not related to the library, but you may do smth. like this:
CountDownLatch doneInAnyWay = new CountDownLatch(1);
Promise<Void> task = CompletableTask.runAsync(() -> {
try{
//...
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new CompletionException(e);
} finally {
doneInAnyWay.countDown();
}
}, EXECUTOR);
task.cancel(true);
//wait for interruption OR normal OR exceptional completion
doneInAnyWay.await();
from tascalate-concurrent.
Thank you,
I've created this implementation i'd like to share:
I've not managed to create a InterruptableTaks
, but only an InterruptableFuture
that will suffice for my project
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Supplier;
import net.tascalate.concurrent.CompletableTask;
import net.tascalate.concurrent.Promise;
public class InterruptableFuture<T> extends CompletableFuture<T> {
public static InterruptableFuture<Void> runAsync(Runnable runnable, Executor executor) {
return submit(() -> {
runnable.run();
return null;
}, executor);
}
public static <U> InterruptableFuture<U> submit(Callable<U> callable, Executor executor) {
return new InterruptableFuture<>(callable, executor);
}
public static <U> InterruptableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor) {
return new InterruptableFuture<>(supplier::get, executor);
}
private final Promise<Void> task;
private final AtomicBoolean running = new AtomicBoolean(false);
protected InterruptableFuture(Callable<T> callable, Executor executor) {
task = CompletableTask.runAsync(() -> {
if (running.compareAndSet(false, true)) {
try {
complete(callable.call());
} catch (Exception e) {
completeExceptionally(e);
}
}
// if tryLock fail cancel has been already called
}, executor);
}
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
boolean cancelled = task.cancel(mayInterruptIfRunning);
if (cancelled && running.compareAndSet(false, true)) {
// task not started yet
super.cancel(false);
}
return cancelled;
}
}
from tascalate-concurrent.
AtomicBoolean.compareAndSet will be a better choice than ReentantLock, IMHO.
from tascalate-concurrent.
You're right! I've updated the code 👍
from tascalate-concurrent.
Related Issues (20)
- Issues with Callbacks HOT 11
- OnTimeout not working when computing intensive task HOT 5
- orTime and submit combination HOT 5
- How to wrap a Promise into Promise.retry HOT 5
- Typo in DelayPolicy#withMinDelay HOT 1
- Feature enhancement on cancel HOT 2
- A FutureLocal.java would be nice(like ThreadLocal.java but more) HOT 1
- MultitargetException swallows Exceptions HOT 7
- Include class names in the documentation when highlighting a method
- Unexpected cancellation behavior HOT 6
- Promise.onTimeout does not cancel the async call in original CompletableTask.supplyAsync? HOT 8
- Promises#all(CompletionStage<? extends T>...) cancellation does not seem to work correctly HOT 5
- The cancel is not working on the async chain HOT 3
- onTimeout behaviour confusing after 0.8.4 HOT 2
- Optimized Same Executor Runs HOT 1
- Mapped promise can't be interrupted HOT 1
- Exceptions in thenCompose() will not propagate HOT 3
- Promises.all - List type argument HOT 3
- whenCompleteAsync adds the failure exception as a suppressed exception to itself HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tascalate-concurrent.