Java 使用多个线程查找数组的和
这是我第一次尝试使用多线程,希望您能给予帮助 以下问题: 用户向控制台输入数组的值以及所需的线程数量。数组应该使用这些线程求和,每个线程在数组中找到自己的块的和。应为每个线程打印求和结果,并覆盖求和Java 使用多个线程查找数组的和,java,arrays,multithreading,Java,Arrays,Multithreading,这是我第一次尝试使用多线程,希望您能给予帮助 以下问题: 用户向控制台输入数组的值以及所需的线程数量。数组应该使用这些线程求和,每个线程在数组中找到自己的块的和。应为每个线程打印求和结果,并覆盖求和 public class Thread1 extends Thread{ @Override public void run() { System.out.println("Enter the required size of the array ::
public class Thread1 extends Thread{
@Override
public void run() {
System.out.println("Enter the required size of the array :: ");
Scanner s = new Scanner(System.in);
int size = s.nextInt();
int[] myArray = new int [size];
System.out.println("Enter the required quantity of the threads :: ");
int k = s.nextInt();
int sum = 0;
System.out.println("Enter the elements of the array one by one ");
for(int i=0; i<size; i++) {
myArray[i] = s.nextInt();
for(int j = 0; j< k; j++){
sum = sum + myArray[i];
}
System.out.println("Sum for thread: " + this.getName() + " = " + sum);
}
System.out.println("Elements of the array are: "+ Arrays.toString(myArray));
System.out.println("Sum of the elements of the array ::"+sum);
}
public static void main(String[] args) {
Thread1 t = new Thread1();
t.start();
}
}
公共类Thread1扩展线程{
@凌驾
公开募捐{
System.out.println(“输入所需的数组大小::”;
扫描仪s=新的扫描仪(System.in);
int size=s.nextInt();
int[]myArray=新的int[size];
System.out.println(“输入所需的线程数量:”;
int k=s.nextInt();
整数和=0;
System.out.println(“逐个输入数组的元素”);
对于(int i=0;iHi Yakov,欢迎来到SO。请你告诉我们一个具体的问题,并提供一个样本。我建议你阅读这个线程,尝试一下,然后,通过一些工作示例,我们将尝试帮助你。呃,我的意思是我知道如何找到数组的和。我不明白的是如何将数组的块分配给每个线程(对不起,如果我没有说清楚,英语不是我的母语)。我添加了一些更改,但我认为这不正确