Fork/join Design Analysis __java

Source: Internet
Author: User

Introduction:

Fork/join parallelism is the simplest and most efficient design technique for obtaining good parallel performance. He is the parallel implementation of the partition (Divide-and-conquer) algorithm, its typical application form:

Result Solve (Problem Problem) {
  if (Problem are small)
	directly solve Problem
  else {
	split Problem into IND Ependent Parts
	Fork New subtasks to solve each part
	join all subtasks
	compose result from Subresults
	}
  
   }
  

The fork operation initiates a new parallel fork/join subtask, and the join operation causes the current thread to wait until subtask execution completes. The fork/join algorithm, like other divide-and-conquer algorithms, is almost always recursive, repeating subtasks-until they are small enough to be accomplished using a simple, short serial method.


thought:

1, divide and conquer, the big task broken down into small tasks, and then merge the final results

2. Job Theft scheduling strategy

(1) Each worker thread uses its own dispatch queue to maintain the executable task.
(2) The queues are two-terminal, and Support LIFO (Last-in-first-out) also supports FIFO (First-in-first-out).
(3) A task fork, which only push to the queue on which it is being thread.
(4) A worker thread uses LIFO to process threads in its own queue through a pop.
(5) When the thread itself has no pending task, it attempts to randomly read (steal) A worker thread's work Queue task (using FIFO).
(6) When the thread enters the join operation, it begins to process the task of the other thread (its own processing is finished) until the target task is completed (through the Isdone method). As a result, all tasks are done without blocking.

(7) When a worker thread does not have a task, and attempts to steal from other threads fail, it yields resources (by using yields, sleeps, or other priority adjustments) and then activates again until all worker threads are idle--at this point, They all block the call waiting for another top-level thread.


two kinds of interfaces:

Recursivetask: With return value, the bottom is using callable interface

Recursiveaction: No return value, Runnable interface


Important fields in Forkjoinpool source code:

EventCount three meanings:
   1, Thread is suspended number
   2, less than 0 means not activated
   3, marked as the first few worker threads
nextwait: Because Fork/join maintains a chain of suspended threads, this chain is the stack structure, This field is to traverse the qlock of the suspension stack: The role of the
lock top
, Base: Storage of the work thread queue header and tail
Poolindex: Refers to the worker thread in the queue subscript


Core approach Scan process resolution:


Design Highlights:

1, mutual help the design of ideas

2. Multi-field packaging operation


Sample code: Complete the cumulative operation

Import java.util.ArrayList;
Import Java.util.concurrent.ForkJoinPool;
Import Java.util.concurrent.ForkJoinTask;

Import Java.util.concurrent.RecursiveTask; /** * @Author: Allen * @Description: Cumulative operation with Forkjoinpool * @Date: Created in 12:42 2018/2/27 * @Modify by: * * Publ
    IC class Counttask extends recursivetask<long> {private static final int THRESHOLD = 10000;
    Private long start;

    private long end;
        Public Counttask (long Start,long end) {This.start = start;
    This.end = end;
        @Override protected Long Compute () {long sum = 0;
        Boolean cancompute = (End-start) <THRESHOLD;
            if (Cancompute)//Can be further divided for (long i = start; I <= end; i++) {sum+=i;                    else{Long step = (End+start)/100000; Divided into 100 task arraylist<counttask> tasks = new arraylist<> ();    Task Array long pos = step;
           Size of each task for (int i = 0; i < 100000 i++) {long lastone = Pos+step;   Gets a task based on the size of the task if (LaStone > End) LaStone = end; If the upper limit is exceeded, the upper limit is set counttask SubTask = new Counttask (pos,lastone);
                Create a new task System.out.println ("New task inf:" +pos+ "T" +lastone);        pos+=step+1; Update the initial position of the next task Tasks.add (SubTask);     Add the task to the Task array subtask.fork (); Perform task}//Get cumulative value for (Counttask t:tasks) {sum
            +=t.join ();
    return sum;     public static void Main (string[] args) {Forkjoinpool forkjoinpool = new Forkjoinpool ();
        The default is to work with the CPU's maximum number of cores counttask task = new Counttask (0,200000000l);  forkjointask<long> result = Forkjoinpool.submit (Task);
            Submit a task try {long res = result.get (); System.out.println ("sum=" +res);
        }catch (Exception e) {e.printstacktrace ();
 }
    }
}


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.