【问题标题】:Multiply three matrices using threads in java在java中使用线程将三个矩阵相乘
【发布时间】:2016-11-06 22:02:14
【问题描述】:

我正在尝试使用线程将三个矩阵相乘。我使用多个线程进行矩阵乘法,我应该使用其他数量的线程将两个矩阵的结果与最后一个矩阵相乘,我想在线程开始有数据后立即启动第二次乘法从第一个乘法结果。我有点卡住了。 我试过的:

public class Threads implements Runnable{
private String action;
private int range;

public Threads(String action, int range) {
    this.action = action;
    this.range = range +1;

}

public synchronized void run() {
    int k;
    int counter = 0;
    int m_size = Program.m1.length;
    int k_size =Program.m1[0].length;
    int n_size = Program.m2[0].length;
    int h_size = Program.m3[0].length;

        for (int i=0; i<m_size; i++)
            for (int j=0; j<n_size; j++) {
                counter++;
                if (counter % range == 0) {
                    int val1 = 0;
                    for (k = 0; k < k_size; k++) {
                        Program.result[i][j] += Program.m1[i][k] * Program.m2[k][j];
                       // int val1 =0;
                        for (int h=0; h<h_size; h++){
                            val1 += Program.result[i][h] * Program.m3[h][j];
                        }
                    }
                    Program.result_matrix[i][j] = val1; //here run error
                }
            }
    }


public class Program {
private final static Random rand = new Random();

public static int[][] m1 = new int[][]{};

public static int[][] m2 = new int[][]{};
public static int[][] m3 = new int[][]{};
public static int[][] result = new int[][]{};
public static int[][] result_matrix = new int[][]{};

public static void generate(String action, int number_of_threads) throws InterruptedException {
    List<Thread> all_threads = new ArrayList<>();
    for (int th = 0; th < number_of_threads; th++) {
        Thread t = new Thread(new Threads(action, th));
        all_threads.add(t);
        t.start();
    }

    for (Thread t : all_threads) {
        t.join();
    }

}

public static void main(String[] args) throws Exception {

    { //here i m reading the matrices
        generate(action, number_of_threads);
        print_result_matrix();
          public static void print_result_matrix() {
    for (int i = 0; i < result_matrix.length; i++) {
        for (int j = 0; j < result_matrix[0].length; j++)
            System.out.print(result_matrix[i][j] + "  ");

        System.out.println();
    }
 }

当我运行这个程序时,在设置矩阵行和列后出现错误,并在 //look at code 行出现错误。 无论如何有一些想法?我知道我的代码最后在run() 方法中没有逻辑,但我真的不知道如何实现这一点。 有什么想法吗?

堆栈跟踪:

Exception in thread "Thread-0" Exception in thread "Thread-1 java.lang.ArrayIndexOutOfBoundsException: 0 
    at Threads.run(Threads.java:43) 
    at java.lang.Thread.run(Thread.java:745) java.lang.ArrayIndexOutOfBoundsException: 0 
    at Threads.run(Threads.java:43) 
    at java.lang.Thread.run(Thread.java:745)

【问题讨论】:

  • 在此处添加异常堆栈跟踪
  • 线程“Thread-0”中的异常 线程“Thread-1”中的异常 java.lang.ArrayIndexOutOfBoundsException: 0 at Threads.run(Threads.java:43) at java.lang.Thread.run( Thread.java:745) java.lang.ArrayIndexOutOfBoundsException: 0 at Threads.run(Threads.java:43) at java.lang.Thread.run(Thread.java:745)
  • 很抱歉这样显示

标签: java multithreading matrix


【解决方案1】:

您需要初始化结果矩阵的大小:

public static int[][] result = new int[m1.length][m2[0].length];
public static int[][] result_matrix = new int[result.length][m3[0].length];

当然,这应该在m1、m2和m3填充数据之后进行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-19
    • 1970-01-01
    相关资源
    最近更新 更多