java - 在JAVA程序中创建线程进行矩阵乘法

标签 java multithreading

我正在尝试使用 Java 中的线程来乘以 2 个 5x5 矩阵。我正在尝试使用 1 个线程来执行此操作,并一次执行一行和一列的乘法。但是该线程不执行。我将如何使用更多线程来做到这一点?这是代码

public class Matrix {
private int A[][];
private int B[][];
private int C[][];
private int r1, c1;
private int r2, c2;

public int[][] getC() {
    return C;
}

public void setC(int[][] c) {
    C = c;
}

public int getR1() {
    return r1;
}

public void setR1(int r1) {
    this.r1 = r1;
}

public int getC1() {
    return c1;
}

public void setC1(int c1) {
    this.c1 = c1;
}

public int getR2() {
    return r2;
}

public void setR2(int r2) {
    this.r2 = r2;
}

public int getC2() {
    return c2;
}

public void setC2(int c2) {
    this.c2 = c2;
}

public int[][] getA() {
    return A;
}

public void setA(int[][] a) {
    A = a;
}

public int[][] getB() {
    return B;
}

public void setB(int[][] b) {
    B = b;
}

synchronized void mul(int r, int c) {
    int sum = 0;
    for (int i = 0; i < c1; i++)
        sum = sum + A[r][i] * B[i][c];
    C[r][c]=sum;

}
}

public class MatrixMul extends Thread {
Matrix matrix;

MatrixMul(Matrix m) {
    this.matrix = m;
}

public void run() {
    try{
    while (true) {
        for(int i=0;i<matrix.getR1();i++){
            for(int j=0;j<matrix.getC2();j++){
                matrix.mul(i,j);
            }

        }


    }
}catch(Exception e){
    e.printStackTrace();
}
}
}
public class MatrixMulDemo {


public class MatrixMulDemo {

public static void main(String[] args) {
    // Create matrices
    Matrix m = new Matrix();
    // Initialize matrices
    int A[][] = { { 1, 2, 3, 4, 5 }, { 1, 2, 3, 4, 5 }, { 1, 2, 3, 4, 5 },
            { 1, 2, 3, 4, 5 }, { 1, 2, 3, 4, 5 } };
    int B[][] = { { 1, 2, 3, 4, 5 }, { 1, 2, 3, 4, 5 }, { 1, 2, 3, 4, 5 },
            { 1, 2, 3, 4, 5 }, { 1, 2, 3, 4, 5 } };
    int C[][] = { { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 },
            { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 } };
    m.setR1(5);
    m.setC1(5);
    m.setR2(5);
    m.setC2(5);
    m.setA(A);
    m.setB(B);
    m.setC(C);

    // Create and start matrix multiply & add threads
    MatrixMul m2 = new MatrixMul(m);
    m2.start();

    // Display Matrix Mul
    for (int i = 0; i < 5; i++) {
        System.out.println("\n");
        for (int j = 0; j < 5; j++)
            System.out.println(m.getC()[i][j]);
    }
}

}

编辑: 这段代码会并行运行 5 个线程而没有任何冗余吗?

public class MatrixMulDemo {

public static void main(String[] args) {
    // Create matrices
    Matrix m = new Matrix();
    // Initialize matrices
    int A[][] = { { 1, 2, 3, 4, 5 }, { 1, 2, 3, 4, 5 }, { 1, 2, 3, 4, 5 },
            { 1, 2, 3, 4, 5 }, { 1, 2, 3, 4, 5 } };
    int B[][] = { { 1, 2, 3, 4, 5 }, { 1, 2, 3, 4, 5 }, { 1, 2, 3, 4, 5 },
            { 1, 2, 3, 4, 5 }, { 1, 2, 3, 4, 5 } };
    int C[][] = { { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 },
            { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 } };
    m.setR1(5);
    m.setC1(5);
    m.setR2(5);
    m.setC2(5);
    m.setA(A);
    m.setB(B);
    m.setC(C);


    MatrixMul m2[] = new MatrixMul[5];
    for (int i = 0; i < 5; i++) {
        m2[i] = new MatrixMul(m);
        m2[i].start();
    }
    // Wait for threads to complete
    for (int i = 0; i < 5; i++) {
        try {
            m2[i].join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    for (int i = 0; i < 5; i++) {
        System.out.println("\n");
        for (int j = 0; j < 5; j++)
            System.out.println(m.getC()[i][j]);
    }
}

}

编辑:我将其更改为创建 25 个线程,但现在有些线程不是随机执行的。

public class Matrix {
private int A[][];
private int B[][];
private int C[][];
private int r1, c1;
private int r2, c2;
private int row, col;


public int getRow() {
    return row;
}

public void setRow(int row) {
    this.row = row;
}

public int getCol() {
    return col;
}

public void setCol(int col) {
    this.col = col;
}

public int[][] getC() {
    return C;
}

public void setC(int[][] c) {
    C = c;
}

public int getR1() {
    return r1;
}

public void setR1(int r1) {
    this.r1 = r1;
}

public int getC1() {
    return c1;
}

public void setC1(int c1) {
    this.c1 = c1;
}

public int getR2() {
    return r2;
}

public void setR2(int r2) {
    this.r2 = r2;
}

public int getC2() {
    return c2;
}

public void setC2(int c2) {
    this.c2 = c2;
}

public int[][] getA() {
    return A;
}

public void setA(int[][] a) {
    A = a;
}

public int[][] getB() {
    return B;
}

public void setB(int[][] b) {
    B = b;
}

synchronized void mul(int r, int c) {
    int sum = 0;
    for (int i = 0; i < c1; i++)
        sum = sum + A[r][i] * B[i][c];
    C[r][c]=sum;

}
}

public class MatrixMul extends Thread {
Matrix matrix;

MatrixMul(Matrix m) {
    this.matrix = m;
}

public void run() {
    try{
                matrix.mul(matrix.getRow(),matrix.getCol());            
}catch(Exception e){
    e.printStackTrace();
}
}
}
public class MatrixMulDemo {

public static void main(String[] args) {
    // Create matrices
    Matrix m = new Matrix();
    // Initialize matrices
    int A[][] = { { 1, 2, 3, 4, 5 }, { 1, 2, 3, 4, 5 }, { 1, 2, 3, 4, 5 },
            { 1, 2, 3, 4, 5 }, { 1, 2, 3, 4, 5 } };
    int B[][] = { { 1, 2, 3, 4, 5 }, { 1, 2, 3, 4, 5 }, { 1, 2, 3, 4, 5 },
            { 1, 2, 3, 4, 5 }, { 1, 2, 3, 4, 5 } };
    int C[][] = { { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 },
            { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 } };
    m.setR1(5);
    m.setC1(5);
    m.setR2(5);
    m.setC2(5);
    m.setA(A);
    m.setB(B);
    m.setC(C);

    MatrixMul m2[][] = new MatrixMul[5][5];
    for (int i = 0; i < 5; i++)
        for (int j = 0; j < 5; j++) {
            m.setRow(i);
            m.setCol(j);
            m2[i][j] = new MatrixMul(m);
            m2[i][j].start();
        }
    // Wait for threads to complete
    for (int i = 0; i < 5; i++)
        for (int j = 0; j < 5; j++)
            try {
                m2[i][j].join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

    for (int i = 0; i < 5; i++) {
        System.out.println("\n");
        for (int j = 0; j < 5; j++)
            System.out.println(m.getC()[i][j] + " ");
    }
}

}

最佳答案

在您的代码段中发生无限循环。

   public void run() {
    try {
        while (true) {     //Infinite Loop - Never terminated.
            for (int i = 0; i < matrix.getR1(); i++) {
                for (int j = 0; j < matrix.getC2(); j++) {
                    matrix.mul(i, j);
                }

            }

        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

关于java - 在JAVA程序中创建线程进行矩阵乘法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32286268/

相关文章:

java - Onclick Recycler View 中的按钮不起作用

java - 缓存控制被忽略

multithreading - 在计算时保持GIF动画运行

java - JAVA中实例方法同步

java - 出于测试目的终止 Java 线程

java - 我应该使用全局变量还是在 java 中传递变量?

java - 将子类添加到数组列表中,然后高级

java - 减少代码..以发挥作用

java - 构造函数中 "this"对象和 CurrentThread 之间

java - 聊天客户端 - 同时使用套接字和服务器套接字