gpt4 book ai didi

java - Java线程CPU使用率

转载 作者:行者123 更新时间:2023-12-03 13:02:02 27 4
gpt4 key购买 nike

我目前正在尝试使用线程编写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类中,我定义了带有随机元素的数组。在 SortThread类中,我使用快速排序算法对数组进行排序。每个子数组在不同的线程中分别排序。从理论上讲,这应该占用每个CPU的所有处理能力。但是实际上,无论数组的长度如何,都只使用40%。

最佳答案

我如何解决:

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;
}
}
刚刚创建了执行程序,并将它们全部添加到了池中。

关于java - Java线程CPU使用率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65061023/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com