我的任務是製作一個項目,它接受兩個矩陣的輸入維度,並讓用戶選擇他希望對這些矩陣執行的操作並輸出結果矩陣。增加的扭曲是它必須平行進行。對於每個元素/單元格,結果矩陣必須有一個線程。例如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");
}
}
這是我的用戶輸入的代碼。
我的問題是我將如何做到這一點。這是我的第一個並行項目,我知道我必須使用一組線程,但我不知道把它們放在哪裏,我嘗試了多種方式,似乎沒有人能夠幫助我。
我知道它需要一個線程數組,並且所需的數組數量是輸出矩陣中元素的數量,但我不知道在何處或如何實現該數組,因爲我以多種方式獲取錯誤我試過了。
嗨,謝謝你也幫我解決這個問題,我是新來的線程和並行編程,這幫助我非常感謝你 –