我研究了Java多线程编程。我的任务是实现一个多线程程序来计算矩阵产品。每个线程负责生成矩阵的不同行。任务说:"在主程序中使用join()
等待所有线程完成,然后打印出结果矩阵"。
所以这是我的代码:
MatrixProduct.java
package multythreading;
import java.util.Arrays;
public class MatrixProduct {
public static Matrix a;
public static Matrix b;
static {
a = new Matrix(new int[][]{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{4, 5, 64, 5, 6, 7, 8, 9, 10, 11},
{7, 8, 94, 5, 6, 7, 8, 9, 10, 12},
{7, 8, 94, 5, 6, 7, 8, 9, 10, 13},
{4, 5, 64, 5, 6, 7, 8, 9, 10, 14},
});
b = new Matrix(new int[][]{{2, 1, 3},
{4, 2, 1},
{6, 4, 5},
{6, 4, 5},
{4, 2, 1},
{2, 1, 3},
{4, 2, 1},
{6, 4, 5},
{6, 4, 5},
{4, 2, 1}});
}
public static void main(String [] args) {
Thread[] threads = new Thread[a.getRowNum()];
for (int i = 0; i < threads.length; i++) {
threads[i] = new Thread(new ResultMatrixLine(i), "Line number " + i + " computation thread");
threads[i].start();
}
for (Thread t: threads) {
if (t.isAlive()) {
try {
System.out.println(t.getName() + " : " + t.getState() + " still alive");
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println("All computation threads terminated and the resulting matrix is ready");
}
}
class ResultMatrixLine implements Runnable {
private int lineNumber;
public ResultMatrixLine(int lineNumber) {
this.lineNumber = lineNumber;
}
@Override
public void run() {
System.out.println("Line number = " + lineNumber + ": " + Arrays.toString(getResultLine()));
}
private int[] getResultLine() {
int[] result = new int[MatrixProduct.b.getColumnNum()];
for (int i = 0; i < MatrixProduct.b.getColumnNum(); i++)
for (int j = 0; j < MatrixProduct.a.getColumnNum(); j++)
result[i] += MatrixProduct.a.getMatrixElement(lineNumber, j)*MatrixProduct.b.getMatrixElement(j, i);
return result;
}
}
class Matrix {
private final int columnNum;
private final int rowNum;
private final int[][] matrix;
public Matrix(int[][] matrix) {
this.columnNum = matrix[0].length;
this.rowNum = matrix.length;
this.matrix = matrix;
}
public int getColumnNum() {
return columnNum;
}
public int getRowNum() {
return rowNum;
}
public int getMatrixElement(int columnIndx, int rowIndx) {
return matrix[columnIndx][rowIndx];
}
}
生成的矩阵行是独立计算的,所以我唯一关心的是在join()
方法中正确使用main()
。我的意思是在主线程结束时,必须终止所有其他线程,以便生成的矩阵准备就绪。我的决定乍一看没有问题,但我希望看到任何意见。
答案 0 :(得分:-1)
以下是我通过修改代码创建的解决方案:
在 Matrix产品中添加了一个ArrayList,以及一个在公共范围内从中删除线程的函数:
private static ArrayList<ResultMatrixLine> threads = new ArrayList<>();
public static void main(String [] args) {
for (int i = 0; i < a.getRowNum(); i++) {
// Create a new ResultMatrixLine thread
ResultMatrixLine thread = new ResultMatrixLine( i );
// Keep the thread handy for manipulation later
threads.add( thread );
// Create a new thread
thread.start();
}
threads.stream().forEach( (thread) -> {
if( thread.isAlive() ) {
try {
thread.join( 1000 );
} catch( InterruptedException ex ) {
ex.printStackTrace( System.out );
}
}
});
System.out.println("All computation threads terminated and the resulting matrix is ready");
}
<强>输出强>
Line number = 1: [670, 423, 503]
Line number = 3: [876, 556, 667]
Line number = 0: [254, 151, 165]
Line number = 4: [682, 429, 506]
Line number = 2: [872, 554, 666]
All computation threads terminated and the resulting matrix is ready
对线程使用join()方法,在创建所有线程之后,它将循环遍历ArrayList并检查是否有任何线程处于活动状态。如果是这样,请加入()他们。