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