如何使用线程池和 Callable 接口对二维数组进行排序?
How can sort a 2D array with the use of thread pools and the Callable interface?
大家好,我正在尝试使用线程池对二维数组进行排序以测量执行时间,并在增加线程数和数组中的元素总数时进行比较。我已经完成了一些编码,但显然它 运行 非常慢,并且由于某种原因我收到空指针异常。我认为原因在于使用 Integer 数组,因为 Callable 不能使用原始 int[] 数组。帮助将不胜感激
public class MatrixBubbleSort {
public static void main(String[] args) throws InterruptedException, ExecutionException {
MatrixBubbleSort obj = new MatrixBubbleSort();
String resultMatrix = "";
resultMatrix = obj.sort2D();
System.out.println(resultMatrix);
}
public String sort2D() throws InterruptedException, ExecutionException {
long start = 0;
long end = 0;
int rows = 5;
int cols = 5;
Integer[][] matrix = new Integer[rows][cols];
Future<Integer[]>[] returned;
returned = new Future[rows];
Sorter[] tasks = new Sorter[rows];
ExecutorService executer = Executors.newFixedThreadPool(5);
for(int r = 0; r< rows; r++ ){
for(int c = 0; c < cols; cols++){
matrix[r][c] = (int) (Math.random() * (rows*cols));
}
}
System.out.print(printArr(matrix) + "\n");
start = System.currentTimeMillis();
for(int r = 0; r< rows; r++ ){
tasks[r] = new Sorter(matrix[r]);
returned[r] = executer.submit(tasks[r]);
}
executer.shutdown();
executer.awaitTermination(1, TimeUnit.DAYS);
end = System.currentTimeMillis();
for(int r = 0; r< rows; r++ ){
matrix[r] = returned[r].get();
}
System.out.print("Time taken = " + (end - start) + "\n");
return printArr(matrix);
}
public static String printArr(Integer[][] arr){
String out = "";
for(int i = 0; i < arr.length;i++ ){
for(int c = 0; c < arr[i].length;c++ ){
out += arr[i][c].intValue();
}
out += "\n";
}
return out;
}
}
class 排序器实现 Callable{
私有最终整数 [] 数组;
Sorter(Integer[] array){
this.array = array.clone();
}
@Override
public Integer[] call() throws Exception {
boolean swap = true;
int buffer = 0;
while(swap){
swap = false;
for(int i = 0; i < array.length -1; i++){
if(array[i].intValue() > array[i+1].intValue()){
buffer = array[i].intValue();
array[i]= array[i+1].intValue();
array[i+1] = buffer;
swap = true;
}
}
}
return array;
}
}
您的代码看起来 运行 OK,稍作改动后:
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) { // instead of cols++
matrix[r][c] = (int) (Math.random() * (rows * cols));
}
}
速度非常快,到目前为止没有空指针异常 - 想仔细检查一下吗?
大家好,我正在尝试使用线程池对二维数组进行排序以测量执行时间,并在增加线程数和数组中的元素总数时进行比较。我已经完成了一些编码,但显然它 运行 非常慢,并且由于某种原因我收到空指针异常。我认为原因在于使用 Integer 数组,因为 Callable 不能使用原始 int[] 数组。帮助将不胜感激
public class MatrixBubbleSort {
public static void main(String[] args) throws InterruptedException, ExecutionException {
MatrixBubbleSort obj = new MatrixBubbleSort();
String resultMatrix = "";
resultMatrix = obj.sort2D();
System.out.println(resultMatrix);
}
public String sort2D() throws InterruptedException, ExecutionException {
long start = 0;
long end = 0;
int rows = 5;
int cols = 5;
Integer[][] matrix = new Integer[rows][cols];
Future<Integer[]>[] returned;
returned = new Future[rows];
Sorter[] tasks = new Sorter[rows];
ExecutorService executer = Executors.newFixedThreadPool(5);
for(int r = 0; r< rows; r++ ){
for(int c = 0; c < cols; cols++){
matrix[r][c] = (int) (Math.random() * (rows*cols));
}
}
System.out.print(printArr(matrix) + "\n");
start = System.currentTimeMillis();
for(int r = 0; r< rows; r++ ){
tasks[r] = new Sorter(matrix[r]);
returned[r] = executer.submit(tasks[r]);
}
executer.shutdown();
executer.awaitTermination(1, TimeUnit.DAYS);
end = System.currentTimeMillis();
for(int r = 0; r< rows; r++ ){
matrix[r] = returned[r].get();
}
System.out.print("Time taken = " + (end - start) + "\n");
return printArr(matrix);
}
public static String printArr(Integer[][] arr){
String out = "";
for(int i = 0; i < arr.length;i++ ){
for(int c = 0; c < arr[i].length;c++ ){
out += arr[i][c].intValue();
}
out += "\n";
}
return out;
} }
class 排序器实现 Callable{ 私有最终整数 [] 数组;
Sorter(Integer[] array){
this.array = array.clone();
}
@Override
public Integer[] call() throws Exception {
boolean swap = true;
int buffer = 0;
while(swap){
swap = false;
for(int i = 0; i < array.length -1; i++){
if(array[i].intValue() > array[i+1].intValue()){
buffer = array[i].intValue();
array[i]= array[i+1].intValue();
array[i+1] = buffer;
swap = true;
}
}
}
return array;
}
}
您的代码看起来 运行 OK,稍作改动后:
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) { // instead of cols++
matrix[r][c] = (int) (Math.random() * (rows * cols));
}
}
速度非常快,到目前为止没有空指针异常 - 想仔细检查一下吗?