并行处理如何使用Threadpool并行化大数据的java源代码
我尝试用Java中的ExecutorService
和LinkedList
来并行一些源代码
在下面的源代码中,对文本文件的每一行都进行了一些处理
当每一行相对较短时,以下源代码运行良好(读取所有行并完成进程)
然而,当它相对较长时,程序会停止读取一行而不会出现任何错误
有^{
ExecutorService threadPool = Executors.newFixedThreadPool(4);
Collection<Callable<Void>> processes = new LinkedList<Callable<Void>>();
int c = 0;
while ((string = br.readLine()) != null) {
final String str = string;
processes.add(new Callable<Void>() {
@Override
public Void call() {
// some process
return null;
}
});
}
try {
threadPool.invokeAll(processes);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
System.out.println("parallel finishes");
threadPool.shutdown();
}
共 (0) 个答案