Java 数组上的多线程(分割)

标签 java multithreading matrix split

我正在寻找一种在线程系统(主从)中使用数组的解决方案,它允许我通过用户输入在多个线程上划分矩阵的计算,并将其通过 1 个主线程引导到多个从属线程,这些从属线程计算矩阵的 1 个字段。

我尝试运用我的知识,但我只是将问题转移到方法中。 该代码可以工作,但仅使用 1 个线程(接着另一个线程),而不是同时使用所有线程。

我想我可以以某种方式划分矩阵(这是一个 Spliterator 吗?),但不知 Prop 体如何划分。

如果结果字段为空,也尝试使用 boolean 值,但它根本不起作用。

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;

class Threadverteiler extends Thread {

public Threadverteiler(Thread[] threads) {
    synchronisiert(threads);
}

public void synchronisiert(Thread[] t) {

    Thread[] threads = t;
    synchronized (threads) {
        for (int i = 0; i < threads.length; i++) {

            threads[i].start();
            System.out.println("Thread " + i + " gestartet");
        } // TODO Auto-generated constructor stub

    }
    }

}

class Threads extends Thread {

    public Threads(int[][] a, int[][] b) {
        run(a, b);
    }

    /*
     * public Threadverteiler(Thread[] c, int[][] a,int[][]b) { //start();
     * run(a, b); //Arrays.spliterator(a);
     * //System.out.println(Arrays.spliterator(a));
     * 
     * }
     */

    public void run(int[][] a, int[][] b) {
        // synchronized (this) {

        // boolean arrayleer = false;

        // int[][]cc= new int[5][5];

        // public int[][] rechnen(int[][] a,int[][]b){

        // while( arrayleer==true){
        int[][] aa = new int[5][5];
        int[][] bb = new int[5][5];
        int[][] cc = new int[5][5];

        aa = a;
        bb = b;

        for (int i = 0; i < aa.length; i++)

        {

            for (int j = 0; j < bb.length; j++)

            {

                for (int k = 0; k < cc.length; k++)

                {

                    cc[i][j] = cc[i][j] + aa[i][k] * bb[k][j];
                    // cc[i][j] = cc[i][j] + a[i][k] * b[k][j];

                    /*
                     * if(cc[i][j]==0){ i++; j++; k++; continue; } else {
                     * System.out.println("Andere thread"); break;
                     * //arrayleer=false; }
                     */

                }

            }

        }

        /*
         * try { PrintWriter print = new PrintWriter(new File());
         * 
         * } catch (Exception e) { // TODO: handle exception } return cc;
         * 
         * 
         * } }
         */

        // c=cc;
        // System.out.println("active Threads " + activeCount());

        System.out.println(Arrays.deepToString(cc));
        System.out.println("active Threads " + activeCount());

    }

}

// }

public class Uebung2 {

    public static void main(String[] args) {

        int[][] a = { { 1, -2, 3, 4, -1 }, { -2, 3, 0, 1, 2 }, { 4, -1, 2, 1, -2 }, { -2, 1, 3, -1, 3 },
                { 0, 2, -1, 2, 4 } };
        int[][] b = { { 2, -4, -1, 1, -2 }, { -1, 1, -2, 2, 1 }, { 5, 0, 3, -2, -4 }, { 1, -2, 1, 0, 2 },
                { 2, 3, -3, 0, 0 } };

        // int[][] c= new int[5][5];

        // System.out.println(Arrays.deepToString(a));
        // System.out.println(Arrays.deepToString(b));

        // public static void threadauswahl(int x){

        // int i = 0;

        System.out.println("Bitte Anzahl Threads eingeben");

        Scanner sc = new Scanner(System.in);
        int eingabe = sc.nextInt();
        sc.close();

        Thread[] threads = new Thread[eingabe];

        // while (threads[i]==null) {

        for (int i = 0; i < threads.length; i++) {
            threads[i] = new Thread(new Threads(a, b));

            // threads[i].start(); // !!!!!!!!!!!!!!!!! ändern!!
            // i++;
        }

        Threadverteiler s = new Threadverteiler(threads);

    }

}

// System.out.println(Arrays.deepToString(a));
// System.out.println(Arrays.deepToString(b));
// System.out.println(Arrays.deepToString(c));

最佳答案

如果我理解正确的话,这个设计已经在 API 中实现了,通过 ExecutorServiceFuture 。这些将允许您的 master (ExecutorService) 控制任务列表,每个任务都包含矩阵的一部分,而 Futures 在完成时报告。您仍然需要分解每个 future 任务的矩阵,但应该是微不足道的。

关于Java 数组上的多线程(分割),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36203122/

相关文章:

c# - 将 Interlocked.CompareExchange 与类一起使用

algorithm - 8 点算法的基本矩阵

java访问客户端文件系统

java - 如何使用 JSTL 根据 Map 中的键显示多个表?

Java 不接受 Double 变量的十进制数

mysql - 确保 MySQL 中的 auto_increment 值排序

Python - 在不同的线程上发送带有相同附件的多封电子邮件

python - 为 "Lights out"变体生成切换矩阵

C++矩阵计算

java - tcp客户端和服务器不互相接收数据