forkjoinpool

Java: How to get finished threads to pickup tasks from running threads

一世执手 提交于 2019-12-09 22:51:01
问题 I am working on a multithreaded application with tasks that have varying run times. When one thread finishes, is there a way for it to take over some tasks from a still running thread? Here is an example. I kick off my program with 5 threads, and each have 50 tasks. When the quickest running thread finishes, another thread still has 40 tasks to complete. How can I get the finished thread to take 20 tasks from the other thread, so each continue working on 20 a piece, rather than waiting for

ForkJoinTask vs CompletableFuture

為{幸葍}努か 提交于 2019-12-08 15:59:03
问题 In Java 8 there are two ways of starting asynchronous computations - CompletableFuture and ForkJoinTask . They both seem fairly similar - the inner classes of CompletableFuture even extend ForkJoinTask . Is there a reason to use one over the other? One key difference that I can see is that the CompletableFuture.join method simply blocks until the future is complete ( waitingGet just spins using a ManagedBlocker ), whereas a ForkJoinTask.join can steal work off the queue to help the task you

Will inner parallel streams be processed fully in parallel before considering parallelizing outer stream?

廉价感情. 提交于 2019-12-08 04:50:09
问题 From this link, I only partially understood that, at least at some point, there was a problem with java nested parallel streams. However, I couldn't deduce the answer to the following question: Let's say I have an outer srtream and an inner stream, both of which are using parallel stream. It turns out, according to my calculations, that it'll be more performant (due to data locality, ie caching in L1/L2/L3 CPU caches) if the inner stream is done fully in parallel first, and then (if and only

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

How to Block a Queue in ForkJoinPool?

不想你离开。 提交于 2019-12-07 11:52:29
问题 I need to block threads on ForkJoinPool when its queue is full. This can be done in the standard ThreadPoolExecutor, e.g.: private static ExecutorService newFixedThreadPoolWithQueueSize(int nThreads, int queueSize) { return new ThreadPoolExecutor(nThreads, nThreads, 5000L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(queueSize, true), new ThreadPoolExecutor.CallerRunsPolicy()); } I know, there is some Dequeue inside ForkJoinPool, but I don't have access to it via its API. Update:

Parallel stream doesn't set Thread.contextClassLoader after tomcat upgrade

不问归期 提交于 2019-12-07 03:59:25
问题 After tomcat upgrade from 8.5.6 to 8.5.28 parallel stream stopped supplying Threads with contextClassLoader: Because of it Warmer::run can't load classes in it. warmers.parallelStream().forEach(Warmer::run); Do you have any ideas what Tomcat was supplying for contextClassLoaders for new Threads? ParallelStream uses ForkJoinPool in newest Tomcat. 回答1: Common ForkJoin pool is problematic and could be responsible for memory leaks and for applications being able to load classes and resources from

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

How to Block a Queue in ForkJoinPool?

╄→гoц情女王★ 提交于 2019-12-06 00:32:29
I need to block threads on ForkJoinPool when its queue is full. This can be done in the standard ThreadPoolExecutor, e.g.: private static ExecutorService newFixedThreadPoolWithQueueSize(int nThreads, int queueSize) { return new ThreadPoolExecutor(nThreads, nThreads, 5000L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(queueSize, true), new ThreadPoolExecutor.CallerRunsPolicy()); } I know, there is some Dequeue inside ForkJoinPool, but I don't have access to it via its API. Update: Please see the answer below. After some research I am happy to answer the question: Reason: There is no

ForkJoinPool scheduling vs ExecutorService

亡梦爱人 提交于 2019-12-05 13:42:32
I'm slightly confused by the internal scheduling mechanism of the ExecutorService and the ForkJoinPool. I understood the ExecutorService scheduling is done this way . A bunch of tasks are queued. Once a thread is available it will handle the first available task and so forth. Meanwhile, a ForkJoinPool is presented as distinct because it uses a work-stealing algorithm. If I understand correctly it means a thread can steal some tasks from another thread. Yet, I don't really understand the difference with the mechanism implemented in the ExecutorService. From my understanding, both mechanisms

Parallel stream doesn't set Thread.contextClassLoader after tomcat upgrade

假装没事ソ 提交于 2019-12-05 07:16:01
After tomcat upgrade from 8.5.6 to 8.5.28 parallel stream stopped supplying Threads with contextClassLoader: Because of it Warmer::run can't load classes in it. warmers.parallelStream().forEach(Warmer::run); Do you have any ideas what Tomcat was supplying for contextClassLoaders for new Threads? ParallelStream uses ForkJoinPool in newest Tomcat. Common ForkJoin pool is problematic and could be responsible for memory leaks and for applications being able to load classes and resources from other contexts/applications (potential security leak if your tomcat is multi tenant). See this Tomcat