![Page 1: Java 8 SearchWithParallelStreams Example (Part 1)schmidt/cs891f/2018-PDFs/05-Java-8-sear… · Java 8 SearchWithParallelStreams Example (Part 1) Douglas C. Schmidt ... fork-join pool](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb3b463e4772f44b2636530/html5/thumbnails/1.jpg)
Java 8 SearchWithParallelStreams
Example (Part 1)
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: Java 8 SearchWithParallelStreams Example (Part 1)schmidt/cs891f/2018-PDFs/05-Java-8-sear… · Java 8 SearchWithParallelStreams Example (Part 1) Douglas C. Schmidt ... fork-join pool](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb3b463e4772f44b2636530/html5/thumbnails/2.jpg)
2
map(phrase -> searchForPhrase(…))
filter(not(SearchResults::isEmpty))
collect(toList())
45,000+ phrases
Search Phrases
parallelStream()
• Know how Java 8 parallel streams are applied in SearchWithParallelStreams
See github.com/douglascraigschmidt/LiveLessons/tree/master/SearchStreamGang
Starting SearchStreamGangTest
PARALLEL_SPLITERATOR executed in 409 msecs
COMPLETABLE_FUTURES_INPUTS executed in 426 msecs
COMPLETABLE_FUTURES_PHASES executed in 427 msecs
PARALLEL_STREAMS executed in 437 msecs
PARALLEL_STREAM_PHASES executed in 440 msecs
RXJAVA_PHASES executed in 485 msecs
PARALLEL_STREAM_INPUTS executed in 802 msecs
RXJAVA_INPUTS executed in 866 msecs
SEQUENTIAL_LOOPS executed in 1638 msecs
SEQUENTIAL_STREAM executed in 1958 msecs
Ending SearchStreamGangTest
Learning Objectives in this Part of the LessonInput Strings to Search
…
![Page 3: Java 8 SearchWithParallelStreams Example (Part 1)schmidt/cs891f/2018-PDFs/05-Java-8-sear… · Java 8 SearchWithParallelStreams Example (Part 1) Douglas C. Schmidt ... fork-join pool](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb3b463e4772f44b2636530/html5/thumbnails/3.jpg)
3
Applying Parallel Streams to SearchStreamGang
![Page 4: Java 8 SearchWithParallelStreams Example (Part 1)schmidt/cs891f/2018-PDFs/05-Java-8-sear… · Java 8 SearchWithParallelStreams Example (Part 1) Douglas C. Schmidt ... fork-join pool](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb3b463e4772f44b2636530/html5/thumbnails/4.jpg)
4
• We focus on parallel streams inprocessStream() & processInput() from SearchWithParallelStreams
See github.com/douglascraigschmidt/LiveLessons/tree/master/SearchStreamGang
Applying Parallel Streams to SearchStreamGang
![Page 5: Java 8 SearchWithParallelStreams Example (Part 1)schmidt/cs891f/2018-PDFs/05-Java-8-sear… · Java 8 SearchWithParallelStreams Example (Part 1) Douglas C. Schmidt ... fork-join pool](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb3b463e4772f44b2636530/html5/thumbnails/5.jpg)
5
• We focus on parallel streams inprocessStream() & processInput() from SearchWithParallelStreams
See SearchStreamGang/src/main/java/livelessons/streamgangs/SearchWithParallelStreams.java
getInput()
.parallelStream()
.map(this::processInput)
.collect(toList());
return mPhrasesToFind
.parallelStream()
.map(phrase -> searchForPhrase(phrase, input, title, false))
.filter(not(SearchResults::isEmpty)
.collect(toList());
Applying Parallel Streams to SearchStreamGang
![Page 6: Java 8 SearchWithParallelStreams Example (Part 1)schmidt/cs891f/2018-PDFs/05-Java-8-sear… · Java 8 SearchWithParallelStreams Example (Part 1) Douglas C. Schmidt ... fork-join pool](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb3b463e4772f44b2636530/html5/thumbnails/6.jpg)
6i.e., the map(), filter(), & collect() aggregate operations
• We focus on parallel streams inprocessStream() & processInput() from SearchWithParallelStreams
getInput()
.parallelStream()
.map(this::processInput)
.collect(toList());
return mPhrasesToFind
.parallelStream()
.map(phrase -> searchForPhrase(phrase, input, title, false))
.filter(not(SearchResults::isEmpty)
.collect(toList());
Applying Parallel Streams to SearchStreamGang
![Page 7: Java 8 SearchWithParallelStreams Example (Part 1)schmidt/cs891f/2018-PDFs/05-Java-8-sear… · Java 8 SearchWithParallelStreams Example (Part 1) Douglas C. Schmidt ... fork-join pool](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb3b463e4772f44b2636530/html5/thumbnails/7.jpg)
7
parallelStream()
map(this::processInput)
collect(toList())
Applying Parallel Streams to SearchStreamGang• We focus on parallel streams in
processStream() & processInput() from SearchWithParallelStreams
• processStream()
• Uses a parallel stream to search a listof input strings within the common fork-join pool of worker threads
45,000+ phrases
Search Phrases
Input Strings to Search
…
Each input string contains one work of Shakepeare (e.g., Hamlet, MacBeth, etc.)
![Page 8: Java 8 SearchWithParallelStreams Example (Part 1)schmidt/cs891f/2018-PDFs/05-Java-8-sear… · Java 8 SearchWithParallelStreams Example (Part 1) Douglas C. Schmidt ... fork-join pool](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb3b463e4772f44b2636530/html5/thumbnails/8.jpg)
8
• We focus on parallel streams inprocessStream() & processInput() from SearchWithParallelStreams
• processStream()
• processInput()
• Uses a parallel stream to search each input string & locate all occurrences of phases within the common fork-join pool of worker threads
Applying Parallel Streams to SearchStreamGang
45,000+ phrases
Search Phrases
Input Strings to Search
…
parallelStream()
map(phrase -> searchForPhrase(…))
filter(not(SearchResults::isEmpty))
collect(toList())
![Page 9: Java 8 SearchWithParallelStreams Example (Part 1)schmidt/cs891f/2018-PDFs/05-Java-8-sear… · Java 8 SearchWithParallelStreams Example (Part 1) Douglas C. Schmidt ... fork-join pool](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb3b463e4772f44b2636530/html5/thumbnails/9.jpg)
9
Visualizing processStream() & processInput()
![Page 10: Java 8 SearchWithParallelStreams Example (Part 1)schmidt/cs891f/2018-PDFs/05-Java-8-sear… · Java 8 SearchWithParallelStreams Example (Part 1) Douglas C. Schmidt ... fork-join pool](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb3b463e4772f44b2636530/html5/thumbnails/10.jpg)
10
Input Strings to Search
…Input a list of input strings
• processStream() searches a list of input strings in parallel
Visualizing processStream() & processInput()
List<String> …
parallelStream()
![Page 11: Java 8 SearchWithParallelStreams Example (Part 1)schmidt/cs891f/2018-PDFs/05-Java-8-sear… · Java 8 SearchWithParallelStreams Example (Part 1) Douglas C. Schmidt ... fork-join pool](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb3b463e4772f44b2636530/html5/thumbnails/11.jpg)
11
• processStream() searches a list of input strings in parallel
Visualizing processStream() & processInput()
List<String> …
parallelStream()
Convert collection to a parallel stream, i.e., substreams with chunks of input strings
Input Strings to Search
…
![Page 12: Java 8 SearchWithParallelStreams Example (Part 1)schmidt/cs891f/2018-PDFs/05-Java-8-sear… · Java 8 SearchWithParallelStreams Example (Part 1) Douglas C. Schmidt ... fork-join pool](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb3b463e4772f44b2636530/html5/thumbnails/12.jpg)
12
Output a stream of input strings
• processStream() searches a list of input strings in parallel
Visualizing processStream() & processInput()
List<String> …
parallelStream()
…Stream<String>
Chunks of input strings are processed in parallel on separate threads/cores
Input Strings to Search
…
![Page 13: Java 8 SearchWithParallelStreams Example (Part 1)schmidt/cs891f/2018-PDFs/05-Java-8-sear… · Java 8 SearchWithParallelStreams Example (Part 1) Douglas C. Schmidt ... fork-join pool](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb3b463e4772f44b2636530/html5/thumbnails/13.jpg)
13
• processStream() searches a list of input strings in parallel
Input a stream of input strings
Visualizing processStream() & processInput()
List<String> …
parallelStream()
…Stream<String>
map(this::processInput)
Input Strings to Search
…
![Page 14: Java 8 SearchWithParallelStreams Example (Part 1)schmidt/cs891f/2018-PDFs/05-Java-8-sear… · Java 8 SearchWithParallelStreams Example (Part 1) Douglas C. Schmidt ... fork-join pool](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb3b463e4772f44b2636530/html5/thumbnails/14.jpg)
14
• processStream() searches a list of input strings in parallel
Visualizing processStream() & processInput()
List<String> …
parallelStream()
…Stream<String>
map(this::processInput)
Call processInput() to search for phrases in an input string in parallel
Input Strings to Search
…
![Page 15: Java 8 SearchWithParallelStreams Example (Part 1)schmidt/cs891f/2018-PDFs/05-Java-8-sear… · Java 8 SearchWithParallelStreams Example (Part 1) Douglas C. Schmidt ... fork-join pool](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb3b463e4772f44b2636530/html5/thumbnails/15.jpg)
15
• processStream() searches a list of input strings in parallel
Output a stream of lists of search results
Visualizing processStream() & processInput()
List<String> …
parallelStream()
…Stream<String>
map(this::processInput)Stream<List<SearchResults>> …
Input Strings to Search
…
![Page 16: Java 8 SearchWithParallelStreams Example (Part 1)schmidt/cs891f/2018-PDFs/05-Java-8-sear… · Java 8 SearchWithParallelStreams Example (Part 1) Douglas C. Schmidt ... fork-join pool](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb3b463e4772f44b2636530/html5/thumbnails/16.jpg)
16
• processStream() searches a list of input strings in parallel
Input a stream of lists of search results
Visualizing processStream() & processInput()
List<String> …
parallelStream()
…Stream<String>
map(this::processInput)Stream<List<SearchResults>> …
collect(toList())
Input Strings to Search
…
![Page 17: Java 8 SearchWithParallelStreams Example (Part 1)schmidt/cs891f/2018-PDFs/05-Java-8-sear… · Java 8 SearchWithParallelStreams Example (Part 1) Douglas C. Schmidt ... fork-join pool](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb3b463e4772f44b2636530/html5/thumbnails/17.jpg)
17
• processStream() searches a list of input strings in parallel
Visualizing processStream() & processInput()
List<String> …
parallelStream()
…Stream<String>
map(this::processInput)Stream<List<SearchResults>> …
Input Strings to Search
…
collect(toList())
Trigger intermediate operation processing to run on multiple worker threads & cores
![Page 18: Java 8 SearchWithParallelStreams Example (Part 1)schmidt/cs891f/2018-PDFs/05-Java-8-sear… · Java 8 SearchWithParallelStreams Example (Part 1) Douglas C. Schmidt ... fork-join pool](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb3b463e4772f44b2636530/html5/thumbnails/18.jpg)
18
• processStream() searches a list of input strings in parallel
Visualizing processStream() & processInput()
List<String> …
map(this::processInput)
…
Stream<List<SearchResults>>
Stream<String>
List<SearchResults>
…
parallelStream()
Return a list of lists of search results based on “encounter order”
Input Strings to Search
…
collect(toList())
![Page 19: Java 8 SearchWithParallelStreams Example (Part 1)schmidt/cs891f/2018-PDFs/05-Java-8-sear… · Java 8 SearchWithParallelStreams Example (Part 1) Douglas C. Schmidt ... fork-join pool](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb3b463e4772f44b2636530/html5/thumbnails/19.jpg)
19
Input a list of phrases to find
• processInput() finds phrases in an input string in parallel
Visualizing processStream() & processInput()
List<String> …
45,000+ phrases
Search Phrases
parallelStream()
![Page 20: Java 8 SearchWithParallelStreams Example (Part 1)schmidt/cs891f/2018-PDFs/05-Java-8-sear… · Java 8 SearchWithParallelStreams Example (Part 1) Douglas C. Schmidt ... fork-join pool](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb3b463e4772f44b2636530/html5/thumbnails/20.jpg)
20
• processInput() finds phrases in an input string in parallel
Visualizing processStream() & processInput()
List<String> …
45,000+ phrases
Search Phrases
parallelStream()
Convert collection to a parallel stream, i.e., substreams with chunks of phrases
![Page 21: Java 8 SearchWithParallelStreams Example (Part 1)schmidt/cs891f/2018-PDFs/05-Java-8-sear… · Java 8 SearchWithParallelStreams Example (Part 1) Douglas C. Schmidt ... fork-join pool](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb3b463e4772f44b2636530/html5/thumbnails/21.jpg)
21
Output a stream of phrases to find
• processInput() finds phrases in an input string in parallel
Visualizing processStream() & processInput()
List<String> …
45,000+ phrases
Search Phrases
parallelStream()
…Stream<String>
Different chunks of phrases are processed in parallel on multiple worker threads & cores
![Page 22: Java 8 SearchWithParallelStreams Example (Part 1)schmidt/cs891f/2018-PDFs/05-Java-8-sear… · Java 8 SearchWithParallelStreams Example (Part 1) Douglas C. Schmidt ... fork-join pool](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb3b463e4772f44b2636530/html5/thumbnails/22.jpg)
22
Input a stream of phrases to find
• processInput() finds phrases in an input string in parallel
Visualizing processStream() & processInput()
List<String> …
45,000+ phrases
Search Phrases
parallelStream()
…Stream<String>
map(phrase -> searchForPhrase(…))
![Page 23: Java 8 SearchWithParallelStreams Example (Part 1)schmidt/cs891f/2018-PDFs/05-Java-8-sear… · Java 8 SearchWithParallelStreams Example (Part 1) Douglas C. Schmidt ... fork-join pool](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb3b463e4772f44b2636530/html5/thumbnails/23.jpg)
23
• processInput() finds phrases in an input string in parallel
Visualizing processStream() & processInput()
List<String> …
45,000+ phrases
Search Phrases
parallelStream()
…Stream<String>
map(phrase -> searchForPhrase(…))
Search for phrases in each input string in parallel
![Page 24: Java 8 SearchWithParallelStreams Example (Part 1)schmidt/cs891f/2018-PDFs/05-Java-8-sear… · Java 8 SearchWithParallelStreams Example (Part 1) Douglas C. Schmidt ... fork-join pool](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb3b463e4772f44b2636530/html5/thumbnails/24.jpg)
24
Output a stream of search results
• processInput() finds phrases in an input string in parallel
Visualizing processStream() & processInput()
List<String> …
45,000+ phrases
Search Phrases
parallelStream()
…Stream<String>
map(phrase -> searchForPhrase(…))Stream<SearchResults> …
![Page 25: Java 8 SearchWithParallelStreams Example (Part 1)schmidt/cs891f/2018-PDFs/05-Java-8-sear… · Java 8 SearchWithParallelStreams Example (Part 1) Douglas C. Schmidt ... fork-join pool](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb3b463e4772f44b2636530/html5/thumbnails/25.jpg)
25
Input a stream of search results
• processInput() finds phrases in an input string in parallel
Visualizing processStream() & processInput()
List<String> …
45,000+ phrases
Search Phrases
parallelStream()
…Stream<String>
map(phrase -> searchForPhrase(…))Stream<SearchResults> …
filter(not(SearchResults::isEmpty))
![Page 26: Java 8 SearchWithParallelStreams Example (Part 1)schmidt/cs891f/2018-PDFs/05-Java-8-sear… · Java 8 SearchWithParallelStreams Example (Part 1) Douglas C. Schmidt ... fork-join pool](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb3b463e4772f44b2636530/html5/thumbnails/26.jpg)
26
• processInput() finds phrases in an input string in parallel
Visualizing processStream() & processInput()
List<String> …
45,000+ phrases
Search Phrases
parallelStream()
…Stream<String>
map(phrase -> searchForPhrase(…))Stream<SearchResults> …
filter(not(SearchResults::isEmpty))
Remove empty search results from substreams in parallel
![Page 27: Java 8 SearchWithParallelStreams Example (Part 1)schmidt/cs891f/2018-PDFs/05-Java-8-sear… · Java 8 SearchWithParallelStreams Example (Part 1) Douglas C. Schmidt ... fork-join pool](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb3b463e4772f44b2636530/html5/thumbnails/27.jpg)
27
• processInput() finds phrases in an input string in parallel
Output a stream of non-empty search results
Stream<SearchResults>
Visualizing processStream() & processInput()
List<String> …
45,000+ phrases
Search Phrases
parallelStream()
…Stream<String>
map(phrase -> searchForPhrase(…))Stream<SearchResults> …
filter(not(SearchResults::isEmpty))
![Page 28: Java 8 SearchWithParallelStreams Example (Part 1)schmidt/cs891f/2018-PDFs/05-Java-8-sear… · Java 8 SearchWithParallelStreams Example (Part 1) Douglas C. Schmidt ... fork-join pool](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb3b463e4772f44b2636530/html5/thumbnails/28.jpg)
28
Input a stream of non-empty search results
• processInput() finds phrases in an input string in parallel
Visualizing processStream() & processInput()
List<String> …
map(phrase -> searchForPhrase(…))
filter(not(SearchResults::isEmpty))
collect(toList())
45,000+ phrases
Search Phrases
…
Stream<SearchResults>
Stream<String>
Stream<SearchResults>
…
parallelStream()
![Page 29: Java 8 SearchWithParallelStreams Example (Part 1)schmidt/cs891f/2018-PDFs/05-Java-8-sear… · Java 8 SearchWithParallelStreams Example (Part 1) Douglas C. Schmidt ... fork-join pool](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb3b463e4772f44b2636530/html5/thumbnails/29.jpg)
29
• processInput() finds phrases in an input string in parallel
Visualizing processStream() & processInput()
List<String> …
map(phrase -> searchForPhrase(…))
filter(not(SearchResults::isEmpty))
45,000+ phrases
Search Phrases
…
Stream<SearchResults>
Stream<String>
Stream<SearchResults>
…
parallelStream()
Trigger intermediate operation processing to run on multiple threads/cores
collect(toList())
![Page 30: Java 8 SearchWithParallelStreams Example (Part 1)schmidt/cs891f/2018-PDFs/05-Java-8-sear… · Java 8 SearchWithParallelStreams Example (Part 1) Douglas C. Schmidt ... fork-join pool](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb3b463e4772f44b2636530/html5/thumbnails/30.jpg)
30
List<SearchResults>
• processInput() finds phrases in an input string in parallel
Visualizing processStream() & processInput()
List<String> …
map(phrase -> searchForPhrase(…))
filter(not(SearchResults::isEmpty))
45,000+ phrases
Search Phrases
…
Stream<SearchResults>
Stream<String>
Stream<SearchResults>
…
parallelStream()
Return a list of search results in the originating thread based on “encounter order”
collect(toList())
![Page 31: Java 8 SearchWithParallelStreams Example (Part 1)schmidt/cs891f/2018-PDFs/05-Java-8-sear… · Java 8 SearchWithParallelStreams Example (Part 1) Douglas C. Schmidt ... fork-join pool](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb3b463e4772f44b2636530/html5/thumbnails/31.jpg)
31
List<SearchResults>
• Note that the actual processing of parallelstreams differs from this visualization..
Visualizing processStream() & processInput()
List<String> …
map(phrase -> searchForPhrase(…))
filter(not(SearchResults::isEmpty))
45,000+ phrases
Search Phrases
…
Stream<SearchResults>
Stream<String>
Stream<SearchResults>
…
parallelStream()
See www.ibm.com/developerworks/library/j-java-streams-3-brian-goetz
collect(toList())
![Page 32: Java 8 SearchWithParallelStreams Example (Part 1)schmidt/cs891f/2018-PDFs/05-Java-8-sear… · Java 8 SearchWithParallelStreams Example (Part 1) Douglas C. Schmidt ... fork-join pool](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb3b463e4772f44b2636530/html5/thumbnails/32.jpg)
32
Implementing processStream() as a Parallel Stream
![Page 33: Java 8 SearchWithParallelStreams Example (Part 1)schmidt/cs891f/2018-PDFs/05-Java-8-sear… · Java 8 SearchWithParallelStreams Example (Part 1) Douglas C. Schmidt ... fork-join pool](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb3b463e4772f44b2636530/html5/thumbnails/33.jpg)
33
• Parallel processStream() has one minuscule change wrt the sequential version
protected List<List<SearchResults>> processStream() {
List<CharSequence> inputList =
getInput();
return getInput()
.parallelStream()
.map(this::processInput)
.collect(toList());
}
Implementing processStream() as a Parallel Stream
![Page 34: Java 8 SearchWithParallelStreams Example (Part 1)schmidt/cs891f/2018-PDFs/05-Java-8-sear… · Java 8 SearchWithParallelStreams Example (Part 1) Douglas C. Schmidt ... fork-join pool](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb3b463e4772f44b2636530/html5/thumbnails/34.jpg)
34
• Parallel processStream() has one minuscule change wrt the sequential version
protected List<List<SearchResults>> processStream() {
List<CharSequence> inputList =
getInput();
return getInput()
.parallelStream()
.map(this::processInput)
.collect(toList());
}
Implementing processStream() as a Parallel Stream
Uses an ArrayList spliterator to create a parallel stream that searches an arraylist of input strings in multiple worker threads
See docs.oracle.com/javase/8/docs/api/java/util/Spliterator.html
![Page 35: Java 8 SearchWithParallelStreams Example (Part 1)schmidt/cs891f/2018-PDFs/05-Java-8-sear… · Java 8 SearchWithParallelStreams Example (Part 1) Douglas C. Schmidt ... fork-join pool](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb3b463e4772f44b2636530/html5/thumbnails/35.jpg)
35
• Parallel processStream() has one minuscule change wrt the sequential version
protected List<List<SearchResults>> processStream() {
List<CharSequence> inputList =
getInput();
return getInput()
.parallelStream()
.map(this::processInput)
.collect(toList());
}
Implementing processStream() as a Parallel Stream
“Chunks” of input strings are processed in parallel in the common fork-join pool
Processsequentially
Processsequentially
Processsequentially
Processsequentially
Input Strings1.1 Input Strings1.2 Input Strings2.1 Input Strings2.2
Input Strings1 Input Strings2
Input Strings
![Page 36: Java 8 SearchWithParallelStreams Example (Part 1)schmidt/cs891f/2018-PDFs/05-Java-8-sear… · Java 8 SearchWithParallelStreams Example (Part 1) Douglas C. Schmidt ... fork-join pool](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb3b463e4772f44b2636530/html5/thumbnails/36.jpg)
36
• Parallel processStream() has one minuscule change wrt the sequential version
protected List<List<SearchResults>> processStream() {
List<CharSequence> inputList =
getInput();
return getInput()
.parallelStream()
.map(this::processInput)
.collect(toList());
}
Implementing processStream() as a Parallel Stream
Searches a given input string to locate all occurrences of phases
![Page 37: Java 8 SearchWithParallelStreams Example (Part 1)schmidt/cs891f/2018-PDFs/05-Java-8-sear… · Java 8 SearchWithParallelStreams Example (Part 1) Douglas C. Schmidt ... fork-join pool](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb3b463e4772f44b2636530/html5/thumbnails/37.jpg)
37
• Parallel processStream() has one minuscule change wrt the sequential version
protected List<List<SearchResults>> processStream() {
List<CharSequence> inputList =
getInput();
return getInput()
.parallelStream()
.map(this::processInput)
.collect(toList());
}
Implementing processStream() as a Parallel Stream
Collectors.toList() returns a non-concurrent collector that obeys encounter order
Trigger intermediate operation processing & merge partial results into a single list of lists
![Page 38: Java 8 SearchWithParallelStreams Example (Part 1)schmidt/cs891f/2018-PDFs/05-Java-8-sear… · Java 8 SearchWithParallelStreams Example (Part 1) Douglas C. Schmidt ... fork-join pool](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb3b463e4772f44b2636530/html5/thumbnails/38.jpg)
38
Implementing processInput() as a Parallel Stream
![Page 39: Java 8 SearchWithParallelStreams Example (Part 1)schmidt/cs891f/2018-PDFs/05-Java-8-sear… · Java 8 SearchWithParallelStreams Example (Part 1) Douglas C. Schmidt ... fork-join pool](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb3b463e4772f44b2636530/html5/thumbnails/39.jpg)
39
• Likewise, this processInput() implementation has just one minuscule change
List<SearchResults> processInput(CharSequence inputSeq) {
String title = getTitle(inputString);
CharSequence input = inputSeq.subSequence(...);
List<SearchResults> results = mPhrasesToFind
.parallelStream()
.map(phase ->
searchForPhrase(phase, input, title, false))
.filter(not(SearchResults::isEmpty))
.collect(toList());
return results;
}
Implementing processInput() as a Parallel Stream
![Page 40: Java 8 SearchWithParallelStreams Example (Part 1)schmidt/cs891f/2018-PDFs/05-Java-8-sear… · Java 8 SearchWithParallelStreams Example (Part 1) Douglas C. Schmidt ... fork-join pool](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb3b463e4772f44b2636530/html5/thumbnails/40.jpg)
40
Create a parallel stream that searches the given input string to locate all occurrences of phases
• Likewise, this processInput() implementation has just one minuscule change
List<SearchResults> processInput(CharSequence inputSeq) {
String title = getTitle(inputString);
CharSequence input = inputSeq.subSequence(...);
List<SearchResults> results = mPhrasesToFind
.parallelStream()
.map(phase ->
searchForPhrase(phase, input, title, false))
.filter(not(SearchResults::isEmpty))
.collect(toList());
return results;
}
Implementing processInput() as a Parallel Stream
![Page 41: Java 8 SearchWithParallelStreams Example (Part 1)schmidt/cs891f/2018-PDFs/05-Java-8-sear… · Java 8 SearchWithParallelStreams Example (Part 1) Douglas C. Schmidt ... fork-join pool](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb3b463e4772f44b2636530/html5/thumbnails/41.jpg)
41
• Likewise, this processInput() implementation has just one minuscule change
List<SearchResults> processInput(CharSequence inputSeq) {
String title = getTitle(inputString);
CharSequence input = inputSeq.subSequence(...);
List<SearchResults> results = mPhrasesToFind
.parallelStream()
.map(phase ->
searchForPhrase(phase, input, title, false))
.filter(not(SearchResults::isEmpty))
.collect(toList());
return results;
}
Implementing processInput() as a Parallel Stream
See docs.oracle.com/javase/8/docs/api/java/util/Spliterator.html
The PhraseMatchSpliterator breaks the input into “chunks” that are processed sequentially
![Page 42: Java 8 SearchWithParallelStreams Example (Part 1)schmidt/cs891f/2018-PDFs/05-Java-8-sear… · Java 8 SearchWithParallelStreams Example (Part 1) Douglas C. Schmidt ... fork-join pool](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb3b463e4772f44b2636530/html5/thumbnails/42.jpg)
42
• Likewise, this processInput() implementation has just one minuscule change
List<SearchResults> processInput(CharSequence inputSeq) {
String title = getTitle(inputString);
CharSequence input = inputSeq.
List<SearchResults> results =
.parallelStream()
.map(phase ->
searchForPhrase(phase, (phase, input, title, false))
.filter(not(SearchResults::::
.collect(toList());
return results;
}
Implementing processInput() as a Parallel Stream
“Chunks” of phrases are processed in parallel in the common fork-join pool
Processsequentially
Processsequentially
Processsequentially
Processsequentially
Phrases1.1 Phrases1.2 Phrases2.1 Phrases2.2
Phrases1 Phrases2
Phrases
![Page 43: Java 8 SearchWithParallelStreams Example (Part 1)schmidt/cs891f/2018-PDFs/05-Java-8-sear… · Java 8 SearchWithParallelStreams Example (Part 1) Douglas C. Schmidt ... fork-join pool](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb3b463e4772f44b2636530/html5/thumbnails/43.jpg)
43
• Likewise, this processInput() implementation has just one minuscule change
List<SearchResults> processInput(CharSequence inputSeq) {
String title = getTitle(inputString);
CharSequence input = inputSeq.
List<SearchResults> results =
.parallelStream()
.map(phase ->
searchForPhrase(phase, input, title, false))
.filter(not(SearchResults::isEmpty))
.collect(toList());
return results;
}
Implementing processInput() as a Parallel Stream
Trigger intermediate operation processing & merge partial results into a single list
![Page 44: Java 8 SearchWithParallelStreams Example (Part 1)schmidt/cs891f/2018-PDFs/05-Java-8-sear… · Java 8 SearchWithParallelStreams Example (Part 1) Douglas C. Schmidt ... fork-join pool](https://reader036.vdocument.in/reader036/viewer/2022062604/5fb3b463e4772f44b2636530/html5/thumbnails/44.jpg)
44
End of Java 8SearchWithParallelStreams
Example (Part 1)