java.util.concurrent

Async method followed by a parallelly executed method in Java 8

ぐ巨炮叔叔 提交于 2021-02-19 07:56:05
问题 After spending the day of learning about the java Concurrency API, I still dont quite get how could I create the following functionality with the help of CompletableFuture and ExecutorService classes: When I get a request on my REST endpoint I need to: Start an asynchronous task (includes DB query, filtering, etc.), which will give me a list of String URLs at the end In the meanwhile, responde back to the REST caller with HTTP OK, that the request was received, I'm working on it When the

Are there any drawbacks with ConcurrentHashMap?

好久不见. 提交于 2021-02-19 01:31:55
问题 I need a HashMap that is accessible from multiple threads. There are two simple options, using a normal HashMap and synchronizing on it or using a ConcurrentHashMap. Since ConcurrentHashMap does not block on read operations it seems much better suited for my needs (almost exclusively reads, almost never updates). On the other hand, I expect very low concurrency anyway, so there should be no blocking (just the cost of managing the lock). The Map will also be very small (under ten entries), if

Thread::yield vs Thread::onSpinWait

六月ゝ 毕业季﹏ 提交于 2021-02-18 05:33:15
问题 Well the title basically says it all, with the small addition that I would really like to know when to use them. And it might be simple enough - I've read the documentation for them both, still can't tell the difference much. There are answers like this here that basically say: Yielding also was useful for busy waiting... I can't agree much with them for the simple reason that ForkJoinPool uses Thread::yield internally and that is a pretty recent addition in the jdk world. The thing that

How to stop Callable tasks in Executor service if exception occur

天大地大妈咪最大 提交于 2021-01-29 08:51:10
问题 I'm trying to implement a sample application to test Callable and ExecutorService interfaces. In my app I have: @Bean("fixedThreadPool") public ExecutorService fixedThreadPool() { return Executors.newFixedThreadPool(5); } Then: public void removeUserIds(Set<String> userIds) { UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("http://localhost:8080/remove"); final List<Callable<String>> callables = new ArrayList<>(); userIds.forEach(userId -> { final Callable<String> task = ()

How to handle uncaught exceptions from CompletableFuture.runAsync

你离开我真会死。 提交于 2021-01-28 08:55:49
问题 Our application has some code that runs asynchronously that is failing. Like this: CompletableFuture.runAsync( () -> { throw new RuntimeException("bad"); }, executorService ); We want default exception handling code that can catch these errors, in case specific uses forget to handle exceptions (this came from a production bug). This is apparently tricky. The answer given in Handling exceptions from Java ExecutorService tasks does not work. It relies on the task being a Future<?> and then

Timeout while waiting for a batch of Futures to complete?

纵然是瞬间 提交于 2020-12-30 06:50:27
问题 I have a set of Futures created by submitting Callable s to an Executor . Pseudo code: for all tasks futures.add(executor.submit(new callable(task))) Now I'd like to get all futures waiting at most n seconds until all complete. I know I can call Future#get(timeout) but if I call that sequentially for all my futures in a loop the timouts start adding up. Pseudo code: for all futures future.get(timeout) get blocks with a timeout until the result is ready. Therefore, if the first completes just

Timeout while waiting for a batch of Futures to complete?

|▌冷眼眸甩不掉的悲伤 提交于 2020-12-30 06:48:40
问题 I have a set of Futures created by submitting Callable s to an Executor . Pseudo code: for all tasks futures.add(executor.submit(new callable(task))) Now I'd like to get all futures waiting at most n seconds until all complete. I know I can call Future#get(timeout) but if I call that sequentially for all my futures in a loop the timouts start adding up. Pseudo code: for all futures future.get(timeout) get blocks with a timeout until the result is ready. Therefore, if the first completes just

Why CompletableFuture.runAsync is not executed?

坚强是说给别人听的谎言 提交于 2020-12-27 05:44:38
问题 I consider that main thread must end after sub thread.But below code shows the process finished before print the "async end".What is the reason?Can anybody explain?Thx. import java.util.concurrent.CompletableFuture; public class Test { public static void main(String[] args) { CompletableFuture.runAsync(() -> { try { System.out.println("async start"); Thread.sleep(3000); System.out.println("async end"); } catch (InterruptedException e) { e.printStackTrace(); } }); System.out.println("main end"

Why CompletableFuture.runAsync is not executed?

点点圈 提交于 2020-12-27 05:41:19
问题 I consider that main thread must end after sub thread.But below code shows the process finished before print the "async end".What is the reason?Can anybody explain?Thx. import java.util.concurrent.CompletableFuture; public class Test { public static void main(String[] args) { CompletableFuture.runAsync(() -> { try { System.out.println("async start"); Thread.sleep(3000); System.out.println("async end"); } catch (InterruptedException e) { e.printStackTrace(); } }); System.out.println("main end"

Why CompletableFuture.runAsync is not executed?

╄→尐↘猪︶ㄣ 提交于 2020-12-27 05:38:46
问题 I consider that main thread must end after sub thread.But below code shows the process finished before print the "async end".What is the reason?Can anybody explain?Thx. import java.util.concurrent.CompletableFuture; public class Test { public static void main(String[] args) { CompletableFuture.runAsync(() -> { try { System.out.println("async start"); Thread.sleep(3000); System.out.println("async end"); } catch (InterruptedException e) { e.printStackTrace(); } }); System.out.println("main end"