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

时间:2016-03-24 14:51:57

标签: java multithreading matrix split

我正在寻找在线程系统(主从)中使用数组的解决方案,它允许我通过用户输入在多个线程上划分矩阵的计算,并通过1个主线程将其指向计算1的多个从属线程矩阵的领域。

我试图运用我的知识,但我只是在方法中转移问题。 代码有效,但只使用1个线程(接一个)而不是同时使用。

以为我可以用某种方式划分矩阵(这是一个Spliterator吗?)但不知道究竟是怎么回事。

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

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));

2 个答案:

答案 0 :(得分:0)

如果我正确理解你的问题,下面应该启动多个线程

public class Main {

    public static void main(final String[] args) {

        final 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 } };
        final 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 } };

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

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

        final Thread[] threads = new Thread[eingabe];

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

        final Threadverteiler s = new Threadverteiler(threads);

    }

}

class Threadverteiler extends Thread {

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

    public void synchronisiert(final Thread[] t) {

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

                threads[i].start();
                System.out.println("Thread " + i + " gestartet");
            }

            for (int i = 0; i < threads.length; i++) {
                try {
                    threads[i].join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}

class Threads extends Thread {

    private final int[][] a;
    private final int[][] b;

    public Threads(final int[][] a, final int[][] b) {
        this.a = a;
        this.b = b;

    }

    @Override
    public void run() {
        int[][] aa = new int[5][5];
        int[][] bb = new int[5][5];
        final 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];
                }

            }

        }

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

    }

}

答案 1 :(得分:0)

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