38 lines
1.4 KiB
Java
Raw Normal View History

2025-02-27 19:12:43 +04:00
import java.util.Random;
import java.util.concurrent.*;
public class main {
public static void main(String[] args) throws InterruptedException, ExecutionException {
int[][] matrix = generateMatrix(1000, 1000);
long startTime = System.nanoTime();
int singleThreadedMax = SingleThreaded.findMax(matrix);
long endTime = System.nanoTime();
System.out.println("Single-threaded max: " + singleThreadedMax);
System.out.println("Single-threaded time: " + (endTime - startTime) + " ns");
startTime = System.nanoTime();
int threadPoolMax = ThreadPool.findMax(matrix, 10);
endTime = System.nanoTime();
System.out.println("ThreadPoolExecutor max: " + threadPoolMax);
System.out.println("ThreadPoolExecutor time: " + (endTime - startTime) + " ns");
startTime = System.nanoTime();
int forkJoinMax = ForkJoin.findMax(matrix);
endTime = System.nanoTime();
System.out.println("ForkJoinPool max: " + forkJoinMax);
System.out.println("ForkJoinPool time: " + (endTime - startTime) + " ns");
}
public static int[][] generateMatrix(int rows, int cols) {
int[][] matrix = new int[rows][cols];
Random random = new Random();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i][j] = random.nextInt(10000);
}
}
return matrix;
}
}