fork-join Framework (Java 7)
Java after JDK7 joined the framework of parallel computing fork/join,fork/join is divided, Fork is a large task split into several subtasks, subtasks are calculated separately, and join is to obtain the results of the subtasks, and then merge, this is a recursive process. When subtasks are assigned to different cores, the Fork-join framework is ideal for resolving concurrency problems that run on multi-core processors;
In general, the core of the Fork-join model is
divide the rule, fork the task, join to collect data;
core classes of the Fork/join framework
The Fork-join framework has several major core classes:
ForkjoinpoolClass: Thread pool class, which is responsible for running subtasks, inheriting from Executorservice, and calling it according to Java5 's Executorservic; Forkjoinpool Common constructors:
Forkjoinpool (int parallelism); Create a Forkjoinpool forkjoinpool () that parallelism a parallel thread; The primary method of submitting a thread with the return value of the Runtime.avaliableprocessors () method as a parallel thread quantity parameter forkjoinpool: Submit,execute,invoke;
|
Client Porkjoinpool thread Pool Submit task |
Forkjointask Task class Internal call task |
Asynchronous Execution |
Execute (forkjointask) |
Forkjointask.fork |
waiting to get results |
Invoke (Forkjointask) |
Forkjointask.invoke |
Execute, get future |
Submit (Forkjointask) |
Forkjointask.fork (Forkjointask are futures) |
ForkjointaskClass: The default implementation of a task class, which is typically used in its two default implementations:
recurisiveaction: A task class abstract class that does not return a value.
Recurisivetask: The task class abstract class with the return value. Two main methods of Forkjointask Fork commit subtasks, join get subtask execution results
The execution of the ※fork-join framework task is performed by the object of the Forkjointask class, and the generic callable and runnable interfaces can be used to represent the task.
Exception Handling
Forkjointask may throw an exception while executing, but there is no way to catch an exception directly in the mainline Chengri, so Forkjointask provides I
scompletedabnormally ()method to check whether the task has thrown an exception or has been canceled, and can pass through the Forkjointask
getexceptionMethod gets the exception. Use the following code:
if (task.iscompletedabnormally ()) {System.out.println (task.getexception ());} GetException method returns the Throwable object, Returns cancellationexception if the task is canceled. Returns null if the task is not completed or if no exception is thrown.
code Example
Example 1:recursivetask has a return value task classA brief simulation of the sum method of Java 8 Parallelstream parallel data streams (in order to facilitate the understanding of segmentation using the binary method, the JDK uses a more complex segmentation algorithm); A thread task with a return value is recursivetask: Parallel calculation of the sum value of the list, which is computed by a child thread after dividing it to a value less than the threshold;
//Child task classpublic static class Sumtask extends
recursivetask<long>{private static final int THRESHOLD = 100;//Specify split threshold private list<long>list; private long low; Blic sumtask (list<long> list,long Low,long high) {this.list = List; this.low = low; This.high = high;} @Override// Cover the Compute interface, specify the partition strategy (using the binary method) protected long compute () {long sum = 0; if (high-low +1 <= THRESHOLD) {sum = List.stream (). Skip (Low). Limit (high-low+1). Maptolong (x->x). sum (); The flow length is less than the threshold to directly compute sum}else{//flow length greater than threshold, and the convection is divided into long mid = (low + high)/2;
Sumtask
Leftsubtask= new Sumtask (LIST,LOW,MID); Sumtask
Rightsubtask= new Sumtask (List,mid+1,high); Distribution Run Child threads
Leftsubtask
. Fork ();
Rightsubtask
. Fork ();
or use Invoke (Lefsubtask,rightsubtask); Replace the two rows submitted by the above task;//Get child thread results