Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implicit and Explicit Parallelism in 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.

  • 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)?

Thanks

like image 504
jjharrison Avatar asked Jan 14 '16 18:01

jjharrison


1 Answers

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 stream are (quoting Oracle's doc) A sequence of elements supporting sequential and parallel aggregate operations and are fundamentally different to collections because

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

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.

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.).

like image 85
Davide Spataro Avatar answered Nov 02 '22 01:11

Davide Spataro