在JAVA程序中为矩阵乘法创建线程
我试图用Java中的线程乘以2个5x5矩阵。我尝试使用一个线程,一次一行一列地执行乘法,但是线程不执行。我如何使用更多的线程来完成它?这是密码在JAVA程序中为矩阵乘法创建线程,java,multithreading,Java,Multithreading,我试图用Java中的线程乘以2个5x5矩阵。我尝试使用一个线程,一次一行一列地执行乘法,但是线程不执行。我如何使用更多的线程来完成它?这是密码 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(
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]);
}
}
}
公共类矩阵{
私人INTA[]];
私有int B[][];
私有INTC[][];
私有int r1,c1;
私人int r2,c2;
public int[]getC(){
返回C;
}
公共无效setC(int[][]c){
C=C;
}
公共int getR1(){
返回r1;
}
公共无效集合r1(int r1){
这是r1=r1;
}
公共int getC1(){
返回c1;
}
公共无效集合c1(内部集合c1){
这1.c1=c1;
}
公共int getR2(){
返回r2;
}
公共无效集合r2(整数r2){
这是r2=r2;
}
公共int getC2(){
返回c2;
}
公共无效集合c2(内部集合c2){
这1.c2=c2;
}
公共int[]getA(){
返回A;
}
公共无效setA(int[]a){
A=A;
}
public int[]getB(){
返回B;
}
公共无效收进(int[]b){
B=B;
}
同步无效mul(整数r,整数c){
整数和=0;
对于(int i=0;i 对于代码段中的(inti=0;i),将出现无限循环
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();
}
}
public void run(){
试一试{
while(true){//无限循环-从未终止。
对于(int i=0;i
在代码段中出现无限循环
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();
}
}
public void run(){
试一试{
while(true){//无限循环-从未终止。
对于(int i=0;i
这种方法有一个非常简单的问题:在显示任何结果之前,您必须等待线程完成。到目前为止,您正在打印矩阵,而计算仍在进行中running@Paul我尝试添加一个join()语句,但线程似乎没有结束,程序在没有运行最终打印语句的情况下被卡住。这是一个非常明显的迹象,表明实际问题在乘法的代码中的某个地方。仔细看看,run
方法包含一个内插循环(while(true)
没有任何中断或返回)@Paul我添加的代码会并行运行5个线程吗?这不是多线程的工作方式。基本上,这段代码生成5个线程,所有线程都乘以完整的矩阵,这只是浪费资源。所有线程都做完全相同的事情,所以是的,它并行运行5个线程,但用途在哪里?有一个非常简单的问题lem使用这种方法:在显示任何结果之前,您必须等待线程完成。到目前为止,您正在打印矩阵,而计算仍在进行running@Paul我尝试添加一个join()语句,但线程似乎没有结束,程序在没有运行最终打印语句的情况下被卡住。这是一个非常明显的迹象,表明实际问题在乘法的代码中的某个地方。仔细看看,run
方法包含一个内插循环(while(true)
没有任何中断或返回)@Paul我添加的代码会并行运行5个线程吗?这不是多线程的工作方式。基本上,这段代码生成5个线程,所有线程都乘以完整的矩阵,这只是浪费资源。所有线程都做完全相同的事情,所以是的,它并行运行5个线程,但这有什么用呢?
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();
}
}