【发布时间】:2025-12-26 20:20:08
【问题描述】:
我的任务是创建一个项目,该项目采用两个矩阵的输入维度,并让用户选择他希望对这些矩阵执行的操作并输出结果矩阵。增加的转折是它必须并行完成。对于每个元素/单元格,结果矩阵必须有一个线程。例如 2x2 和另一个 2x2 矩阵输出 4 个元素。所以必须有 4 个线程,每个线程对每个元素执行操作。这是我的矩阵代码:
public class Matrix {
public int row,column;
private double [][] matrixElements;
public Matrix (int rows, int columns){
this.row= rows;
this.column = columns;
matrixElements = new double[row][column];
populatematrix(-100,100);
}
public Matrix(double[][] matrixArray){
this.row = matrixArray.length;
this.column = (matrixArray[0]).length;
matrixElements = new double [row][column];
for (int i=0; i<row;i++){
for (int j=0; j<column;j++){
matrixElements[i][j] = matrixArray[i][j];
}
}
}
private void populatematrix(int min, int max){
Random randnum = new Random();
Random rand = new Random();
for (int i=0; i<row; i++){
for (int j= 0;i<row;i++){
matrixElements[i][j] = rand.nextInt((max - min) + 1) + min;
}
}
}
public Matrix add(Matrix otherMatrix){
double[][] resultMatrixArray = new double[this.row][this.column];
for (int i=0; i<row; i++){
for (int j=0; j<column; j++){
resultMatrixArray[i][j] = this.matrixElements[i][j] + otherMatrix.matrixElements[i][j];
}
}
return new Matrix(resultMatrixArray);
}
public Matrix subtract(Matrix otherMatrix){
double[][] resultMatrixArray = new double[row][column];
for (int i=0; i<row; i++){
for (int j=0; j<column; j++){
resultMatrixArray[i][j] = this.matrixElements[i][j] - otherMatrix.matrixElements[i][j];
}
}
return new Matrix(resultMatrixArray);
}
public Matrix dotProduct(Matrix otherMatrix){
double[][] resultMatrixArray = new double [row][column];
double sum = 0;
if (this.column !=otherMatrix.row)
System.out.println("\n\n Matrices Multiplication is not possible...Invalid Dimensions...\n\n");
else {
for (int c=0; c<this.row;c++){
for (int d = 0; d<otherMatrix.column;d++){
for (int k = 0; k<otherMatrix.row; k++){
sum = sum+((this.matrixElements[c][k])*(otherMatrix.matrixElements[k][d]));
}
resultMatrixArray[c][d]=sum;
sum = 0;
}
}
}
return new Matrix(resultMatrixArray);
}
public String getPrintableMatrix(){
String result ="";
for (double[] roww: matrixElements){
for (double j:roww){
result +=""+j + "";
}
result +="\n";
}
return result;
}
}
这是我用于查找任何操作的矩阵结果的方法的代码。
public class MatrixOperations {
public static void main(String args[]){
int row1,col1,row2,col2;
Scanner sc = new Scanner(System.in);
System.out.print("\n\n Input Matrix 1 dimensions (ROWS space COLUMNS):");
row1= sc.nextInt();
col1 = sc.nextInt();
System.out.print("\n\n Input Matrix 2 dimensions (ROWS space COlUMNS):");
row2= sc.nextInt();
col2 = sc.nextInt();
int operation;
System.out.print("\n\n Select the operation to executed: 1. Add 2. Subtract 3. Multiply \n > ");
operation = sc.nextInt();
Matrix result;
Matrix m1 = new Matrix(row1, col1);
Matrix m2 = new Matrix(row2, col2);
Thread myThreads[] = new Thread[Matrix.row];
switch(operation){
case 1:
result = m1.add(m2);
System.out.println("\n\n First Matrix: \n " + m1.getPrintableMatrix());
System.out.println("\n\n Second Matrix: \n " + m2.getPrintableMatrix());
System.out.println("\n\n Resultant Matrix: \n " + result.getPrintableMatrix());
break;
case 2:
result = m1.subtract(m2);
System.out.println("\n\n First Matrix: \n " + m1.getPrintableMatrix());
System.out.println("\n\n Second Matrix: \n " + m2.getPrintableMatrix());
System.out.println("\n\n Resultant Matrix: \n " + result.getPrintableMatrix());
break;
case 3:
result = m1.dotProduct(m2);
System.out.println("\n\n First Matrix: \n " + m1.getPrintableMatrix());
System.out.println("\n\n Second Matrix: \n " + m2.getPrintableMatrix());
System.out.println("\n\n Resultant Matrix: \n " + result.getPrintableMatrix());
break;
default: System.out.println("\nInvalid operation......\n");break;
}
System.out.print("\n\n");
}
}
这是我的用户输入代码。
我的问题是如何使这种平行。这是我的第一个并行项目,我知道我必须使用一组线程,但我不知道将它们放在哪里,我尝试了多种方法,似乎没有人能帮助我。
我知道它需要一个线程数组,所需的数组数量是输出矩阵中的元素数量,但我不知道在哪里或如何实现该数组,因为我在多种方式中都遇到了错误试过了。
【问题讨论】:
标签: java multithreading performance matrix