Questions tagged [fork-join]

Fork-Join means to split the work into fragments and join the results together. You can split the work into components and schedule each component to a thread pool joining the results when all components complete. You can recursively decompose an aggregate structure into identical tasks and join the results when all tasks complete.

By following the divide-and-conquer strategy, problems, such as merge sort of an array, are split into (two or more) smaller sub-problems. If these sub-problems are still too big to be solved directly, they are again recursively split into even smaller sub-problems. Once the sub-problems become small enough, a simple/naive algorithm can be used to solve them. The partial solutions of these sub-problems are then gradually combined into solutions of the original larger problems.

You can visualize the progress of the algorithm as a tree with the original problem as the root, all its direct sub-problems as the nodes right below the root, their sub-problems as the nodes further down and finally with the smallest directly solved problems as the leafs.

Trees are great data structures for parallel processing. The root node typically points to two or more child nodes. These child nodes are roots of their own sub-trees, and these sub-trees are mutually independent. Start a new parallel thread for each of the sub-trees and you are guaranteed the threads will never collide. You get thread-safety by design. This is exactly what fork/join does. Not only does it gradually divide the problem into smaller and smaller sub-problems, but it also allows these sub-problems to be processed concurrently.

As an example, you may try Java 1.7, which comes with a Fork/Join (originally referred to as jsr-166y) framework bundled.

418 questions
142
votes
11 answers

How is the fork/join framework better than a thread pool?

What are the benefits of using the new fork/join framework over just simply splitting the big task into N subtasks in the beginning, sending them to a cached thread pool (from Executors) and waiting for each task to complete? I fail to see how…
Joonas Pulakka
  • 34,943
  • 25
  • 103
  • 165
87
votes
3 answers

Why does parallel stream with lambda in static initializer cause a deadlock?

I came across a strange situation where using a parallel stream with a lambda in a static initializer takes seemingly forever with no CPU utilization. Here's the code: class Deadlock { static { IntStream.range(0, 10000).parallel().map(i…
Reinstate Monica
  • 2,270
  • 12
  • 21
79
votes
7 answers

Coordinating parallel execution in node.js

The event-driven programming model of node.js makes it somewhat tricky to coordinate the program flow. Simple sequential execution gets turned into nested callbacks, which is easy enough (though a bit convoluted to write down). But how about…
Thilo
  • 241,635
  • 91
  • 474
  • 626
72
votes
6 answers

Java's Fork/Join vs ExecutorService - when to use which?

I just finished reading this post: What's the advantage of a Java-5 ThreadPoolExecutor over a Java-7 ForkJoinPool? and felt that the answer is not straight enough. Can you explain in simple language and examples, what are the trade-offs between Java…
Parobay
  • 2,309
  • 3
  • 20
  • 33
49
votes
5 answers

What determines the number of threads a Java ForkJoinPool creates?

As far as I had understood ForkJoinPool, that pool creates a fixed number of threads (default: number of cores) and will never create more threads (unless the application indicates a need for those by using managedBlock). However, using…
Holger Peine
  • 1,009
  • 1
  • 9
  • 13
47
votes
2 answers

Difference between Fork/Join and Map/Reduce

What is the key difference between Fork/Join and Map/Reduce? Do they differ in the kind of decomposition and distribution (data vs. computation)?
hotzen
  • 2,622
  • 25
  • 39
45
votes
1 answer

Observable.forkJoin and array argument

In the Observables forkJoin documentation, it says that args can be an array but it doesn't list an example doing so: https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/forkjoin.md I have tried a function similar to what…
Benjamin McFerren
  • 1,312
  • 5
  • 20
  • 32
27
votes
3 answers

Detailed difference between Java8 ForkJoinPool and Executors.newWorkStealingPool?

What is the low-level difference among using: ForkJoinPool = new ForkJoinPool(X); and ExecutorService ex = Executors.neWorkStealingPool(X); Where X is the desired level of parallelism i.e threads running.. According to the docs I found them…
27
votes
1 answer

Java ForkJoinPool with non-recursive tasks, does work-stealing work?

I want to submit Runnable tasks into ForkJoinPool via a method: forkJoinPool.submit(Runnable task) Note, I use JDK 7. Under the hood, they are transformed into ForkJoinTask objects. I know that ForkJoinPool is efficient when a task is split into…
Ivan Voroshilin
  • 4,493
  • 3
  • 29
  • 56
26
votes
3 answers

Is Java's fork-and-join thread pool is good for executing IO bound task?

in my application, I have to solve a problem by executing many network-io bound task and sometime one io bound task and be divided into smaller io bound tasks. These tasks are currently getting executed using Java's standard threadpool mechanism. I…
Shamik
  • 6,442
  • 9
  • 52
  • 72
26
votes
3 answers

Where does official documentation say that Java's parallel stream operations use fork/join?

Here's my understanding of the Stream framework of Java 8: Something creates a source Stream The implementation is responsible for providing a BaseStream#parallel() method, which in turns returns a Stream that can run it's operations in…
Gima
  • 1,672
  • 18
  • 22
25
votes
1 answer

Why does Scala use a ForkJoinPool to back its default ExecutionContext?

In Scala you can use a global ExecutionContext if you don't need to define your own by importing scala.concurrent.ExecutionContext.Implicits.global. My question is why ForkJoinPool was chosen for this executor instead of ThreadPoolExecutor. My…
Brian Gordon
  • 5,993
  • 2
  • 28
  • 37
18
votes
3 answers

java Fork/Join clarification about stack usage

I read about the implementation of the Fork/Join framework that was introduced in Java 7 and I just wanted to check that I understand how the magic works. As I understand, when a thread forks, it creates subtasks in its queue (which other thread…
bennyl
  • 2,643
  • 2
  • 26
  • 42
17
votes
4 answers

Error rxjs_Observable__.Observable.forkJoin is not a function?

I am using Rxjs in an angualr-cli application. in viewer.component.ts //Other Imports import { Observable } from 'rxjs/Observable'; //omitting for brevity export class ViewerComponent implements OnInit, AfterViewInit, OnDestroy { …
Ankesh
  • 4,689
  • 3
  • 34
  • 75
17
votes
3 answers

Can I use the work-stealing behaviour of ForkJoinPool to avoid a thread starvation deadlock?

A thread starvation deadlock occurs in a normal thread pool if all the threads in the pool are waiting for queued tasks in the same pool to complete. ForkJoinPool avoids this problem by stealing work from other threads from inside the join() call,…
Tavian Barnes
  • 11,679
  • 3
  • 41
  • 107
1
2 3
27 28