Java中的隐式和显式并行 [英] Implicit and Explicit Parallelism in Java

查看:53
本文介绍了Java中的隐式和显式并行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在研究Java语言中的隐式和显式并发之前,经过大量研究后想弄清楚一些事情.

Been looking into Implicit and Explicit concurrency within the Java language and after a fair bit of research wanted to clear a few things up.

  • 仅通过使用Java 8流来实现Java语言中的隐式并行性吗?
  • 我正确地认为Java中的显式并行性是通过使用程序员编写的线程来实现的.(区别是隐式并行性应该通过编译器而不是程序员编写的代码来实现)?
  • Is Implicit parallelism in the Java language implemented only through the use of Java 8 streams?
  • Am I right in thinking that Explicit parallelism in Java is implemented through the use of threads written by the programmer. (The difference being Implicit parallelism should happen via the compiler and not the programmers written code)?

谢谢

推荐答案

隐式并行性是指编译器能够并行执行某些操作而无需程序员提供任何"提示的能力.如果要执行的计算具有某些属性(例如,并行作业之间没有数据依赖性),则可以实现此目的.

Implicit parallelism refers to the ability of the compiler to perform certain operation in parallel without "any" hint from the programmer. This is achievable if the computation you want to perform has certain properties (e.g. no data dependencies among the parallel jobs).

Java流是(引用Oracle的文档)一系列支持顺序和并行聚合操作的元素,并且与集合根本不同,因为

Java stream are (quoting Oracle's doc) A sequence of elements supporting sequential and parallel aggregate operations and are fundamentally different to collections because

1)集合旨在快速访问和操纵集合的元素

1) Collections were designed to be fast in accessing and manipulated collection's elements

2) Stream 并非旨在进行修改,而是在源元素和结果元素之间创建某种管道.通过对源应用操作(通常以函数/lambda样式描述)来创建结果元素.这种计算可以安全地划分为子管道并以并行方式进行处理.

2) Streams are not meant to be modified, but to create a sort of pipe between source elements and result elements. Result elements are created applying an operation (usually described in functional/lambda-style) to the source. This kind of computation can be safely divided into sub pipes and processed in a parallel way.

组成此管道的函数必须具有以下属性:1)必须是无干扰的(它们不修改流源);2)是无状态的(其结果不应依赖于在执行流水线过程中可能改变的任何状态).请参见此处

Function that make up this pipe have to have the following properties: 1) must be non-interfering (they do not modify the stream source); 2) be stateless (their result should not depend on any state that might change during execution of the stream pipeline). see here

显式并行性(顾名思义)是由程序员实现的,程序员可以根据需要生成尽可能多的(计算元素).这些元素通常是线程或进程(

Explicit parallelism (as the name suggest) is achieved by the programmer that can spawn as much (the computing elements) as he needs.Those elements are usually threads or processes (see here for the difference Those computing elements execute concurrently and is up to the programmer to orchestrate all the operations in order to produce the right result (e.g. explicitly synchronize threads, exchanging messages, make transactional operations etc.).

这篇关于Java中的隐式和显式并行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆