I'm trying to make a multithreaded merge sort and I've encountered a stack overflow error and I'm not sure what is causing it.
(我正在尝试进行多线程合并排序,但是遇到了堆栈溢出错误,我不确定是什么原因引起的。)
public static void concurrentMergeSort(int[] arr, int threadCount) {
if(threadCount <= 1){
regularMergeSort(arr);
return;
}
int middle = arr.length/2;
int[] left = Arrays.copyOfRange(arr, 0, middle); //Says error here
int[] right = Arrays.copyOfRange(arr, middle, arr.length);
concurrentMergeSort(left);//Says error here
concurrentMergeSort(right);
Thread leftSort = new Thread(new Sorting(left, threadCount));
Thread rightSort = new Thread(new Sorting(right, threadCount));
try{
leftSort.join();
rightSort.join();
}
catch (Exception ex){
ex.printStackTrace();
}
merge(arr, left, right);
}
public static void regularMergeSort(int[] arr){
if(arr.length == 1){
return;
}
int middle = arr.length/2;
int[] left = Arrays.copyOfRange(arr, 0, middle);
int[] right = Arrays.copyOfRange(arr, middle, arr.length);
regularMergeSort(left);
regularMergeSort(right);
merge(arr, left, right);
}
}
(})
I was thinking that maybe it was the thread count never decreasing, but when I modify the thread count I still get the same result.
(我在想,也许线程数从未减少,但是当我修改线程数时,我仍然得到相同的结果。)
Also it was working until I added a regular merge sort and concurrent merge sort to separate it. (在我添加常规合并排序和并发合并排序以将其分开之前,它一直有效。)
I only added the regular merge sort as well because I was barely getting a speed increase from just having the concurrent merge sort method and the main purpose of this modification of merge sort is to increase the time it takes to sort with multithreading. (我也只添加了常规的合并排序,因为仅使用并发合并排序方法几乎无法提高速度,这种修改合并排序的主要目的是增加使用多线程进行排序的时间。)
ask by Liamnator1 translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…