我获取了一个给定的 int 数组并将其分解为多个子数组并将其放入 ArrayList 中。
1) 我需要为 a 的每个索引生成一个线程。 (这不起作用并导致线程使用相同的数据)
2)我需要发送每个线程通过快速排序类进行排序。(就目前情况而言,这是可行的)
3)我还需要将所有线程排序数组合并到一个排序数组中。(也不知道如何执行此操作)
public void run(ArrayList<int[]> a) {
Quicksort x = new Quicksort();
for (int i = 0; i < a.size(); i++) {
store = a.get(i);
System.out.println(store);
new Thread() {
public void run() {
x.sort(store);
System.out.println(Arrays.toString(store));
}
}.start();
}
}
public class Quicksort {
private int array[];
private int length;
public void sort(int[] inputArr) {
if (inputArr == null || inputArr.length == 0) {
return;
}
this.array = inputArr;
length = inputArr.length;
quickSort(0, length - 1);
}
private void quickSort(int lowerIndex, int higherIndex) {
int i = lowerIndex;
int j = higherIndex;
int pivot = array[lowerIndex+(higherIndex-lowerIndex)/2];
while (i <= j) {
while (array[i] < pivot) {
i++;
}
while (array[j] > pivot) {
j--;
}
if (i <= j) {
exchangeNumbers(i, j);
i++;
j--;
}
}
if (lowerIndex < j)
quickSort(lowerIndex, j);
if (i < higherIndex)
quickSort(i, higherIndex);
}
private void exchangeNumbers(int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
我如何填充
public class ArrayLists {
private ArrayList<int[]> List = new ArrayList<>();
public ArrayLists(int[] a, int size) {
int[] temp = new int[size];
int count = 0;
while (count + size <= a.length) {
temp = Arrays.copyOfRange(a, count, count + size);
List.add(temp);
count = count + size;
}
int[] temp2 = new int[a.length - count];
if (count != a.length && count < a.length) {
temp2 = Arrays.copyOfRange(a, count, a.length);
List.add(temp2);
}
run(List);
}
最佳答案
问题是我如何使用 store 变量。我需要一个 Final int[] 而不是 static int[] 全局变量。
关于java - 如何阻止线程接管另一个线程作业?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43392887/