import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class Matrix300x300 { private static final int SIZE = 300; public static void main(String[] args) { int[][] matrix1 = generateMatrix(SIZE, SIZE); int[][] matrix2 = generateMatrix(SIZE, SIZE); long startTime = System.currentTimeMillis(); int[][] resultSequential = multiplyMatricesSequential(matrix1, matrix2); long endTime = System.currentTimeMillis(); long sequentialTime = endTime - startTime; startTime = System.currentTimeMillis(); int[][] resultParallel = multiplyMatricesParallel(matrix1, matrix2); endTime = System.currentTimeMillis(); long parallelTime = endTime - startTime; System.out.println("Sequential multiplication time: " + sequentialTime + " ms"); System.out.println("Parallel multiplication time: " + parallelTime + " ms"); } public static int[][] multiplyMatricesSequential(int[][] matrix1, int[][] matrix2) { int rows1 = matrix1.length; int columns1 = matrix1[0].length; int columns2 = matrix2[0].length; int[][] result = new int[rows1][columns2]; for (int i = 0; i < rows1; i++) { for (int j = 0; j < columns2; j++) { for (int k = 0; k < columns1; k++) { result[i][j] += matrix1[i][k] * matrix2[k][j]; } } } return result; } public static int[][] multiplyMatricesParallel(int[][] matrix1, int[][] matrix2) { int rows1 = matrix1.length; int columns1 = matrix1[0].length; int columns2 = matrix2[0].length; int[][] result = new int[rows1][columns2]; int numberOfThreads = 5; // Количество потоков ExecutorService executor = Executors.newFixedThreadPool(numberOfThreads); for (int i = 0; i < rows1; i++) { final int row = i; executor.execute(new Runnable() { @Override public void run() { for (int j = 0; j < columns2; j++) { for (int k = 0; k < columns1; k++) { result[row][j] += matrix1[row][k] * matrix2[k][j]; } } } }); } executor.shutdown(); while (!executor.isTerminated()) { // Ожидаем завершения всех потоков } return result; } public static int[][] generateMatrix(int rows, int columns) { int[][] matrix = new int[rows][columns]; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { matrix[i][j] = (int) (Math.random() * 10); } } return matrix; } }