completable-future

How to interrupt underlying execution of CompletableFuture

£可爱£侵袭症+ 提交于 2019-11-30 08:04:26
I know that CompletableFuture design does not control its execution with interruptions, but I suppose some of you might have this problem. CompletableFuture s are very good way to compose async execution, but given the case when you want the underlying execution to be interrupted or stopped when future is canceled, how do we do that? Or we must just accept that any canceled or manually completed CompletableFuture will not impact the thread working out there to complete it? That is, in my opinion, obviously a useless work that takes time of executor worker. I wonder what approach or design

Java 8 Completable Futures allOf different data types

穿精又带淫゛_ 提交于 2019-11-30 07:11:38
I have 3 CompletableFutures all 3 returning different data types. I am looking to create a result object that is a composition of the result returned by all the 3 futures. So my current working code looks like this: public ClassD getResultClassD() { ClassD resultClass = new ClassD(); CompletableFuture<ClassA> classAFuture = CompletableFuture.supplyAsync(() -> service.getClassA() ); CompletableFuture<ClassB> classBFuture = CompletableFuture.supplyAsync(() -> service.getClassB() ); CompletableFuture<ClassC> classCFuture = CompletableFuture.supplyAsync(() -> service.getClassC() );

CompletableFuture#whenComplete not called if thenApply is used

荒凉一梦 提交于 2019-11-30 05:15:00
问题 I have the following code (resulting from my previous question) that schedules a task on a remote server, and then polls for completion using ScheduledExecutorService#scheduleAtFixedRate . Once the task is complete, it downloads the result. I want to return a Future to the caller so they can decide when and how long to block, and give them the option to cancel the task. My problem is that if the client cancels the Future returned by the download method, whenComplete block doesn't execute. If

CompletableFuture/parallelStream in JavaEE app server

删除回忆录丶 提交于 2019-11-30 03:45:30
问题 Given the new Java8 we are getting really nice features for asynchronous tasks, e.g. CompletableFuture and .paralellStream(). If you run this in Java SE as I've understood it you will utilize the ForkJoinPool, but what happens if I run the following examples in e.g. Wildfly or TomcatEE? //Here I start a comp.Future without giving an Executor test = CompletableFuture.supplyAsync(() -> timeConsumingMethod()); //Here I start a parallel stream mList.paralell().filter(...).collect(Collectors

CompletableFuture allof(..).join() vs CompletableFuture.join()

你说的曾经没有我的故事 提交于 2019-11-29 12:54:09
I am currently using CompletableFuture supplyAsync() method for submitting some tasks to common thread pool. Here is what code snippet looks like: final List<CompletableFuture<List<Test>>> completableFutures = resolvers.stream() .map(resolver -> supplyAsync(() -> task.doWork())) .collect(toList()); CompletableFuture.allOf(completableFutures.toArray(new CompletableFuture[completableFutures.size()])).join(); final List<Test> tests = new ArrayList<>(); completableFutures.stream() .map(completableFuture -> completableFuture.getNow()) .forEach(tests::addAll); I would like to know how below differs

CompletableFuture / ForkJoinPool Set Class Loader

做~自己de王妃 提交于 2019-11-29 12:17:09
问题 I tackled down a very specific problem, whose solution seems to be something basic: My (Spring) application's classloader hierarchy is something like this: SystemClassLoader -> PlatformClassLoader -> AppClassLoader If I use Java CompleteableFuture to run threads. the ContextClassLoader of the threads is: SystemClassLoader -> PlatformClassLoader -> ThreadClassLoader Thus, I cannot access any class in AppClassLoader although I have to because all external library classes reside there. The

How to interrupt underlying execution of CompletableFuture

天大地大妈咪最大 提交于 2019-11-29 11:31:38
问题 I know that CompletableFuture design does not control its execution with interruptions, but I suppose some of you might have this problem. CompletableFuture s are very good way to compose async execution, but given the case when you want the underlying execution to be interrupted or stopped when future is canceled, how do we do that? Or we must just accept that any canceled or manually completed CompletableFuture will not impact the thread working out there to complete it? That is, in my

Java 8 Completable Futures allOf different data types

前提是你 提交于 2019-11-29 09:45:25
问题 I have 3 CompletableFutures all 3 returning different data types. I am looking to create a result object that is a composition of the result returned by all the 3 futures. So my current working code looks like this: public ClassD getResultClassD() { ClassD resultClass = new ClassD(); CompletableFuture<ClassA> classAFuture = CompletableFuture.supplyAsync(() -> service.getClassA() ); CompletableFuture<ClassB> classBFuture = CompletableFuture.supplyAsync(() -> service.getClassB() );

How to cancel Java 8 completable future?

走远了吗. 提交于 2019-11-29 05:38:43
I am playing with Java 8 completable futures. I have the following code: CountDownLatch waitLatch = new CountDownLatch(1); CompletableFuture<?> future = CompletableFuture.runAsync(() -> { try { System.out.println("Wait"); waitLatch.await(); //cancel should interrupt System.out.println("Done"); } catch (InterruptedException e) { System.out.println("Interrupted"); throw new RuntimeException(e); } }); sleep(10); //give it some time to start (ugly, but works) future.cancel(true); System.out.println("Cancel called"); assertTrue(future.isCancelled()); assertTrue(future.isDone()); sleep(100); //give

Chaining several CompletionStage only if a condition is achieved

◇◆丶佛笑我妖孽 提交于 2019-11-29 03:03:12
问题 I have several CompletionStage methods that I'd like to chain. The problem is that the result of the first one will determine if the next ones should be executed. Right now the only way to achieve this seems to be passing "special" arguments to next CompletionStage so it doesn't execute the full code. For example: public enum SomeResult { RESULT_1, RESULT_2, RESULT_3 } public CompletionStage<SomeResult> someMethod(SomeArgument someArgument) { return CompletableFuture.supplyAsync(() -> { //