completable-future

CompletableFuture reuse thread from pool

淺唱寂寞╮ 提交于 2019-12-12 17:53:50
问题 I'm testing Completable Future. As described here I thought thread would be reused from common pool but this snippet shows strange behaviour for (int i = 0; i < 10000; i++) { final int counter = i; CompletableFuture.supplyAsync(() -> { System.out.println("Looking up " + counter + " on thread " + Thread.currentThread().getName()); return null; }); } I have that kind of output: Looking up 0 on thread Thread-2 Looking up 1 on thread Thread-3 Looking up 2 on thread Thread-4 ... Looking up 10000

Non-intuitive object eviction from garbage collection

橙三吉。 提交于 2019-12-12 14:16:13
问题 I'm debugging a memory leak and had to dive into CompletableFuture internals. There is this piece of code (CompletableFuture.uniComposeStage): CompletableFuture<V> g = f.apply(t).toCompletableFuture(); ... CompletableFuture<V> d = new CompletableFuture<V>(); UniRelay<V> copy = new UniRelay<V>(d, g); g.push(copy); copy.tryFire(SYNC); return d; The code itself is quite clear to me: apply a function that returns CompletionStage ( g ), create a relay that eventually will transfer value to another

What advantage is there to using Spring @Async vs. CompleteableFuture directly?

不问归期 提交于 2019-12-12 07:35:44
问题 What's the advantage of using Spring Async vs. Just returning the CompletableFuture on your own? 回答1: Your application is managed by the container. Since it's discouraged to spawn Thread s on you own, you can let the container inject a managed Executor . @Service class MyService { @Autowired private Executor executor; public CompletableFuture<?> compute() { return CompletableFuture.supplyAsync(() -> /* compute value */, executor); } } 回答2: There is no “ vs. ” between the two: these are

CompletableFuture: several tasks

三世轮回 提交于 2019-12-11 16:43:06
问题 How can I asynchronously execute 20 Runnable tasks(or 1 task 20 times), using 5 CompletableFutures? That's what I've got: Runnable task = () -> { long startTime = System.currentTimeMillis(); Random random = new Random(); while (System.currentTimeMillis() - startTime < 3000) { DoubleStream.generate(() -> random.nextDouble()) .limit(random.nextInt(100)) .map(n -> Math.cos(n)) .sum(); } System.out.println("Done"); }; for (int i = 0; i < 4; i++) { CompletableFuture<Void> future1 =

Can API Request (GET Call) return a response to client and start a background task to finish request

本秂侑毒 提交于 2019-12-11 14:26:48
问题 I am using Spring Boot 1.4 and Java8. I want to know is it possible that if I receive a get request for an API in controller. I immediately return a response to the client and then create a background task for the request (that handle success and exception scenarios). I understand we can use completablefuture for async processing, but still from controller method for this API we generally send the response after using thenapply, exceptionally or get. That means though we have spawned a new

What is java CompletableFuture equivalent of scala Future rescue and handle

旧巷老猫 提交于 2019-12-11 05:57:32
问题 I see that CompletableFuture has a method handle that is the same as that of scala Future 's handle basically converting success and exceptions all to success to be map and flatMap upstream(or thenApply and thenCompose in java world). What is the equivalent of twitter future rescue (or scala future recoverWith ) in java though? rescue in scala is basically like the old java try....catch , then rethrow with more information so it can be nice to use. For example in twitterFuture.handle or

How to debug CompletableStage deadlocks?

こ雲淡風輕ζ 提交于 2019-12-11 05:04:04
问题 The most difficult debugging problem I've run across recently is deadlocks between asynchronous operations. For example, given two CompletionStage chains, where the first chain invokes a method that depends upon the completion of the second chain, and the second chain invokes a method that depends upon the completion of the first chain. It isn't this obvious in real-life because the dependency tends to be hidden and sometimes deadlocks involve more than three parties. Part of the problem is

CompletableFutures and filtering based on values that are inside

丶灬走出姿态 提交于 2019-12-11 04:32:53
问题 I'm in a bit of confusion right now, so I have a method that should return CompletableFuture<List<A>> inside the method is: CompletableFuture<List<String>> toReturn = asyncCall().thenApply(....) .thenCompose(listOfStuff -> convertToList(listOfStuff.stream().map( key -> asyncCall2(key) .thenApply(optionalValue -> optionalValue.orElse(null)) ).collect(Collectors.toList())); and convertToList() simply joins futures to convert CompletableFuture<List<ComputableFuture<A>>> into CompletableFuture

spring hibernate Async task issue No Session found for current thread

烈酒焚心 提交于 2019-12-10 21:03:56
问题 This is my method to save data . It is working fine public Future<SocialLogin> loginUserSocial(Social model) { Session session = this.sessionFactory.getCurrentSession(); session.save(model); SocialLogin dto = new SocialLogin(); dto.setUser_id(model.getUser_id()); return new AsyncResult<SocialLogin>(dto); } But if I put @Async annotion on method I've got the following exception. org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.util

Using thenAccept after supplyAsync blocks the main thread

杀马特。学长 韩版系。学妹 提交于 2019-12-10 18:23:26
问题 I am developing a web application which communicates with other web applications. From time to time, my system sends HTTP request as notification to other systems. Since their responses are not essential to me, I send the requests with Java 8 CompletableFuture supplyAsync and prints their responses with thenAccept so that my main thread will not get blocked. However, I found the CompletableFuture function chains took around 100 to 200 ms each time, which confused me because from my