Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.concurrent.CompletableFuture;
- import java.util.concurrent.Executor;
- import java.util.concurrent.Executors;
- public class Test {
- public static void main(String argv[]) throws Exception {
- int nThreads = 1;
- Executor executor = Executors.newFixedThreadPool( nThreads );
- CompletableFuture.completedFuture(true)
- .thenComposeAsync((unused)->{
- System.err.println("About to enqueue task");
- CompletableFuture<Boolean> innerFuture = new CompletableFuture<>();
- executor.execute(() -> {
- // pretend this is some really expensive computation done asynchronously
- System.err.println("Inner task");
- innerFuture.complete(true);
- });
- System.err.println("Task enqueued");
- return innerFuture;
- }, executor).get();
- // This is never reached. The inner runnable (with pretend... in its comment) is
- // starved because the single threaded executor is blocking in the call to thenComposeAsync
- System.err.println("All done");
- System.exit(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement