forkjoinpool

Why does stream parallel() not use all available threads?

早过忘川 提交于 2020-01-23 02:52:27
问题 I tried to run 100 Sleep tasks in parallel using Java8(1.8.0_172) stream.parallel() submitted inside a custom ForkJoinPool with 100+ threads available. Each task would sleep for 1s. I expected the whole work would finish after ~1s, given the 100 sleeps could be done in parallel. However I observe a runtime of 7s. @Test public void testParallelStream() throws Exception { final int REQUESTS = 100; ForkJoinPool forkJoinPool = null; try { // new ForkJoinPool(256): same results for all tried

ForkJoinPool scheduling vs ExecutorService

人走茶凉 提交于 2020-01-01 11:32:11
问题 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

ForkJoinPool resets thread interrupted state

微笑、不失礼 提交于 2019-12-31 00:46:10
问题 I just noticed the following phenomena when cancelling a Future returned by ForkJoinPool. Given the following example code: ForkJoinPool pool = new ForkJoinPool(); Future<?> fut = pool.submit(new Callable<Void>() { @Override public Void call() throws Exception { while (true) { if (Thread.currentThread().isInterrupted()) { // <-- never true System.out.println("interrupted"); throw new InterruptedException(); } } } }); Thread.sleep(1000); System.out.println("cancel"); fut.cancel(true); The

Confused by docs and source of CountedCompleter

隐身守侯 提交于 2019-12-24 04:09:28
问题 Here is a code fragment of java.util.concurrent.CountedCompleter class (JDK 1.8.0_25). /** * If the pending count is nonzero, decrements the count; * otherwise invokes {@link #onCompletion(CountedCompleter)} * and then similarly tries to complete this task's completer, * if one exists, else marks this task as complete. */ public final void tryComplete() { CountedCompleter<?> a = this, s = a; for (int c;;) { if ((c = a.pending) == 0) { a.onCompletion(s); if ((a = (s = a).completer) == null) {

ThreadPoolExecutor vs ForkJoinPool: stealing subtasks

北城以北 提交于 2019-12-21 12:05:42
问题 From java docs, A ForkJoinPool differs from other kinds of ExecutorService mainly by virtue of employing work-stealing: all threads in the pool attempt to find and execute subtasks created by other active tasks (eventually blocking waiting for work if none exist). This enables efficient processing when most tasks spawn other subtasks (as do most ForkJoinTasks). When setting asyncMode to true in constructors, ForkJoinPools may also be appropriate for use with event-style tasks that are never

How to configure and tune Akka Dispatchers

£可爱£侵袭症+ 提交于 2019-12-20 10:23:29
问题 I'm looking over the documentation here: http://doc.akka.io/docs/akka/2.3.3/java/dispatchers.html We're using Akka in such a way where we have two separate dispatchers (default fork-join executors) for different actors. We're now running into some performance issues and we're looking into how we can tune the dispatcher configuration parameters and see how they affect the performance of the application. I've looked over the documentation but don't really understand the configuration parameters

Deadlock happens if I use lambda in parallel stream but it doesn't happen if I use anonymous class instead? [duplicate]

北城以北 提交于 2019-12-20 03:05:42
问题 This question already has answers here : Why does parallel stream with lambda in static initializer cause a deadlock? (3 answers) Closed last year . The following code leads to deadlock(on my pc): public class Test { static { final int SUM = IntStream.range(0, 100) .parallel() .reduce((n, m) -> n + m) .getAsInt(); } public static void main(String[] args) { System.out.println("Finished"); } } But if I replace reduce lambda argument with anonymous class it doesn't lead to deadlock: public class

How do I know if Fork and Join has enough pool size in Java?

我的梦境 提交于 2019-12-19 10:52:47
问题 I am trying to implement a divide-and-conquer solution to some large data. I use fork and join to break down things into threads. However I have a question regarding the fork mechanism: if I set my divide and conquer condition as: @Override protected SomeClass compute(){ if (list.size()<LIMIT){ //Do something here ... }else{ //Divide the list and invoke sub-threads SomeRecursiveTaskClass subWorker1 = new SomeRecursiveTaskClass(list.subList()); SomeRecursiveTaskClass subWorker2 = new

Java ExecutorService - scaling

隐身守侯 提交于 2019-12-11 10:47:37
问题 I am trying to write a program in Java using ExecutorService and its function invokeAll . My question is: does the invokeAll function solve the tasks simultaneously? I mean, if I have two processors, there will be two workers at the same time? Because aI can't make it scale correctly. It takes the same time to complete the problem if I give newFixedThreadPool(2) or 1. List<Future<PartialSolution>> list = new ArrayList<Future<PartialSolution>>(); Collection<Callable<PartialSolution>> tasks =

MDC Context getting copied to only one thread of ForkJoinPool

风格不统一 提交于 2019-12-11 02:50:48
问题 I want to pass MDC context to threads of the ForkJoinPool as I need to print the requestId populated in the MDCContextMap in all the spawned threads logs for better debugging. I am using the following solution for this: How to use MDC with ForkJoinPool?. I am using the pool as: final ForkJoinPool mdcPool = new MdcForkJoinPool(30); mdcPool.submit(() -> ruleset.getOperationList().parallelStream().forEach(operation -> { log.info("Sample log line"); But from the logs it looks like MDC Context is