用循环遍历一个线程并处理多个结果
Iterate through a thread with loop and process multiple results
我正在尝试使用线程使我的程序 运行 并行运行,但我很挣扎。
目标是处理链接列表,urlList
通过 ImageProcessor().processLink
函数。我有两个问题要解决:
- 我如何循环它以便它在池中使用 N 个线程,在本例中为 10 个?也就是我想一次处理10个链接。
- 上面的处理函数returns一个
File
,我需要添加到一个数组,fileList
。说到多线程,我将如何处理?
这是我目前得到的:
ArrayList<String> urlList = new ArrayList<>(Arrays.asList(arr.split("\r?\n"))) ;
ArrayList<File> fileList = new ArrayList<>();
ExecutorService executor = Executors.newFixedThreadPool(10);
//process the requested files
for (int i = 0; i < urlList.size(); i++){
Future<File> value = executor.submit(new Callable<File>() {
@Override
public File call(int i) throws IOException {
return new ImageProcessor().processLink(urlList.get(i));
}
});
try {
fileList.add(value.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
得到它与以下工作:
ArrayList<String> urlList = new ArrayList<>(Arrays.asList(arr.split("\r?\n"))) ;
ArrayList<File> fileList = new ArrayList<>();
ExecutorService executor = Executors.newFixedThreadPool(THREAD_SIZE);
List<Future<File>> futures = new ArrayList<>();
for (int i = 0; i < urlList.size(); i++) {
ImageProcessor proc = new ImageProcessor(urlList.get(i));
final Future<File> future = executor.submit(proc);
futures.add(future);
}
try {
executor.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < futures.size(); i++)
{
Future<File> result = futures.get(i);
try {
fileList.add(result.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
executor.shutdown();
while (!executor.isTerminated()) { }
System.out.println("Finished all threads");
我正在尝试使用线程使我的程序 运行 并行运行,但我很挣扎。
目标是处理链接列表,urlList
通过 ImageProcessor().processLink
函数。我有两个问题要解决:
- 我如何循环它以便它在池中使用 N 个线程,在本例中为 10 个?也就是我想一次处理10个链接。
- 上面的处理函数returns一个
File
,我需要添加到一个数组,fileList
。说到多线程,我将如何处理?
这是我目前得到的:
ArrayList<String> urlList = new ArrayList<>(Arrays.asList(arr.split("\r?\n"))) ;
ArrayList<File> fileList = new ArrayList<>();
ExecutorService executor = Executors.newFixedThreadPool(10);
//process the requested files
for (int i = 0; i < urlList.size(); i++){
Future<File> value = executor.submit(new Callable<File>() {
@Override
public File call(int i) throws IOException {
return new ImageProcessor().processLink(urlList.get(i));
}
});
try {
fileList.add(value.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
得到它与以下工作:
ArrayList<String> urlList = new ArrayList<>(Arrays.asList(arr.split("\r?\n"))) ;
ArrayList<File> fileList = new ArrayList<>();
ExecutorService executor = Executors.newFixedThreadPool(THREAD_SIZE);
List<Future<File>> futures = new ArrayList<>();
for (int i = 0; i < urlList.size(); i++) {
ImageProcessor proc = new ImageProcessor(urlList.get(i));
final Future<File> future = executor.submit(proc);
futures.add(future);
}
try {
executor.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < futures.size(); i++)
{
Future<File> result = futures.get(i);
try {
fileList.add(result.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
executor.shutdown();
while (!executor.isTerminated()) { }
System.out.println("Finished all threads");