Fork/join-java Parallel Computing Framework

Source: Internet
Author: User

Java in the JDK7 after the addition of Parallel computing framework Fork/join, can solve our system of big data computing performance problems. Fork/join is the division of the method, fork is a large task split into several sub-tasks, sub-tasks to calculate, and join is to get to sub-task calculation results, and then merge, this is the process of recursion. Subtasks are most efficient when assigned to different cores for execution. The pseudo code is as follows:

Result solve (problem problem) {

If (problem is small)

Directly solve problem

else {

Split problem into independent parts

Fork new subtasks to solve each part

Join All Subtasks

Compose result from Subresults

}

}

The core class of the Fork/join framework is forkjoinpool, which is capable of receiving a forkjointask and getting the results of the calculation. Forkjointask has two subclasses, Recursivetask (with return values) and Recursiveaction (no return results), and when we define our own tasks, we simply select the two classes to inherit.

Here's an example: computes the and of all elements of a very large array. The code is as follows:

Import Java.util.Arrays;

Import Java.util.Random;

Import java.util.concurrent.ExecutionException;

Import Java.util.concurrent.ForkJoinPool;

Import Java.util.concurrent.RecursiveTask;

/**

* @author: Shuang.gao DATE:2015/7/14 time:8:16

*/public class Sumtask extends Recursivetask<integer> {

Private static final long serialversionuid = -6196480027075657316l;

private static final int THRESHOLD = 500000;

Private long[] array;

private int low;

private int high;

Public Sumtask (long[] Array, int. low, int high) {

This.array = array;

This.low = low;

This.high = high;

}

@Override

Protected Integer Compute () {

int sum = 0;

if (high-low <= THRESHOLD) {

Less than threshold is calculated directly

for (int i = low, i < high; i++) {

Sum + = Array[i];

}

} else {

1. A large task is divided into two sub-tasks

int mid = (low + high) >>> 1;

Sumtask left = new Sumtask (array, low, mid);

Sumtask right = new Sumtask (array, mid + 1, high);

2. Calculate separately

Left.fork ();

Right.fork ();

3. Merge Results

sum = Left.join () + Right.join ();

}

return sum;

}

public static void Main (string[] args) throws Executionexception, Interruptedexception {

long[] Array = Genarray (1000000);

System.out.println (arrays.tostring (array));

1. Create a task

Sumtask sumtask = new Sumtask (array, 0, array.length-1);

Long begin = System.currenttimemillis ();

2. Create a thread pool

Forkjoinpool Forkjoinpool = new Forkjoinpool ();

3. Submit task to thread pool

Forkjoinpool.submit (Sumtask);

4. Get results

Integer result = Sumtask.get ();

Long end = System.currenttimemillis ();

System.out.println (String.Format ("result%s time consuming%sms", result, end-begin));

}

private static long[] Genarray (int size) {

long[] array = new Long[size];

for (int i = 0; i < size; i++) {

Array[i] = new Random (). Nextlong ();

}

return array;

}

}

Fork/join-java Parallel Computing Framework

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.