For&join Frame

Source: Internet
Author: User
Tags join split

For&join Framework, also known as the branch-merging framework, breaks down a large task fork into several small-task multithreading, returning the result jion summary. Divide-and-conquer strategy and recursive algorithm are adopted.

work-stealing mode:



Framework Package Java.util.concurrent
The first step is to split the task. First we need to have a fork class to divide the large task into sub-tasks, it is possible that the subtasks are still very large, so it is necessary to continue to split, until the split sub-task is small enough. Note: Divide and conquer strategy and recursive algorithm
The second step executes the task and merges the results. Split subtasks are placed in a double-ended queue, and several boot threads get task execution from the double-ended queue, respectively. The results of the subtasks are uniformly placed in a queue, starting a thread to take the data from the queue, and then merging the data.

Fork/join uses two classes to accomplish these two things: forkjointask: To use the Forkjoin framework, you must first create a forkjoin task. It provides a mechanism for executing the Fork () and join () operations in a task, and typically we do not need to inherit the Forkjointask class directly, but only inherit its subclasses, the Fork/join framework provides the following two subclasses:
recursiveaction: Used for tasks that do not return results. recursivetask : Used for tasks that have return results. forkjoinpool : forkjointask needs to be executed through Forkjoinpool, and the subtasks are added to the double-ended queue maintained by the current worker thread, and into the head of the queue. When there is no task in the queue for a worker thread, it randomly fetches a task from the end of the queue of other worker threads.


use of the framework:

calculation: 1+2+3+4;


Package cn.yh.jdk_8.forkjoin; import Java.util.concurrent.RecursiveTask;
 /** * Created by Liang on 2017/3/3.
    */public class Forkjointaskcaculator extends recursivetask<integer>{private int start;
    private int end;

    Private final int throdhold = 2;
        Public forkjointaskcaculator (int start, int end) {This.start = start;
    This.end = end;
        } @Override protected Integer Compute () {int sum = 0;
        if (end-start<=throdhold) {//Task split is small enough for sum = Caculator (start,end);
            }else {forkjointaskcaculator lefttask = new Forkjointaskcaculator (start,start+ (End-start)/2);
            Forkjointaskcaculator righttask = new Forkjointaskcaculator (start+ (End-start)/2,end);
            Lefttask.fork ();//callback The function righttask.fork ();
            Wait for the calculation complete and the word task int leftresult = Lefttask.join ();
            int rightresult = Righttask.join ();
        sum = Leftresult + Rightresult;
      }  return sum;
        } private int Caculator (int start,int end) {int result = 0;
        for (int i=start;i<end;i++) {result +=i;
    } return result;
 }
}


Framework Implementation principle:

    

The forkjoinpool consists of an array of Forkjointask and Forkjoinworkerthread, and the Forkjointask array is responsible for storing the tasks that the program submits to Forkjoinpool. The Forkjoinworkerthread array is responsible for performing these tasks.

Forkjointask's Fork method implementation principle. When we call Forkjointask's Fork method, the program calls Forkjoinworkerthread's Pushtask method to perform the task asynchronously, and then returns the result immediately. The code is as follows:

Public final Forkjointask fork () {
((Forkjoinworkerthread) Thread.CurrentThread ())
Pushtask (this);
return this;
}

The Pushtask method stores the current task in the Forkjointask array queue. Then call Forkjoinpool's Signalwork () method to wake up or create a worker thread to perform the task.

Forkjointask the Join method implementation principle. The primary role of the Join method is to block the current thread and wait for the results to be obtained.


Reference: http://ifeve.com/customizing-concurrency-classes-1/





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.