gpt4 book ai didi

java - 如何显示类(class)的结果?

转载 作者:行者123 更新时间:2023-11-29 05:16:53 30 4
gpt4 key购买 nike

我一直在研究一个使用线程将矩阵相乘的程序。我以非线程方式编写程序,它运行起来非常棒。但是,当我使用线程函数编写它时,似乎我没有从 Threading 类中获得结果(稍后我将重命名它)。此外,如果我要使用 1 个线程,我将获得所有 3 个矩阵,然后是 matC 矩阵的空结果。任何大于 1 的值都会使我的数组索引超出范围。

使用类和诸如此类的东西仍然很无能,我提前道歉有点罗嗦。但我们将不胜感激。

主类:

package matrixthread;

import java.io.*;
import java.util.*;

public class MatrixThread extends Thread{

public static void matrixPrint(int[][] A) {
int i, j;
for (i = 0; i < A.length; i++) {
for (j = 0; j < A.length; j++) {
System.out.print(A[i][j] + " ");
}
System.out.println("");
}
}

public static void matrixFill(int[][] A) {
int i, j;
Random r = new Random();
for (i = 0; i < A.length; i++) {
for (j = 0; j < A.length; j++) {
A[i][j] = r.nextInt(10);
}
}
}

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int threadCounter = 0;


System.out.print("Enter number of rows: ");
int M = in.nextInt();
System.out.print("Enter number of threads: ");
int N = in.nextInt();
Thread[] thrd = new Thread[N];


if (M < N)
{
System.out.println("Error! Numbers of rows are greater than number of threads!");
System.exit(0);
}

if(M % N != 0)
{
System.out.println("Error! Number of rows and threads aren't equal");
System.exit(0);
}


int[][] matA = new int[M][M];
int[][] matB = new int[M][M];
int[][] matC = new int[M][M];

try
{
for (int x = 0; x < N; x ++)
for (int y = 0; y < N; y++)
{
thrd[threadCounter] = new Thread(new Threading(matA, matB, matC));
thrd[threadCounter].start();
thrd[threadCounter].join();
threadCounter++;
}
}
catch(InterruptedException ie){}


long startTime = (int)System.currentTimeMillis();
matrixFill(matA);
matrixFill(matB);
//mulMatrix(matA, matB, matC);
long stopTime = (int)System.currentTimeMillis();

int execTimeMin = (int) ((((stopTime - startTime)/1000)/60)%60);
int execTimeSec = ((int) ((stopTime - startTime)/1000)%60);


System.out.println("\n" + "Matrix 1: ");
matrixPrint(matA);
System.out.println("\n" + "Matrix 2: ");
matrixPrint(matB);
System.out.println("\n" + "Results: ");
matrixPrint(matC);


System.out.println("\n" + "Finish: " + execTimeMin + "m " + execTimeSec + "s");

}

}

这是我的线程类:

package matrixthread;

public class Threading implements Runnable {

//private int N;
private int[][] matA;
private int[][] matB;
private int[][] matC;

public Threading(int matA[][], int matB[][], int matC[][]) {
this.matA = matA;
this.matB = matB;
this.matC = matC;
}

@Override
public void run() {
int i, j, k;
for (i = 0; i < matA.length; i++) {
for (j = 0; j < matA.length; j++) {
for (k = 0; k < matA.length; k++) {
matC[i][j] += matA[i][k] * matB[k][j];
}
}
}
}
}

这是我使用 2 行和 1 个线程的结果

矩阵 1:7 84 5

矩阵 2:3 01 5

结果:0 00 0

最佳答案

你这里有很多问题。首先也是最重要的是这个循环:

for (int x = 0; x < N; x ++)
for (int y = 0; y < N; y++) {
thrd[threadCounter] = new Thread(new Threading(matA, matB, matC));
thrd[threadCounter].start();
thrd[threadCounter].join();
threadCounter++;
}

调用matrixFill 之前运行此循环。 matAmatB此时都等于零矩阵。

然后,对于每个 Thread,调用等待完成的 join()。所以你所做的只是 0 * 0 = 0 N2 次。

在循环中创建线程并在线程上调用 join() 创建 等待每个线程完成。这意味着您永远不会同时运行两个任务。这不是线程,这是以非常复杂的方式使用单个线程执行某些操作。

然后您填写matAmatB 并打印出所有三个。不出所料,matC 仍然等于零矩阵。

你的下一个问题是你的线程:

public void run() {
int i, j, k;
for (i = 0; i < matA.length; i++) {
for (j = 0; j < matA.length; j++) {
for (k = 0; k < matA.length; k++) {
matC[i][j] += matA[i][k] * matB[k][j];
}
}
}
}

每个线程都运行这段代码。此代码将两个矩阵相乘。像您一样运行此代码 N2 次只会使单个矩阵乘法慢 N2 倍。

如果您修复线程(见上文)以便所有线程并发运行,那么您将面临自 Java 创建以来最大的竞争风险。我非常怀疑您永远不会得到正确的结果。

TL;DR matC 为零的原因是,当发生乘法时,maA == matB == 0

关于java - 如何显示类(class)的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26230460/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com