简体   繁体   English

并行化(Java 8)与并发(Java 7)

[英]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来处理数据,而不是直接使用并发库,因为;

  • a functional style of coding discourages shared mutable state. 编码的一种功能风格不鼓励共享的可变状态。 (Actually how are not supposed to have an mutable state in functional programming but Java is not really a functional language) (实际上,在函数式编程中不应该具有可变状态,但是Java并不是真正的函数式语言)
  • it's easier to write and understand for processing data. 它更易于编写和理解以处理数据。
  • it's easier to test whether using parallel helps or not. 测试是否使用并行帮助会更容易。 Most likely ti won't and you can just as easily change it back to being serial. ti很可能不会,您可以轻松地将其更改回串行状态。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM