gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 12:47:07 28 4
gpt4 key购买 nike

我正在寻找一种在线程系统(主从)中使用数组的解决方案,它允许我通过用户输入在多个线程上划分矩阵的计算,并将其通过 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/

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