[英]Parallelization (Java 8) vs Concurrency (Java 7)
I want to parse multiple files to extract the required data and then write the output into an XML
file. 我想解析多个文件以提取所需的数据,然后将输出写入XML
文件。 I have used Callable Interface
to implement this. 我已经使用Callable Interface
来实现这一点。 My colleague asked me to use Java 8
feature which does this job easily. 我的同事要求我使用Java 8
功能,该功能可以轻松完成此任务。 I am really confused which one of them I should use now. 我真的很困惑,我现在应该使用其中之一。
list.parallelStream().forEach(a -> {
System.out.println(a);
});
Using concurrency or a parallel stream only helps if you have independent tasks to work on. 仅当您要处理独立任务时,才使用并发或并行流。 A good example of when you wouldn't do this is what you are locking on a shared resources eg 一个您什么时候不愿意做的很好的例子是您锁定共享资源,例如
// makes no sense to use parallel here.
list.parallelStream().forEach(a -> {
// locks System.out so only one thread at a time can do any work.
System.out.println(a);
});
However, as a general question, I would use parallelStream for processing data, instead of the concurrency libraries directly because; 但是,作为一个普遍的问题,我将使用parallelStream来处理数据,而不是直接使用并发库,因为;
IMHO Given the chances that using parallel coding will really help is low, the best feature of parallelStream
is not how simple it is to add, but how simple it is to take out. 恕我直言,鉴于使用并行编码确实有帮助的机会很低, parallelStream
的最佳功能不是添加的简单,而是取出的简单。
The concurrency library is better if you have ad hoc work which is difficult to model as a stream of data. 如果您的临时工作很难建模为数据流,那么并发库会更好。 eg a worker pool for client requests might be simplier to implement using an ExecutorService. 例如,使用ExecutorService可能更容易实现用于客户端请求的工作池。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.