I want to find the maximum value of the two-dimensional array. I found this value without using multithreading. How do I find the maximum value of the two-dimensional array using multithreading? I want to compare the speed of finding the maximum value of the array in different ways.
public class Search {
public int[][] fillMatrix(int matrix[][]) {
for (int i = 0; i < matrix.length; i++){
for (int j = 0; j < matrix[i].length; j++){
matrix[i][j] = (int)(Math.random() * 1000);
}
}
return matrix;
}
public int searchMaxValue(int[][] matrix, int row, int column) {
int max = matrix[0][0];
for (int a = 0; a < row; a++) {
for (int b = 0; b < column; b++) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (matrix[a][b] > max) {
max = matrix[a][b];
}
}
}
return max;
}
public static void main(String[] args) {
Search search = new Search();
int[][] matrix = new int[4][100];
search.fillMatrix(matrix);
long start = System.currentTimeMillis();
int max = search.searchMaxValue(matrix, 4, 100);
long end = System.currentTimeMillis();
System.out.println("Max value is " + max);
System.out.println("Time for execution: " + (end - start));
}
}
column) that looks for the max value, returns it and ends. The main thread will then look for the biggest value in all those values only, improving speed on multicore CPUs.