completable-future

Java: Serialize object to file asynchronously

落花浮王杯 提交于 2019-12-08 05:06:44
问题 I am making a Minecraft clone, and I have a Map<Vector3i, Chunk> called chunks that stores all the loaded chunks. Every frame, this loop runs: for(Vector3i v:chunksToUnrender){ ... CompletableFuture.runAsync(() -> { try { chunks.get(v).toFile(new File(String.format("res-server/regions/%s.%s.%s.mcr", v.x, v.y, v.z))); synchronized (chunksStateGuard) { chunks.remove(v); } } catch(IOException e) { e.printStackTrace(); System.err.println("Unable to save chunk " + Utils.format3i(v)); } }); } The

Exception propagation in java.util.concurrent.CompletableFuture

懵懂的女人 提交于 2019-12-07 14:56:09
问题 There are two snippets of code. In the first one we create the CompletableFuture from the task which always throws some exception. Then we apply "exceptionally" method to this future, then "theAccept" method. We DO NOT assign new future returned by theAccept method to any variable. Then we invoke "join" on original future. What we see is that "exceptionally" method has been invoked as well as the "thenAccept". We see It because they printed appropriate lines in output. But the Exception has

CompletionStage: Why is allOf or anyOf defined in CompletableFuture [closed]

时光怂恿深爱的人放手 提交于 2019-12-07 09:48:59
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 3 years ago . I have a framework that uses the interface CompletionStage and I'm curious why the helper methods anyOf or allOf found in CompletableFuture are defined there. Seems like they should be operating on the interfaces rather than the implementation ? I'm quite dissatisfied with

How to make a future that gets completed when any of the given CompletableFutures is completed with a result that matches a certain predicate?

徘徊边缘 提交于 2019-12-06 14:12:13
I have a list of CompletableFuture<MyService> : List<CompletableFuture<MyService>> where MyService is immutable like the following: public class MyService { public boolean isAvailable() { return true; // or false } } I now want a future that is completed when one of the futures: is completed; and for the MyService instance as provided by that future: MyService.isAvailable() returns true When proceeding, I need to have the MyService instance that is available. In other words, I want a CompletableFuture<MyService> which completes when the two conditions are met. How can I do this? I know I can

Return a CompletableFuture without exposing executor thread

对着背影说爱祢 提交于 2019-12-06 12:49:18
问题 I expose a method in a library, which returns a CompletableFuture. That method's computation happens on a single-threaded Executor which is my bottleneck, hence I do not want any subsequent work to happen on the same thread. If I use the simple approach of returning the result of "supplyAsync", I'll be exposing my precious thread to the callers, who may be adding synchronous operations (e.g. via thenAccept) which could take some CPU time on that thread. Repro below: import java.util

How to turn a List<Item> to a Map<Item, completeablefuture<xyz>>

…衆ロ難τιáo~ 提交于 2019-12-06 06:08:12
I have an async method with a completeablefuture result: public CompletableFuture<DogLater> asyncDogLater(String dogName){} I have a list of dogs: List<Dog> dogs; Now, I want to create a map from the dog's name to the Completeablefuture: Map<String, CompletableFuture<DogLater>> map; After checking this and this I was trying to do so: Map<String, CompletableFuture<DogLater>> completableFutures = dogs.stream() .collect( Collectors.toMap(Dog::getName, asyncDogLater(Dog::getName ))); But the compiler complains that the first Dog::getName is problematic since: Non-static method cannot be referenced

Exception propagation in java.util.concurrent.CompletableFuture

Deadly 提交于 2019-12-06 03:38:54
There are two snippets of code. In the first one we create the CompletableFuture from the task which always throws some exception. Then we apply "exceptionally" method to this future, then "theAccept" method. We DO NOT assign new future returned by theAccept method to any variable. Then we invoke "join" on original future. What we see is that "exceptionally" method has been invoked as well as the "thenAccept". We see It because they printed appropriate lines in output. But the Exception has not been suppressed by "exceptionally" method. Suppress exception and provide us with some default value

Spring promoting request scoped bean to child threads (HttpServletRequest)

馋奶兔 提交于 2019-12-05 20:48:40
问题 I tried a lot of things now but i seem to miss a piece of the puzzle. Here is the story: I have a request scoped bean that reads some SessionContext from the HttpServletRequest. This attribute is set in a filter. So this is working absolutely fine while the code runs on the correct thread. @Component @Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.INTERFACES) public class SessionContextProviderImpl implements SessionContextProvider<SessionContext> { private

Execute a for loop in parallel using CompletableFuture in Java and log the execution

僤鯓⒐⒋嵵緔 提交于 2019-12-05 18:34:15
I have a for loop which I am trying to parallelize using CompletableFuture. for (int i = 0; i < 10000; i++) { doSomething(); doSomethingElse(); } What I have till now is: for (int i = 0; i < 10000; i++) { CompletableFuture.runAsync(() -> doSomething()); CompletableFuture.runAsync(() -> doSomethingElse()); } I guess this serves the purpose but there is a requirement to print log just before the start and end of all the processing. If I do this: log("Started doing things"); for (int i = 0; i < 10000; i++) { CompletableFuture.runAsync(() -> doSomething()); CompletableFuture.runAsync(() ->

CompletionStage: Why is allOf or anyOf defined in CompletableFuture [closed]

孤街浪徒 提交于 2019-12-05 18:11:55
Closed . This question is opinion-based . It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post . Closed 3 years ago . I have a framework that uses the interface CompletionStage and I'm curious why the helper methods anyOf or allOf found in CompletableFuture are defined there. Seems like they should be operating on the interfaces rather than the implementation ? I'm quite dissatisfied with the CompletionStage interface thus far. Are there other Java libraries that are CompletionStage