Jdk7 parallel computing framework fork/join

Source: Internet
Author: User

In the old name, split fork + and merge join. Jdk1.7 integrates fork/join, which greatly improves the performance.

Idea: Make full use of multi-core CPUs to split computing into multiple sub-tasks for parallel computing, improving CPU utilization and greatly reducing computing time. It's a bit like the idea of mapreduce is roughly the same.

JDK 7 has provided the most concise interface, so that you do not need to worry too much about the communication, deadlock, and thread synchronization of concurrent threads. The following is the interface it provides:

Simple Example:

Package tank. forjoin. demo; import Java. util. concurrent. recursivetask;/*** @ author tank * @ Date: 01:30:06 * @ Description: * @ version: Task * futrue: * forkjointask: recursivetask recursiveaction */public class demo1 extends recursivetask <integer> {private int start; private int end; Public demo1 (INT start, int end) {This. start = start; this. end = end;} // calculate @ override protected integer compute () {int sum = 0; If (START-end <100) {for (INT I = start; I <end; I ++) {sum + = I ;}} else {// If the interval is 100, split multiple tasks to calculate int middle = (start + end)/2; demo1 left = new demo1 (START, middle); demo1 right = new demo1 (middle + 1, end); left. fork (); right. fork (); sum = left. join () + right. join () ;}return sum ;}}
Package tank. forjoin. demo; import Java. util. concurrent. executionexception; import Java. util. concurrent. forkjoinpool; import Java. util. concurrent. future; import Org. JUnit. test;/*** @ author tank * @ Date: 01:50:20 * @ Description: * @ version: */public class testdemo1 {@ test public void testdemo1 () throws interruptedexception, executionexception {forkjoinpool = new forkjoinpool (); // extended future of the thread pool <integer> result = forkjoinpool. submit (New demo1 (1, 10000); system. out. println (result. get ());
     
Forkjoinpool. Shutdown ();}}

Recursiveaction has no returned value.

Recursivetask has the return value type.

Forkjoinpool provides a series of submit methods for computing tasks. The default number of threads in forkjoinpool is obtained through runtime. availableprocessors (), because in computing-intensive tasks, threads that obtain more than the number of processing cores cannot improve performance.

 

Jdk7 parallel computing framework fork/join

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.