gpt4 book ai didi

java - 使用 10 个线程处理一个数组

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:08:48 26 4
gpt4 key购买 nike

我正在努力提高我的 Java 技能,但有点不确定如何处理这个多线程应用程序。基本上,该程序读取一个文本文件并找到最大的数字。我在我的搜索算法中添加了一个 for 循环来创建 10 个线程,但我不确定它是否真的创建了 10 个线程。这个想法是为了缩短执行时间,或者至少这是我认为应该发生的事情。有没有办法检查我是否做对了以及执行时间是否确实有所改善?

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class ProcessDataFile {

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

int max = Integer.MIN_VALUE;
int i = 0;
int[] numbers = new int[100000];
String datafile = "dataset529.txt"; //string which contains datafile
String line; //current line of text file

try (BufferedReader br = new BufferedReader(new FileReader(datafile))) { //reads in the datafile
while ((line = br.readLine()) != null) { //reads through each line
numbers[i++] = Integer.parseInt(line); //pulls out the number of each line and puts it in numbers[]
}
}

for (i = 0; i < 10000; i++){ //loop to go through each number in the file and compare it to find the largest int.
for(int j = 0; j < 10; j++) { //creates 10 threads
new Thread();
}
if (max < numbers[i]) //As max gets bigger it checks the array and keeps increasing it as it finds a larger int.
max = numbers[i]; //Sets max equal to the final highest value found.
}


System.out.println("The largest number in DataSet529 is: " + max);
}
}

最佳答案

这是一个非常基本的示例,它演示了创建和运行线程的基本概念,这些线程处理特定数组中给定范围的值。该示例做了一些假设(例如只有偶数个元素)。这个例子也有点冗长,是故意这样做的,目的是为了演示所需的基本步骤

先看看 the Concurrency Trail了解更多详情

import java.util.Random;

public class ThreadExample {

public static void main(String[] args) {
int[] numbers = new int[100000];
Random rnd = new Random();
for (int index = 0; index < numbers.length; index++) {
numbers[index] = rnd.nextInt();
}

Thread[] threads = new Thread[10];
Worker[] workers = new Worker[10];

int range = numbers.length / 10;
for (int index = 0; index < 10; index++) {
int startAt = index * range;
int endAt = startAt + range;
workers[index] = new Worker(startAt, endAt, numbers);
}

for (int index = 0; index < 10; index++) {
threads[index] = new Thread(workers[index]);
threads[index].start();
}

boolean isProcessing = false;
do {
isProcessing = false;
for (Thread t : threads) {
if (t.isAlive()) {
isProcessing = true;
break;
}
}
} while (isProcessing);

for (Worker worker : workers) {
System.out.println("Max = " + worker.getMax());
}

}

public static class Worker implements Runnable {

private int startAt;
private int endAt;
private int numbers[];

private int max = Integer.MIN_VALUE;

public Worker(int startAt, int endAt, int[] numbers) {
this.startAt = startAt;
this.endAt = endAt;
this.numbers = numbers;
}

@Override
public void run() {
for (int index = startAt; index < endAt; index++) {
max = Math.max(numbers[index], max);
}
}

public int getMax() {
return max;
}

}

}

一个稍微简单的解决方案将涉及 ExecutorService API,它允许您向服务提供一系列 Callable,然后返回一个 List future 。这里的好处是,在所有 Callable 完成(或失败)之前服务不会返回,因此您不需要经常检查线程的状态

import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class ThreadExample {

public static void main(String[] args) {
int[] numbers = new int[100000];
Random rnd = new Random();
for (int index = 0; index < numbers.length; index++) {
numbers[index] = rnd.nextInt();
}

ExecutorService executor = Executors.newFixedThreadPool(10);

Worker[] workers = new Worker[10];

int range = numbers.length / 10;
for (int index = 0; index < 10; index++) {
int startAt = index * range;
int endAt = startAt + range;
workers[index] = new Worker(startAt, endAt, numbers);
}

try {
List<Future<Integer>> results = executor.invokeAll(Arrays.asList(workers));
for (Future<Integer> future : results) {
System.out.println(future.get());
}
} catch (InterruptedException | ExecutionException ex) {
ex.printStackTrace();
}

}

public static class Worker implements Callable<Integer> {

private int startAt;
private int endAt;
private int numbers[];


public Worker(int startAt, int endAt, int[] numbers) {
this.startAt = startAt;
this.endAt = endAt;
this.numbers = numbers;
}

@Override
public Integer call() throws Exception {
int max = Integer.MIN_VALUE;
for (int index = startAt; index < endAt; index++) {
max = Math.max(numbers[index], max);
}
return max;
}

}

}

关于java - 使用 10 个线程处理一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33358302/

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