Threading Basics: Multitasking (--fork/join) framework (solving sorting problems)

Source: Internet
Author: User
Tags new set cpu usage

==============
Thread Basics: Multitasking (--fork/join) framework (Basic use)

3. Use Fork/join to solve practical problems

In the previous article explaining the basic use of the Fork/join framework, the example given is to use the Fork/join framework to complete the 1-1000 integer summation. This example would be fine if it was just a demonstration of the use of the Fork/join framework, but there is still a gap between this example and the problems that are faced in the actual work. In this article, we use the Fork/join framework to solve a practical problem, that is, the problem of efficient sequencing.

3-1. Use the merge algorithm to solve sorting problems

Sorting is a common problem in our work. At present, there are many algorithms are invented to solve this problem, such as a variety of interpolation sorting algorithm, multi-exchange sorting algorithm. And the ranking algorithm is a sort algorithm which has better average time complexity (O (NLGN)) and better stability of algorithm in all current sorting algorithms. Its core algorithmic approach is to break up big problems into smaller problems and merge the results.

The split phase of the entire algorithm is to recursively split the unordered set of numbers from a larger set into smaller collections that either contain a maximum of two elements or are considered small enough to continue splitting.

Then the ordering of the elements in a collection becomes two questions: 1, the size of a maximum of two elements in a smaller set, and 2, how to combine two ordered sets into a new ordered set. The first problem is well solved, so is the second question complicated? In fact, the second problem is very simple, you just need to do two of the collection at the same time to complete---compare the smallest element in the current collection, the smallest element into a new collection, its time complexity is O (n):

The following is a simple implementation of the merge sort algorithm:

 PackageTest.thread.pool.merge;ImportJava.util.Arrays;ImportJava.util.Random;/** * Merge sort * @author Yinwenjie */ Public  class Merge1 {    Private Static intMAX =10000;Private Static intInits[] =New int[MAX];//This is to generate a set of random integers with a quantity of Max, ready to calculate the data    //And the algorithm itself does not matter    Static{Random R =NewRandom (); for(intindex =1; Index <= MAX; index++) {inits[index-1] = R.nextint (10000000); }    } Public Static void Main(string[] args) {LongBeginTime = System.currenttimemillis ();intResults[] = forkits (inits);LongEndTime = System.currenttimemillis ();//If the data involved in the sequencing is very large, remember to remove this printing methodSystem.out.println ("Time consuming ="+ (Endtime-begintime) +" | "+ arrays.tostring (results)); }//split into smaller elements or make a sort of small enough set of elements    Private Static int[]forkits(intSource[]) {intSourcelen = Source.length;if(Sourcelen >2) {intMidindex = Sourcelen/2;intResult1[] = forkits (arrays.copyof (source, midindex));intResult2[] = forkits (Arrays.copyofrange (source, Midindex, Sourcelen));//combine two ordered arrays into an ordered array            intMer[] = joinints (RESULT1, RESULT2);returnMer }//Otherwise there is only one or two elements in the set that can be sorted by the comparison of these two elements        Else{//If the condition is true, it indicates that there is only one element in the array, or that the elements in the array are already positioned.            if(Sourcelen = =1|| source[0] <= source[1]) {returnSource }Else{intTargetp[] =New int[Sourcelen]; targetp[0] = source[1]; targetp[1] = source[0];returnTARGETP; }        }    }/** * This method is used to merge two ordered sets * @param array1 * @param array2 * *    Private Static int[]joinints(intArray1[],intArray2[]) {intDestints[] =New int[Array1.length + array2.length];intArray1len = Array1.length;intArray2len = Array2.length;intDestlen = Destints.length;///Just take the new set destints length as standard and traverse once to         for(intindex =0, Array1index =0, Array2index =0; Index < Destlen; index++) {intvalue1 = Array1index >= array1len? Integer.max_value:array1[array1index];intvalue2 = Array2index >= array2len? Integer.max_value:array2[array2index];//If the condition is true, the value in the array array1 should be taken            if(Value1 < value2)                {array1index++;            Destints[index] = value1; }//Otherwise take the value in the array array2            Else{array2index++;            Destints[index] = value2; }        }returndestints; }}

The merge algorithm only takes 2-3 milliseconds to sort 10,000 random numbers, and it takes about 20 milliseconds to sort 100,000 random numbers, and the average time to sort 1 million random numbers is about 160 milliseconds (this depends on how cluttered the randomly generated array is). The visible merging algorithm has good performance in itself. Using the JMX tool and the CPU monitor that comes with the operating system to monitor the execution of the application, you can see that the entire algorithm is single-threaded, and that the CPU only has a single core working as the primary processing core at the same time:

    • The thread condition observed in JMX:

    • CPU Operating conditions:

3-2. Using Fork/join to run the merge algorithm

However, as the data size in the set to be sorted continues to increase, the code implementation of the above merge algorithm is somewhat inadequate, for example, when the above algorithm is used to sort 100 million random number sets, it takes about 27 seconds.

We can then use the Fork/join framework to optimize the execution performance of the merge algorithm, instantiating the split subtasks into multiple forkjointask tasks into the queue for execution, and by fork/ The join framework dispatches these tasks between multiple forkjoinworkerthread threads. As shown in the following:

Following is the merge algorithm code after using the Fork/join framework, note that the code for merging two ordered sets into a new ordered set in the Joinints method is unchanged and can be found in the previous section of this article. So you don't have to go into the code anymore:

....../** * Merge sort algorithm using Fork/join framework * @author Yinwenjie */ Public  class Merge2 {    Private Static intMAX =100000000;Private Static intInits[] =New int[MAX];//The same random queue initialization, here will not repeat the    Static{        ......    } Public Static void Main(string[] args)throwsException {//Official start        LongBeginTime = System.currenttimemillis (); Forkjoinpool pool =NewForkjoinpool (); MyTask task =NewMyTask (inits); forkjointask<int[]> Taskresult = pool.submit (Task);Try{Taskresult.get (); }Catch(Interruptedexception |        Executionexception e) {e.printstacktrace (System.out); }LongEndTime = System.currenttimemillis (); System.out.println ("Time consuming ="+ (endtime-begintime)); }/** * Single-sorted subtasks * @author Yinwenjie */    StaticClass MyTask extends recursivetask<int[]> {Private intSource[]; Public MyTask(intSource[]) { This. Source = Source; }/* (non-javadoc) * @see java.util.concurrent.recursivetask#compute () */        @Override        protected int[]Compute() {intSourcelen = Source.length;//If the conditions are true, the set of tasks to be sorted is not small enough            if(Sourcelen >2) {intMidindex = Sourcelen/2;//Split into two sub-tasksMyTask Task1 =NewMyTask (arrays.copyof (source, midindex));                Task1.fork (); MyTask Task2 =NewMyTask (Arrays.copyofrange (source, Midindex, Sourcelen)); Task2.fork ();//combine two ordered arrays into an ordered array                intResult1[] = Task1.join ();intResult2[] = Task2.join ();intMer[] = joinints (RESULT1, RESULT2);returnMer }//Otherwise there is only one or two elements in the set that can be sorted by the comparison of these two elements            Else{//If the condition is true, it indicates that there is only one element in the array, or that the elements in the array are already positioned.                if(Sourcelen = =1|| source[0] <= source[1]) {returnSource }Else{intTargetp[] =New int[Sourcelen]; targetp[0] = source[1]; targetp[1] = source[0];returnTARGETP; }            }        }Private int[]joinints(intArray1[],intArray2[]) {//And consistent with the code shown above}    }}

With the Fork/join framework optimization, the same execution time of 100 million random numbers is about 17 seconds, of course, which is also related to the level of clutter, CPU performance, etc. of the set to be sorted. In general, however, there is a 50% performance improvement over the performance of a merge sort algorithm that does not use the Fork/join framework. The following are the CPU states and thread States observed at execution time:

    • Memory, thread state in JMX:

    • CPU Usage:

In addition to the merging algorithm code to achieve internal optimization of the details, using the Fork/join framework, we basically ensure that the operating system thread size, the computing resources of each CPU core at the same time to play out.

Threading Basics: Multitasking (--fork/join) framework (solving sorting problems)

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.