Java Concurrency Basics Practices: divide and conquer

Source: Internet
Author: User

The third article in this series will take a very simple look at the maximum number of tasks for example, given four versions: 1. Sequential execution; 2. Based on the traditional Thread.Join (), 3. Future;4 based on the Concurrency Toolkit, the Fork/join framework introduced based on JDK 7. (Last updated in 2013.10.25)

Divide and Conquer (Divide-and-conquer) is a common method to solve complex problems. In concurrent applications, a complex algorithm can be decomposed into several child problems, and then a separate thread is used to execute the program that solves the child problem, then the results of each child problem are merged to obtain the final result. In a particular environment, this approach may better improve the execution efficiency of the program.

Scenario 1: Sequential execution

To find the maximum value in the given array of integers, the simple approach is to iterate through each element one by one, comparing the current element with the current maximum value, and then comparing it to the next element if the value of the current element is large, and then repeating it as the new current maximum value. Before writing a concurrent program to implement this algorithm, this article will first give an implementation version of the sequential implementation, but still use the idea of divide and conquer. That is, the given sequence is divided into a number of smaller subgroups, to find the maximum value of each subsequence, the maximum value of these to form a new series, and then use the same method to the new sequence of the division and the maximum value of the merge, ... And so on until you find the maximum value.

The Maxnumberfinder in Code Listing 1 is the base class for this article: the 1.getMaxNumber () method shows how to find the maximum value in a sequence; 2. Use the tool Method Getnumberarray ()/createnumberarray () You can create a random integer sequence of a specified length; 3. Method Findmaxnumber () shows the division of a sequence into a subsequence (the maximum length of the substring does not exceed the specified value threshold) and finds the maximum value of the subsequence. And the maximal value of the subsequence is composed of the new middle sequence of each level to find its maximum value.

Listing 1 public class Maxnumberfinder {public static final int THRESHOLD = 50;
    
    private static final Random Random = new Random ();
    public static int[] Getnumberarray () {return Createnumberarray (3000);
        private static int[] Createnumberarray (int capacity) {int[] numbers = new int[capacity];
        for (int i = 0; i < numbers.length i++) {Numbers[i] = Random.nextint (capacity);
    return numbers; public static int Getmaxnumber (int[] numbers) {if (numbers.length = = 0) {return integer.m
        In_value;
        int max = numbers[0];
            for (int i = 1; i < numbers.length i++) {if (Numbers[i] > max) {max = numbers[i];
    } return max; private static int findmaxnumber (int[] numbers) {//Interim max number array int[] Maxnumbers = new Int[numbers.length/thresHOLD + (numbers.length% THRESHOLD = 0? 0:1)]; for (int i = 0; I <= numbers.length-threshold i = THRESHOLD) {final int[] subnumbers = new Int[thresho
            LD];
            System.arraycopy (Numbers, I, subnumbers, 0, subnumbers.length);
        Maxnumbers[i/threshold] = Getmaxnumber (subnumbers); } if (numbers.length% THRESHOLD!= 0) {int[] lastsubnumbers = new Int[numbers.length% Threshol
            D];
            System.arraycopy (Numbers, numbers.length-lastsubnumbers.length, lastsubnumbers, 0, lastsubnumbers.length);
        Maxnumbers[maxnumbers.length-1] = Getmaxnumber (lastsubnumbers); //If the length of interim max number array is greater than threshold,//it must DIVIDE-AND-SEARC
        H recursively.
        if (Maxnumbers.length > THRESHOLD) {return findmaxnumber (maxnumbers);
        else {return getmaxnumber (maxnumbers); }
    }
}

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.