java实现串行、并行、并发求大数量矩阵乘积,并把每一步的矩阵乘积输出来
时间: 2023-12-24 12:05:55 浏览: 38
以下是Java实现串行、并行、并发求大数量矩阵乘积,并把每一步的矩阵乘积输出的示例:
1. 串行求矩阵乘积并输出每一步结果
```java
public class SerialMatrixMultiplication {
public static void main(String[] args) {
int[][] A = generateMatrix(100, 100);
int[][] B = generateMatrix(100, 100);
int[][] C = new int[A.length][B[0].length];
for (int i = 0; i < A.length; i++) {
for (int j = 0; j < B[0].length; j++) {
for (int k = 0; k < B.length; k++) {
C[i][j] += A[i][k] * B[k][j];
}
System.out.print(C[i][j] + " ");
}
System.out.println();
}
}
private 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(10);
}
}
return matrix;
}
}
```
2. 并行求矩阵乘积并输出每一步结果
```java
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class ParallelMatrixMultiplication {
private static int[][] A, B, C;
public static void main(String[] args) throws InterruptedException {
A = generateMatrix(100, 100);
B = generateMatrix(100, 100);
C = new int[A.length][B[0].length];
int processors = Runtime.getRuntime().availableProcessors();
ExecutorService executor = Executors.newFixedThreadPool(processors);
for (int i = 0; i < A.length; i++) {
for (int j = 0; j < B[0].length; j++) {
executor.submit(new Task(i, j));
}
}
executor.shutdown();
executor.awaitTermination(1, TimeUnit.HOURS);
}
private static class Task implements Runnable {
int i, j;
public Task(int i, int j) {
this.i = i;
this.j = j;
}
@Override
public void run() {
for (int k = 0; k < B.length; k++) {
C[i][j] += A[i][k] * B[k][j];
}
System.out.println("C[" + i + "][" + j + "] = " + C[i][j]);
}
}
private 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(10);
}
}
return matrix;
}
}
```
3. 并发求矩阵乘积并输出每一步结果
```java
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class ConcurrentMatrixMultiplication {
private static final int THREAD_COUNT = 4;
private static int[][] A, B, C;
public static void main(String[] args) throws InterruptedException {
A = generateMatrix(100, 100);
B = generateMatrix(100, 100);
C = new int[A.length][B[0].length];
ExecutorService executor = Executors.newFixedThreadPool(THREAD_COUNT);
for (int t = 0; t < THREAD_COUNT; t++) {
executor.submit(new Task(t));
}
executor.shutdown();
executor.awaitTermination(1, TimeUnit.HOURS);
}
private static class Task implements Runnable {
private int id;
public Task(int id) {
this.id = id;
}
@Override
public void run() {
int start = id * A.length / THREAD_COUNT;
int end = (id + 1) * A.length / THREAD_COUNT;
for (int i = start; i < end; i++) {
for (int j = 0; j < B[0].length; j++) {
for (int k = 0; k < B.length; k++) {
C[i][j] += A[i][k] * B[k][j];
}
System.out.println("C[" + i + "][" + j + "] = " + C[i][j]);
}
}
}
}
private 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(10);
}
}
return matrix;
}
}
```
上述三个示例分别实现了串行、并行和并发求大数量矩阵乘积,并输出了每一步的结果。其中,串行和并行示例使用了线程池和多线程技术,而并发示例使用了Java并发编程中的Executor框架。在实际应用中,可以根据具体情况选择使用不同的实现方式,以获得最优的计算效率。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)