java线程cpu使用率

nbysray5  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(438)

我正在尝试用java编写一个使用线程的快速排序算法。但是,无论数组的长度如何,cpu利用率永远不会是100%。你能帮我找出这个问题吗?这是我的密码:

import java.util.Random;

public class Main {

    public static void main(String[] args) {
        int[] arr;
        Random random = new Random();
        arr = new int[53000000];
        for (int i = 0; i < arr.length; i++){
            arr[i] = random.nextInt();
        }
        SortThread sortThread = new SortThread(arr, 0, arr.length - 1);
        Thread threadSort = new Thread(sortThread);
        threadSort.start();
    }

}
public class SortThread implements Runnable {

    private int[] arr;
    private int start;
    private int end;

    public SortThread(int[] arr, int start, int end) {
        this.arr = arr;
        this.start = start;
        this.end = end;
    }

    @Override
    public void run() {

        if (start < end){
            int partitionIndex = partition(arr, start, end);
            SortThread sortThreadLeft = new SortThread(arr, start, partitionIndex - 1);
            SortThread sortThreadRight = new SortThread(arr, partitionIndex + 1, end);
            Thread sortLeft = new Thread(sortThreadLeft);
            Thread sortRight = new Thread(sortThreadRight);
            sortLeft.start();
            sortRight.start();
        }
    }

    private int partition(int arr[], int begin, int end){

        int pivot = arr[end];
        int i = (begin - 1);

        for (int j = begin; j < end; j++){
            if (arr[j] <= pivot ){
                i++;
                int swpTemp = arr[i];
                arr[i] = arr[j];
                arr[j] = swpTemp;
            }
        }

        int swapTemp = arr[i + 1];
        arr[i + 1] = arr[end];
        arr[end] = swapTemp;
        return i + 1;
    }
}

Main 类i定义了具有随机元素的数组。在 SortThread i类使用快速排序算法对数组进行排序。每个子数组在不同的线程中分别排序。从理论上讲,这应该占用每个cpu的所有处理能力。但实际上,不管数组的长度如何,只使用了40%。

thigvfpy

thigvfpy1#

我如何解决:

import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

public class Main {

    static long start;

    public static void main(String[] args) {
        int numOfThreads = 8;
        try {
            numOfThreads = Integer.parseInt(args[0]);
            System.out.println("Number of threads: " + numOfThreads);
        } catch (Exception e) {
            System.out.println("Invalid number of threads: Default 8");
        }

        SortThread.executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(numOfThreads);

        int[] arr = new int[Integer.parseInt(args[1])];
        for (int i = 0; i < arr.length; i++){
            arr[i] = (int)(Math.random() * 256) + 1;
        }

        SortThread sortThread = new SortThread(arr, 0, arr.length - 1);
        Thread threadSort = new Thread(sortThread);
        start = System.nanoTime();
        threadSort.start();
    }

}

class SortThread implements Runnable {
    static ThreadPoolExecutor executor;

    private int[] arr;
    private int start;
    private int end;

    public SortThread(int[] arr, int start, int end) {

        this.arr = arr;
        this.start = start;
        this.end = end;

    }

    @Override
    public void run() {

        if (start < end){
            int partitionIndex = partition(arr, start, end);

            SortThread sortThreadLeft = new SortThread(arr, start, partitionIndex - 1);
            SortThread sortThreadRight = new SortThread(arr, partitionIndex + 1, end);

            Thread sortLeft = new Thread(sortThreadLeft);
            Thread sortRight = new Thread(sortThreadRight);

            SortThread.executor.execute(sortLeft);
            SortThread.executor.execute(sortRight);
        } else {
            if (SortThread.executor.getQueue().size() == 0 && SortThread.executor.getActiveCount() == 1) {
                System.out.println((System.nanoTime() - Main.start)/1000000);
                SortThread.executor.shutdown();
            }
        }
    }

    private static int partition(int[] arr, int begin, int end){

        int pivot = arr[end];
        int i = (begin - 1);

        for (int j = begin; j < end; j++){
            if (arr[j] <= pivot ){

                i++;
                int swpTemp = arr[i];
                arr[i] = arr[j];
                arr[j] = swpTemp;

            }
        }

        int swapTemp = arr[i + 1];
        arr[i + 1] = arr[end];
        arr[end] = swapTemp;

        return i + 1;
    }
}

刚刚创建了executor并将它们全部添加到池中。

相关问题