我有一个矩阵运算的代码add,subtract,multiply。该代码生成两个矩阵,其中包含用户声明的维度内的随机元素。我的问题是我必须为输出矩阵的每个元素创建一个线程。我尝试使用数组来存储线程,但它返回“mul”的错误,这是我用于实现run的方法的变量。当然主要方法的类中有两个不同的类与Thread数组,下一个类包含我的所有Matrix操作算法,并且还包含为运行方法实现Runnable的类“MatrixThreads”。如果有人可以看看他们是否可以帮助我,我会非常感激。
MatrixOperations类
public class MatrixOperations{
public static void main(String args[]) throws InterruptedException{
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);
int m3 = col1*row2;
Thread myThreads[]= new Thread[m3];
for(int i=0; i<m3;i++){
myThreads[i] = new Thread(new MatrixOperations(mul));//here is the error
myThreads[i].start();
}
for (int i=0; i<m3;i++){
myThreads[i].join();
}
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");
}
}
Matrix类
import java.util.Scanner;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Matrix {
private int row,column;
private double [][] matrixElements;
class ThreadMatrix implements Runnable{
private Matrix mul;
public ThreadMatrix(Matrix mul){
this.mul = mul;
}
@Override
public void run() {
mul.add(mul);
}
}
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;j<row;j++){
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;
}
}
答案 0 :(得分:1)
首先,你的问题非常不清楚;但我想我能给你足够的答案让你前进。也许你可以澄清一下;我们从那里看得更远。所以...
不清楚:“每个输出一个线程”矩阵是什么意思。这样做毫无意义。可能你想并行运行几个矩阵上的不同的操作;你应该使用线程。
现在,为了帮助您:您的过于简单的模型使您的整个设计如此过于复杂,解决您的问题看起来要比它应该复杂得多。< / p>
我的意思是:你正在混淆责任:你知道, Matrix 应该与Matrix数据有关;也许是关于可以应用于矩阵的数学运算。因此:将整个“线程业务”排除在外。编写允许您创建两个矩阵的代码;然后在它们上运行add(),multiple()等操作。
然后,当一切正常时; 实例化那些Matrix对象的代码,该代码可以使用多个线程并行执行A + B,A * C,A-D等操作。