Sitemap
Javarevisited

A humble place to learn Java and Programming better.

Member-only story

Fork/Join Thread Pool Framework: A Powerful Tool for Task — splitting Computation!

--

My articles are open to everyone; non-member readers can read theull article by clicking this link.

Starting from JDK 1.7, a new Fork/Join thread pool framework was introduced. It can split a large task into multiple smaller tasks to be executed in parallel and finally aggregate the execution results.

For example, if you want to calculate the sum of an array currently, the simplest way is to complete it in one thread using a loop. However, when the array is extremely large, the execution efficiency of this method will be relatively poor, as shown in the following sample code.

long sum = 0;
for (int i = 0; i < array.length; i++) {
sum += array[i];
}
System.out.println("result: " + sum);

There is another way, which is to split the array. For example, divide it into 4 parts and execute them in parallel with 4 threads. Each part is calculated separately, and finally the results are aggregated. In this way, the execution efficiency will be significantly improved.

Javarevisited
Javarevisited

Published in Javarevisited

A humble place to learn Java and Programming better.

Dylan Smith
Dylan Smith

Written by Dylan Smith

Software Engineer for a leading global e-commerce company. Dedicated to explaining every programming knowledge with interesting and simple examples.

Responses (1)