pros & cons of java 8 parallel streams - vanderbilt...
TRANSCRIPT
![Page 1: Pros & Cons of Java 8 Parallel Streams - Vanderbilt Universityschmidt/cs891f/2018-PDFs/20-pros... · 2018-10-29 · Pros Cons Cons of Java 8 Parallel Streams •In general, however,](https://reader030.vdocuments.us/reader030/viewer/2022041000/5e9fd7c1c914ae4272103cd6/html5/thumbnails/1.jpg)
Pros & Cons of Java 8 Parallel Streams
Douglas C. [email protected]
www.dre.vanderbilt.edu/~schmidt
Professor of Computer Science
Institute for Software
Integrated Systems
Vanderbilt University
Nashville, Tennessee, USA
![Page 2: Pros & Cons of Java 8 Parallel Streams - Vanderbilt Universityschmidt/cs891f/2018-PDFs/20-pros... · 2018-10-29 · Pros Cons Cons of Java 8 Parallel Streams •In general, however,](https://reader030.vdocuments.us/reader030/viewer/2022041000/5e9fd7c1c914ae4272103cd6/html5/thumbnails/2.jpg)
2
Learning Objectives in this Lesson• Evaluate the pros & cons of Java 8
parallel streams
![Page 3: Pros & Cons of Java 8 Parallel Streams - Vanderbilt Universityschmidt/cs891f/2018-PDFs/20-pros... · 2018-10-29 · Pros Cons Cons of Java 8 Parallel Streams •In general, however,](https://reader030.vdocuments.us/reader030/viewer/2022041000/5e9fd7c1c914ae4272103cd6/html5/thumbnails/3.jpg)
3
Pros of Java 8 Parallel Streams
![Page 4: Pros & Cons of Java 8 Parallel Streams - Vanderbilt Universityschmidt/cs891f/2018-PDFs/20-pros... · 2018-10-29 · Pros Cons Cons of Java 8 Parallel Streams •In general, however,](https://reader030.vdocuments.us/reader030/viewer/2022041000/5e9fd7c1c914ae4272103cd6/html5/thumbnails/4.jpg)
4
• The Java 8 streams framework simplifiesparallel programming by shieldingdevelopers from details of splitting, applying, & combining results
join join
join
Processsequentially
Processsequentially
Processsequentially
Processsequentially
InputString1.1 InputString1.2 InputString2.1 InputString2.2
InputString1 InputString2
trySplit()
InputString
trySplit() trySplit()
Pros of Java 8 Parallel Streams
![Page 5: Pros & Cons of Java 8 Parallel Streams - Vanderbilt Universityschmidt/cs891f/2018-PDFs/20-pros... · 2018-10-29 · Pros Cons Cons of Java 8 Parallel Streams •In general, however,](https://reader030.vdocuments.us/reader030/viewer/2022041000/5e9fd7c1c914ae4272103cd6/html5/thumbnails/5.jpg)
5
• Parallel stream implementations are often(much) faster & more scalable than sequential (stream & loops) implementations
Pros of Java 8 Parallel Streams
Starting SearchStreamGangTestPARALLEL_SPLITERATOR executed in 409 msecsCOMPLETABLE_FUTURES_INPUTS executed in 426 msecsCOMPLETABLE_FUTURES_PHASES executed in 427 msecsPARALLEL_STREAMS executed in 437 msecsPARALLEL_STREAM_PHASES executed in 440 msecsRXJAVA_PHASES executed in 485 msecsPARALLEL_STREAM_INPUTS executed in 802 msecsRXJAVA_INPUTS executed in 866 msecsSEQUENTIAL_LOOPS executed in 1638 msecsSEQUENTIAL_STREAM executed in 1958 msecsEnding SearchStreamGangTest
45,000+ phrases
Search Phrases
Input Strings to Search
…
![Page 6: Pros & Cons of Java 8 Parallel Streams - Vanderbilt Universityschmidt/cs891f/2018-PDFs/20-pros... · 2018-10-29 · Pros Cons Cons of Java 8 Parallel Streams •In general, however,](https://reader030.vdocuments.us/reader030/viewer/2022041000/5e9fd7c1c914ae4272103cd6/html5/thumbnails/6.jpg)
6
• The performance speedup is a largely a function of the partitioning strategy for the input (N), the amount of work performed (Q), & the # of cores
The NQ model
• N is the # of data elements to process per thread
• Q quantifies how CPU-intensive the processing is
N
hilo
lo
hi
Q
Ideal
Pros of Java 8 Parallel Streams
![Page 7: Pros & Cons of Java 8 Parallel Streams - Vanderbilt Universityschmidt/cs891f/2018-PDFs/20-pros... · 2018-10-29 · Pros Cons Cons of Java 8 Parallel Streams •In general, however,](https://reader030.vdocuments.us/reader030/viewer/2022041000/5e9fd7c1c914ae4272103cd6/html5/thumbnails/7.jpg)
7
• Apps often don’t need explicit synchronization or threading
Pros of Java 8 Parallel Streams
Alleviates many accidental & inherent complexities of concurrency/parallelism
![Page 8: Pros & Cons of Java 8 Parallel Streams - Vanderbilt Universityschmidt/cs891f/2018-PDFs/20-pros... · 2018-10-29 · Pros Cons Cons of Java 8 Parallel Streams •In general, however,](https://reader030.vdocuments.us/reader030/viewer/2022041000/5e9fd7c1c914ae4272103cd6/html5/thumbnails/8.jpg)
8Java class library handles locking needed to protect shared mutable state
• Apps often don’t need explicit synchronization or threading
Pros of Java 8 Parallel Streams
![Page 9: Pros & Cons of Java 8 Parallel Streams - Vanderbilt Universityschmidt/cs891f/2018-PDFs/20-pros... · 2018-10-29 · Pros Cons Cons of Java 8 Parallel Streams •In general, however,](https://reader030.vdocuments.us/reader030/viewer/2022041000/5e9fd7c1c914ae4272103cd6/html5/thumbnails/9.jpg)
9
• Streams ensures that the structure of sequential & parallel code is the same
Pros of Java 8 Parallel Streams
List<List<SearchResults>>
processStream() {
return getInput()
.stream()
.map(this::processInput)
.collect(toList());
}
List<List<SearchResults>>
processStream() {
return getInput()
.parallelStream()
.map(this::processInput)
.collect(toList());
}
Converting sequential to parallel streams only require minuscule changes!
![Page 10: Pros & Cons of Java 8 Parallel Streams - Vanderbilt Universityschmidt/cs891f/2018-PDFs/20-pros... · 2018-10-29 · Pros Cons Cons of Java 8 Parallel Streams •In general, however,](https://reader030.vdocuments.us/reader030/viewer/2022041000/5e9fd7c1c914ae4272103cd6/html5/thumbnails/10.jpg)
10
• Streams ensures that the structure of sequential & parallel code is the same
List<SearchResults> results =
mPhrasesToFind
.parallelStream()
.map(phase ->
searchForPhrase(...,
false))
.filter(not(SearchResults
::isEmpty))
.collect(toList());
List<SearchResults> results =
mPhrasesToFind
.parallelStream()
.map(phase ->
searchForPhrase(...,
true))
.filter(not(SearchResults
::isEmpty))
.collect(toList());
Pros of Java 8 Parallel Streams
Converting sequential to parallel streams only require minuscule changes!
![Page 11: Pros & Cons of Java 8 Parallel Streams - Vanderbilt Universityschmidt/cs891f/2018-PDFs/20-pros... · 2018-10-29 · Pros Cons Cons of Java 8 Parallel Streams •In general, however,](https://reader030.vdocuments.us/reader030/viewer/2022041000/5e9fd7c1c914ae4272103cd6/html5/thumbnails/11.jpg)
11
• Examples show synergies between functional & object-oriented programming
e.g., Prologe.g., ML,Haskell
e.g., C#,Java, C++
e.g., C, FORTRANJava
8
Pros of Java 8 Parallel Streams
![Page 12: Pros & Cons of Java 8 Parallel Streams - Vanderbilt Universityschmidt/cs891f/2018-PDFs/20-pros... · 2018-10-29 · Pros Cons Cons of Java 8 Parallel Streams •In general, however,](https://reader030.vdocuments.us/reader030/viewer/2022041000/5e9fd7c1c914ae4272103cd6/html5/thumbnails/12.jpg)
12
Pros of Java 8 Parallel Streams• Object-oriented design & programming
features simplify understanding, reuse, & extensibility
![Page 13: Pros & Cons of Java 8 Parallel Streams - Vanderbilt Universityschmidt/cs891f/2018-PDFs/20-pros... · 2018-10-29 · Pros Cons Cons of Java 8 Parallel Streams •In general, however,](https://reader030.vdocuments.us/reader030/viewer/2022041000/5e9fd7c1c914ae4272103cd6/html5/thumbnails/13.jpg)
13
• Implementing object-oriented hookmethods with functional programmingfeatures helps to close gap betweendomain intent & computations
Pros of Java 8 Parallel Streams
getInput()
.parallelStream()
.map(this::processInput)
.collect(toList());
return mPhrasesToFind
.parallelStream()
.map(phrase -> searchForPhrase(phrase, input, title, false))
.filter(not(SearchResults::isEmpty)
.collect(toList());
![Page 14: Pros & Cons of Java 8 Parallel Streams - Vanderbilt Universityschmidt/cs891f/2018-PDFs/20-pros... · 2018-10-29 · Pros Cons Cons of Java 8 Parallel Streams •In general, however,](https://reader030.vdocuments.us/reader030/viewer/2022041000/5e9fd7c1c914ae4272103cd6/html5/thumbnails/14.jpg)
14
Cons of Java 8 Parallel Streams
![Page 15: Pros & Cons of Java 8 Parallel Streams - Vanderbilt Universityschmidt/cs891f/2018-PDFs/20-pros... · 2018-10-29 · Pros Cons Cons of Java 8 Parallel Streams •In general, however,](https://reader030.vdocuments.us/reader030/viewer/2022041000/5e9fd7c1c914ae4272103cd6/html5/thumbnails/15.jpg)
15
• There are some limitations with Java 8 parallel streams
Cons of Java 8 Parallel Streams
The Java 8 parallel streams framework is not all unicorns & rainbows!!
![Page 16: Pros & Cons of Java 8 Parallel Streams - Vanderbilt Universityschmidt/cs891f/2018-PDFs/20-pros... · 2018-10-29 · Pros Cons Cons of Java 8 Parallel Streams •In general, however,](https://reader030.vdocuments.us/reader030/viewer/2022041000/5e9fd7c1c914ae4272103cd6/html5/thumbnails/16.jpg)
16
• There are some limitations with Java 8 parallel streams, e.g.
• Some problems can’t be expressedvia the “split-apply-combine” model
See dzone.com/articles/whats-wrong-java-8-part-iii
join join
join
Processsequentially
Processsequentially
Processsequentially
Processsequentially
DataSource1.1 DataSource1.2 DataSource2.1 DataSource2.2
DataSource1 DataSource2
DataSource
trySplit()
trySplit() trySplit()
Cons of Java 8 Parallel Streams
![Page 17: Pros & Cons of Java 8 Parallel Streams - Vanderbilt Universityschmidt/cs891f/2018-PDFs/20-pros... · 2018-10-29 · Pros Cons Cons of Java 8 Parallel Streams •In general, however,](https://reader030.vdocuments.us/reader030/viewer/2022041000/5e9fd7c1c914ae4272103cd6/html5/thumbnails/17.jpg)
17
• There are some limitations with Java 8 parallel streams, e.g.
• Some problems can’t be expressedvia the “split-apply-combine” model
• If behaviors aren’t thread-saferace conditions may occur
See en.wikipedia.org/wiki/Race_condition#Software
Thread1
Thread2
Shared State
Cons of Java 8 Parallel Streams
Race conditions occur when a program depends on the sequence or timing of threads for it to operate properly
![Page 18: Pros & Cons of Java 8 Parallel Streams - Vanderbilt Universityschmidt/cs891f/2018-PDFs/20-pros... · 2018-10-29 · Pros Cons Cons of Java 8 Parallel Streams •In general, however,](https://reader030.vdocuments.us/reader030/viewer/2022041000/5e9fd7c1c914ae4272103cd6/html5/thumbnails/18.jpg)
18
• There are some limitations with Java 8 parallel streams, e.g.
• Some problems can’t be expressedvia the “split-apply-combine” model
• If behaviors aren’t thread-saferace conditions may occur
• Parallel spliterators may be tricky…
Cons of Java 8 Parallel Streams
![Page 19: Pros & Cons of Java 8 Parallel Streams - Vanderbilt Universityschmidt/cs891f/2018-PDFs/20-pros... · 2018-10-29 · Pros Cons Cons of Java 8 Parallel Streams •In general, however,](https://reader030.vdocuments.us/reader030/viewer/2022041000/5e9fd7c1c914ae4272103cd6/html5/thumbnails/19.jpg)
19
Processsequentially
Processsequentially
Processsequentially
Processsequentially
accumulate() accumulate()
accumulate()
ConcurrentResult Container
• There are some limitations with Java 8 parallel streams, e.g.
• Some problems can’t be expressedvia the “split-apply-combine” model
• If behaviors aren’t thread-saferace conditions may occur
• Parallel spliterators may be tricky…
• Concurrent collectors are easier
Cons of Java 8 Parallel Streams
InputSource1.1 InputSource1.2 InputSource2.1 InputSource2.2
InputSource1 InputSource2
trySplit()
InputSource
trySplit() trySplit()
![Page 20: Pros & Cons of Java 8 Parallel Streams - Vanderbilt Universityschmidt/cs891f/2018-PDFs/20-pros... · 2018-10-29 · Pros Cons Cons of Java 8 Parallel Streams •In general, however,](https://reader030.vdocuments.us/reader030/viewer/2022041000/5e9fd7c1c914ae4272103cd6/html5/thumbnails/20.jpg)
20
• There are some limitations with Java 8 parallel streams, e.g.
• Some problems can’t be expressedvia the “split-apply-combine” model
• If behaviors aren’t thread-saferace conditions may occur
• Parallel spliterators may be tricky…
• All parallel streams share a common fork-join pool
See dzone.com/articles/think-twice-using-java-8
Cons of Java 8 Parallel Streams
![Page 21: Pros & Cons of Java 8 Parallel Streams - Vanderbilt Universityschmidt/cs891f/2018-PDFs/20-pros... · 2018-10-29 · Pros Cons Cons of Java 8 Parallel Streams •In general, however,](https://reader030.vdocuments.us/reader030/viewer/2022041000/5e9fd7c1c914ae4272103cd6/html5/thumbnails/21.jpg)
21
• There are some limitations with Java 8 parallel streams, e.g.
• Some problems can’t be expressedvia the “split-apply-combine” model
• If behaviors aren’t thread-saferace conditions may occur
• Parallel spliterators may be tricky…
• All parallel streams share a common fork-join pool
• Java 8 completable futuresdon’t have this limitation
See dzone.com/articles/think-twice-using-java-8
Cons of Java 8 Parallel Streams
![Page 22: Pros & Cons of Java 8 Parallel Streams - Vanderbilt Universityschmidt/cs891f/2018-PDFs/20-pros... · 2018-10-29 · Pros Cons Cons of Java 8 Parallel Streams •In general, however,](https://reader030.vdocuments.us/reader030/viewer/2022041000/5e9fd7c1c914ae4272103cd6/html5/thumbnails/22.jpg)
22See docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinPool.ManagedBlocker.html
• There are some limitations with Java 8 parallel streams, e.g.
• Some problems can’t be expressedvia the “split-apply-combine” model
• If behaviors aren’t thread-saferace conditions may occur
• Parallel spliterators may be tricky…
• All parallel streams share a common fork-join pool
• Java 8 completable futuresdon’t have this limitation
• It’s important to know how toapply ManagedBlockers
Cons of Java 8 Parallel Streams
![Page 23: Pros & Cons of Java 8 Parallel Streams - Vanderbilt Universityschmidt/cs891f/2018-PDFs/20-pros... · 2018-10-29 · Pros Cons Cons of Java 8 Parallel Streams •In general, however,](https://reader030.vdocuments.us/reader030/viewer/2022041000/5e9fd7c1c914ae4272103cd6/html5/thumbnails/23.jpg)
23
• There are some limitations with Java 8 parallel streams, e.g.
• Some problems can’t be expressedvia the “split-apply-combine” model
• If behaviors aren’t thread-saferace conditions may occur
• Parallel spliterators may be tricky…
• All parallel streams share a common fork-join pool
• Some overhead occurs from use ofspliterators & fork-join framework
See coopsoft.com/dl/Blunder.pdf
Cons of Java 8 Parallel Streams
![Page 24: Pros & Cons of Java 8 Parallel Streams - Vanderbilt Universityschmidt/cs891f/2018-PDFs/20-pros... · 2018-10-29 · Pros Cons Cons of Java 8 Parallel Streams •In general, however,](https://reader030.vdocuments.us/reader030/viewer/2022041000/5e9fd7c1c914ae4272103cd6/html5/thumbnails/24.jpg)
24
Printing results for input 1 from fastest to slowestCOMPLETABLE_FUTURES_2 executed in 276 msecsCOMPLETABLE_FUTURES_1 executed in 285 msecsPARALLEL_STREAM executed in 383 msecsSEQUENTIAL_STREAM executed in 1288 msecs
Printing results for input 2 from fastest to slowestCOMPLETABLE_FUTURES_1 executed in 137 msecsCOMPLETABLE_FUTURES_2 executed in 138 msecsPARALLEL_STREAM executed in 170 msecsSEQUENTIAL_STREAM executed in 393 msecs
• There are some limitations with Java 8 parallel streams, e.g.
• Some problems can’t be expressedvia the “split-apply-combine” model
• If behaviors aren’t thread-saferace conditions may occur
• Parallel spliterators may be tricky…
• All parallel streams share a common fork-join pool
• Some overhead occurs from use ofspliterators & fork-join framework
• Java 8 completable futures maybe more efficient & scalable
Cons of Java 8 Parallel Streams
![Page 25: Pros & Cons of Java 8 Parallel Streams - Vanderbilt Universityschmidt/cs891f/2018-PDFs/20-pros... · 2018-10-29 · Pros Cons Cons of Java 8 Parallel Streams •In general, however,](https://reader030.vdocuments.us/reader030/viewer/2022041000/5e9fd7c1c914ae4272103cd6/html5/thumbnails/25.jpg)
25
• There are some limitations with Java 8 parallel streams, e.g.
• Some problems can’t be expressedvia the “split-apply-combine” model
• If behaviors aren’t thread-saferace conditions may occur
• Parallel spliterators may be tricky…
• All parallel streams share a common fork-join pool
• Some overhead occurs from use ofspliterators & fork-join framework
• Java 8 completable futures maybe more efficient & scalable
Cons of Java 8 Parallel Streams
• Naturally, your mileage may vary..
![Page 26: Pros & Cons of Java 8 Parallel Streams - Vanderbilt Universityschmidt/cs891f/2018-PDFs/20-pros... · 2018-10-29 · Pros Cons Cons of Java 8 Parallel Streams •In general, however,](https://reader030.vdocuments.us/reader030/viewer/2022041000/5e9fd7c1c914ae4272103cd6/html5/thumbnails/26.jpg)
26
• There are some limitations with Java 8 parallel streams, e.g.
• Some problems can’t be expressedvia the “split-apply-combine” model
• If behaviors aren’t thread-saferace conditions may occur
• Parallel spliterators may be tricky…
• All parallel streams share a common fork-join pool
• Some overhead occurs from use ofspliterators & fork-join framework
• There’s no substitute for benchmarking!
Cons of Java 8 Parallel Streams
See java-performance.info/jmh
![Page 27: Pros & Cons of Java 8 Parallel Streams - Vanderbilt Universityschmidt/cs891f/2018-PDFs/20-pros... · 2018-10-29 · Pros Cons Cons of Java 8 Parallel Streams •In general, however,](https://reader030.vdocuments.us/reader030/viewer/2022041000/5e9fd7c1c914ae4272103cd6/html5/thumbnails/27.jpg)
27
• In general, there's a tradeoff between computing performance & programmer productivity when choosing amongst these frameworks
• i.e., completable futures are more efficient & scalable, but are harder to program
Cons of Java 8 Parallel Streams
Performance
Productivity
![Page 28: Pros & Cons of Java 8 Parallel Streams - Vanderbilt Universityschmidt/cs891f/2018-PDFs/20-pros... · 2018-10-29 · Pros Cons Cons of Java 8 Parallel Streams •In general, however,](https://reader030.vdocuments.us/reader030/viewer/2022041000/5e9fd7c1c914ae4272103cd6/html5/thumbnails/28.jpg)
28See www.ibm.com/developerworks/library/j-jvmc2
Pros
Cons
Cons of Java 8 Parallel Streams• In general, however, the pros of Java 8 parallel streams far outweigh the
cons in many use cases!!
![Page 29: Pros & Cons of Java 8 Parallel Streams - Vanderbilt Universityschmidt/cs891f/2018-PDFs/20-pros... · 2018-10-29 · Pros Cons Cons of Java 8 Parallel Streams •In general, however,](https://reader030.vdocuments.us/reader030/viewer/2022041000/5e9fd7c1c914ae4272103cd6/html5/thumbnails/29.jpg)
29
• Good coverage of Java 8 parallel streams appears in the book “Java 8 in Action”
See www.manning.com/books/java-8-in-action
Cons of Java 8 Parallel Streams
![Page 30: Pros & Cons of Java 8 Parallel Streams - Vanderbilt Universityschmidt/cs891f/2018-PDFs/20-pros... · 2018-10-29 · Pros Cons Cons of Java 8 Parallel Streams •In general, however,](https://reader030.vdocuments.us/reader030/viewer/2022041000/5e9fd7c1c914ae4272103cd6/html5/thumbnails/30.jpg)
30
End of Pros & Cons of Java 8 Parallel Streams