Java Fork/join Framework _java

Source: Internet
Author: User

The Fork/join framework is an implementation of the Executorservice interface, through which we can implement multiple processes. Fork/join can be used to split a large task recursively into smaller tasks, with the goal of leveraging all resources to enhance the performance of the application as much as possible.

As with any Executorservice interface implementation, Fork/join also uses a line pool distributed management worker threads. The unique feature of the Fork/join framework is that it uses the work-stealing (work-stealing) algorithm. With this algorithm, the worker thread can steal other tasks that are busy threads to execute when nothing can be done.

The core of the Fork/join framework is the Forkjoinpool class, a subclass of a Abstractexecutorservice class. Forkjoinpool realizes the core work-stealing algorithm and can perform forkjointask processing.

Basic usage

The first step in using the Fork/join framework is to write code to perform the fragmentation task. The code to write resembles the following pseudocode:

If task is small enough:
 Direct task
else:
 cut the task into two small tasks
 perform two small tasks and wait for the result

Using the Forkjointask subclass to encapsulate code like this, you will typically use a class provided by JDK, with Recursivetask (which returns a result) and recursiveaction two classes.

After you have prepared the Forkjointask subclass, create an object that represents all the tasks and pass it to the Invoke () method of a Forkjoinpool instance.

from Blur to clarity

To help understand how the Fork/join framework works, we use a case to illustrate: for example, to blur a picture. We use an integer array to represent the picture, where each value represents the color of a pixel. The blurred picture is also represented by an array of equal length.

The

execution blur is done by processing each pixel representing the picture. Calculates the mean value of each pixel and its surrounding pixels (the mean of the red, yellow, and blue primaries), and the computed array of results is the blurred picture. Since the representation of an image is usually a large array, it usually takes a lot of time to complete the process. The Fork/join framework can be used to speed up using the concurrency processing advantages of multiprocessor systems. The following is a possible implementation:

Package com.zhyea.robin;
 
Import java.util.concurrent.RecursiveAction;
  public class Forkblur extends Recursiveaction {private int[] msource;
  private int mstart;
  private int mlength;
 
  Private int[] mdestination; handle window size;
  Need to be an odd number.
 
  private int mblurwidth = 15;
    Public Forkblur (int[] src, int start, int length, int[] dst) {msource = src;
    Mstart = start;
    mlength = length;
  Mdestination = DST;
    } protected void computedirectly () {int sidepixels = (mBlurWidth-1)/2;
      for (int index = Mstart; index < Mstart + mlength; index++) {//calculates the average.
      float RT = 0, GT = 0, BT = 0; for (int mi =-sidepixels mi <= sidepixels mi++) {int mindex = Math.min (Math.max (mi + index, 0), MSOURCE.L
 
        ENGTH-1);
        int pixel = Msource[mindex];
        RT + = (float) (Pixel & 0x00ff0000) >>)/mblurwidth;
        GT + = (float) ((Pixel & 0x0000ff00) >> 8)/mblurwidth; BT + = (float) (Pixel &amP
      0X000000FF) >> 0)/mblurwidth;
      }//Reorganize target pixel.
          int dpixel = (0xff000000) |
          (((int) RT) << 16 |
          (((int) GT) << 8) |
      ((int) bt) << 0);
    Mdestination[index] = Dpixel;
 }
  }
  
  ....
}

Now the abstract Method compute () is implemented, in which both fuzzy operations are implemented and a task is split into two small tasks. This is simply based on the length of the array to decide whether to execute the task directly or split it into two small tasks:

  protected static int sthreshold = 100000;
 
  protected void Compute () {
    if (Mlength < sthreshold) {
      computedirectly ();
      return;
    }
 
    int split = MLENGTH/2;
 
    InvokeAll (New Forkblur (Msource, Mstart, Split, Mdestination),
        new Forkblur (Msource, Mstart + split, mlength-split,< c9/>mdestination));
  }

Because the implementation of these methods above is defined in a subclass of Recursiveaction, you can create and run tasks directly in a forkjoinpool. The specific steps are as follows:

1. Create an object that represents the task you want to perform:

SRC represents the source picture pixel array
//DST that represents the generated picture of the pixel
forkblur fb = new Forkblur (src, 0, src.length, DST);

2. Create a Forkjoinpool instance of a running task:

ForkJoinPool pool = new ForkJoinPool();

3. Run the task:

pool.invoke(fb);

The source code also contains some code to create the target picture. A specific reference Forkblur example.

Standard implementation

Using the Fork/join framework to perform concurrent tasks on multicore systems by custom algorithms of course, you need to implement custom classes (such as the Forkblur class we implemented earlier). In addition, some of the features of the Fork/join framework have been widely used in javase. For example, the Parallelsort () method of the Java.util.Arrays class in Java8 uses the Fork/join framework. You can refer to the Java API documentation specifically.

Another implementation of the Fork/join framework is under the Java.util.streams package, which is also part of the JAVA8 's lambda characteristics.

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.