![Page 1: Applying Java Functional Programming Features: Introduction · •Understand how Java functional programming features are applied in a simple parallel program •This program searches](https://reader036.vdocuments.net/reader036/viewer/2022062415/5fc56e0a281576426931ec4c/html5/thumbnails/1.jpg)
Applying Java Functional
Programming Features: Introduction
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: Applying Java Functional Programming Features: Introduction · •Understand how Java functional programming features are applied in a simple parallel program •This program searches](https://reader036.vdocuments.net/reader036/viewer/2022062415/5fc56e0a281576426931ec4c/html5/thumbnails/2.jpg)
2
Learning Objectives in this Part of the Lesson• Understand how Java functional programming
features are applied in a simple parallel program
See github.com/douglascraigschmidt/LiveLessons/tree/master/ThreadJoinTest/updated
Starting SearchStreamin thread 23 the phrase "Anon," was found at character offset 111628 in "The First Part of Henry VI"in thread 20 the phrase "Anon," was found at character offset 30949 in "The First Part of King Henry IV"in thread 20 the phrase "Anon," was found at character offset 48850 in "The First Part of King Henry IV"in thread 19 the phrase "Anon," was found at character offset 170485 in "The Tragedy of Hamlet"in thread 20 the phrase "Anon," was found at character offset 49402 in "The First Part of King Henry IV"in thread 20 the phrase "Anon," was found at character offset 49640 in "The First Part of King Henry IV"in thread 20 the phrase "Anon," was found at character offset 50003 in "The First Part of King Henry IV"in thread 20 the phrase "Anon," was found at character offset 50140 in "The First Part of King Henry IV"in thread 20 the phrase "Anon," was found at character offset 50464 in "The First Part of King Henry IV"in thread 20 the phrase "Anon," was found at character offset 50486 in "The First Part of King Henry IV"in thread 20 the phrase "Anon," was found at character offset 51628 in "The First Part of King Henry IV"in thread 20 the phrase "Anon," was found at character offset 52190 in "The First Part of King Henry IV"in thread 21 the phrase "Anon," was found at character offset 67832 in "Second Part of King Henry IV"in thread 16 the phrase "Anon," was found at character offset 75139 in "The Comedy of Errors"in thread 16 the phrase "Anon," was found at character offset 76511 in "The Comedy of Errors"in thread 31 the phrase "Anon," was found at character offset 34971 in "The Tragedy of Macbeth"in thread 40 the phrase "Anon," was found at character offset 37045 in "The Tragedy of Romeo & Juliet"in thread 40 the phrase "Anon," was found at character offset 46837 in "The Tragedy of Romeo & Juliet"Ending SearchStream
![Page 3: Applying Java Functional Programming Features: Introduction · •Understand how Java functional programming features are applied in a simple parallel program •This program searches](https://reader036.vdocuments.net/reader036/viewer/2022062415/5fc56e0a281576426931ec4c/html5/thumbnails/3.jpg)
3
Learning Objectives in this Part of the Lesson• Understand how Java functional programming
features are applied in a simple parallel program
• This program searches for a list of phrases in the complete works of William Shakespeare
Starting SearchStreamin thread 23 the phrase "Anon," was found at character offset 111628 in "The First Part of Henry VI"in thread 20 the phrase "Anon," was found at character offset 30949 in "The First Part of King Henry IV"in thread 20 the phrase "Anon," was found at character offset 48850 in "The First Part of King Henry IV"in thread 19 the phrase "Anon," was found at character offset 170485 in "The Tragedy of Hamlet"in thread 20 the phrase "Anon," was found at character offset 49402 in "The First Part of King Henry IV"in thread 20 the phrase "Anon," was found at character offset 49640 in "The First Part of King Henry IV"in thread 20 the phrase "Anon," was found at character offset 50003 in "The First Part of King Henry IV"in thread 20 the phrase "Anon," was found at character offset 50140 in "The First Part of King Henry IV"in thread 20 the phrase "Anon," was found at character offset 50464 in "The First Part of King Henry IV"in thread 20 the phrase "Anon," was found at character offset 50486 in "The First Part of King Henry IV"in thread 20 the phrase "Anon," was found at character offset 51628 in "The First Part of King Henry IV"in thread 20 the phrase "Anon," was found at character offset 52190 in "The First Part of King Henry IV"in thread 21 the phrase "Anon," was found at character offset 67832 in "Second Part of King Henry IV"in thread 16 the phrase "Anon," was found at character offset 75139 in "The Comedy of Errors"in thread 16 the phrase "Anon," was found at character offset 76511 in "The Comedy of Errors"in thread 31 the phrase "Anon," was found at character offset 34971 in "The Tragedy of Macbeth"in thread 40 the phrase "Anon," was found at character offset 37045 in "The Tragedy of Romeo & Juliet"in thread 40 the phrase "Anon," was found at character offset 46837 in "The Tragedy of Romeo & Juliet"Ending SearchStream
See shakespeare.mit.edu
![Page 4: Applying Java Functional Programming Features: Introduction · •Understand how Java functional programming features are applied in a simple parallel program •This program searches](https://reader036.vdocuments.net/reader036/viewer/2022062415/5fc56e0a281576426931ec4c/html5/thumbnails/4.jpg)
4
Overview of the ThreadJoinTest Program
![Page 5: Applying Java Functional Programming Features: Introduction · •Understand how Java functional programming features are applied in a simple parallel program •This program searches](https://reader036.vdocuments.net/reader036/viewer/2022062415/5fc56e0a281576426931ec4c/html5/thumbnails/5.jpg)
5
Overview of the ThreadJoinTest Program
See github.com/douglascraigschmidt/LiveLessons/tree/master/ThreadJoinTest/updated
• Use Java functional programming features to start() & join() threads to search for phrases in works of William Shakespeare
workerThreads
.forEach(Thread::start);
workerThreads
.forEach(thread ->
{ try { thread.join(); }
catch (Exception e)
{ throw new RuntimeException(e);
}});
Starting SearchStreamin thread 23 the phrase "Anon," was found at character offset 111628 in "The First Part of Henry VI"in thread 20 the phrase "Anon," was found at character offset 30949 in "The First Part of King Henry IV"in thread 20 the phrase "Anon," was found at character offset 48850 in "The First Part of King Henry IV"in thread 19 the phrase "Anon," was found at character offset 170485 in "The Tragedy of Hamlet"in thread 20 the phrase "Anon," was found at character offset 49402 in "The First Part of King Henry IV"in thread 20 the phrase "Anon," was found at character offset 49640 in "The First Part of King Henry IV"in thread 20 the phrase "Anon," was found at character offset 50003 in "The First Part of King Henry IV"in thread 20 the phrase "Anon," was found at character offset 50140 in "The First Part of King Henry IV"in thread 20 the phrase "Anon," was found at character offset 50464 in "The First Part of King Henry IV"in thread 20 the phrase "Anon," was found at character offset 50486 in "The First Part of King Henry IV"in thread 20 the phrase "Anon," was found at character offset 51628 in "The First Part of King Henry IV"in thread 20 the phrase "Anon," was found at character offset 52190 in "The First Part of King Henry IV"in thread 21 the phrase "Anon," was found at character offset 67832 in "Second Part of King Henry IV"in thread 16 the phrase "Anon," was found at character offset 75139 in "The Comedy of Errors"in thread 16 the phrase "Anon," was found at character offset 76511 in "The Comedy of Errors"in thread 31 the phrase "Anon," was found at character offset 34971 in "The Tragedy of Macbeth"in thread 40 the phrase "Anon," was found at character offset 37045 in "The Tragedy of Romeo & Juliet"in thread 40 the phrase "Anon," was found at character offset 46837 in "The Tragedy of Romeo & Juliet"Ending SearchStream
![Page 6: Applying Java Functional Programming Features: Introduction · •Understand how Java functional programming features are applied in a simple parallel program •This program searches](https://reader036.vdocuments.net/reader036/viewer/2022062415/5fc56e0a281576426931ec4c/html5/thumbnails/6.jpg)
6
• This program is "embarrassingly parallel"
See en.wikipedia.org/wiki/Embarrassingly_parallel
Starting SearchStreamin thread 23 the phrase "Anon," was found at character offset 111628 in "The First Part of Henry VI"in thread 20 the phrase "Anon," was found at character offset 30949 in "The First Part of King Henry IV"in thread 20 the phrase "Anon," was found at character offset 48850 in "The First Part of King Henry IV"in thread 19 the phrase "Anon," was found at character offset 170485 in "The Tragedy of Hamlet"in thread 20 the phrase "Anon," was found at character offset 49402 in "The First Part of King Henry IV"in thread 20 the phrase "Anon," was found at character offset 49640 in "The First Part of King Henry IV"in thread 20 the phrase "Anon," was found at character offset 50003 in "The First Part of King Henry IV"in thread 20 the phrase "Anon," was found at character offset 50140 in "The First Part of King Henry IV"in thread 20 the phrase "Anon," was found at character offset 50464 in "The First Part of King Henry IV"in thread 20 the phrase "Anon," was found at character offset 50486 in "The First Part of King Henry IV"in thread 20 the phrase "Anon," was found at character offset 51628 in "The First Part of King Henry IV"in thread 20 the phrase "Anon," was found at character offset 52190 in "The First Part of King Henry IV"in thread 21 the phrase "Anon," was found at character offset 67832 in "Second Part of King Henry IV"in thread 16 the phrase "Anon," was found at character offset 75139 in "The Comedy of Errors"in thread 16 the phrase "Anon," was found at character offset 76511 in "The Comedy of Errors"in thread 31 the phrase "Anon," was found at character offset 34971 in "The Tragedy of Macbeth"in thread 40 the phrase "Anon," was found at character offset 37045 in "The Tragedy of Romeo & Juliet"in thread 40 the phrase "Anon," was found at character offset 46837 in "The Tragedy of Romeo & Juliet"Ending SearchStream
Overview of the ThreadJoinTest Program
![Page 7: Applying Java Functional Programming Features: Introduction · •Understand how Java functional programming features are applied in a simple parallel program •This program searches](https://reader036.vdocuments.net/reader036/viewer/2022062415/5fc56e0a281576426931ec4c/html5/thumbnails/7.jpg)
7
• This program is "embarrassingly parallel"
• i.e., there are no data dependencies between worker threads
See en.wikipedia.org/wiki/Embarrassingly_parallel
Starting SearchStreamin thread 23 the phrase "Anon," was found at character offset 111628 in "The First Part of Henry VI"in thread 20 the phrase "Anon," was found at character offset 30949 in "The First Part of King Henry IV"in thread 20 the phrase "Anon," was found at character offset 48850 in "The First Part of King Henry IV"in thread 19 the phrase "Anon," was found at character offset 170485 in "The Tragedy of Hamlet"in thread 20 the phrase "Anon," was found at character offset 49402 in "The First Part of King Henry IV"in thread 20 the phrase "Anon," was found at character offset 49640 in "The First Part of King Henry IV"in thread 20 the phrase "Anon," was found at character offset 50003 in "The First Part of King Henry IV"in thread 20 the phrase "Anon," was found at character offset 50140 in "The First Part of King Henry IV"in thread 20 the phrase "Anon," was found at character offset 50464 in "The First Part of King Henry IV"in thread 20 the phrase "Anon," was found at character offset 50486 in "The First Part of King Henry IV"in thread 20 the phrase "Anon," was found at character offset 51628 in "The First Part of King Henry IV"in thread 20 the phrase "Anon," was found at character offset 52190 in "The First Part of King Henry IV"in thread 21 the phrase "Anon," was found at character offset 67832 in "Second Part of King Henry IV"in thread 16 the phrase "Anon," was found at character offset 75139 in "The Comedy of Errors"in thread 16 the phrase "Anon," was found at character offset 76511 in "The Comedy of Errors"in thread 31 the phrase "Anon," was found at character offset 34971 in "The Tragedy of Macbeth"in thread 40 the phrase "Anon," was found at character offset 37045 in "The Tragedy of Romeo & Juliet"in thread 40 the phrase "Anon," was found at character offset 46837 in "The Tragedy of Romeo & Juliet"Ending SearchStream
Overview of the ThreadJoinTest Program
![Page 8: Applying Java Functional Programming Features: Introduction · •Understand how Java functional programming features are applied in a simple parallel program •This program searches](https://reader036.vdocuments.net/reader036/viewer/2022062415/5fc56e0a281576426931ec4c/html5/thumbnails/8.jpg)
9
List<String> mInputList =
TestDataFactory.getInput
(sSHAKESPEARE_DATA_FILE,
"@");
...
@The Tragedy of Hamlet
...
@The Tragedy of Julius Caesar
...
@The Tragedy of Macbeth
...
Each work begins with a ’@’ character
• The program obtains the complete worksof Shakespeare & a list of phrases fromtwo text files
Overview of the ThreadJoinTest Program
![Page 9: Applying Java Functional Programming Features: Introduction · •Understand how Java functional programming features are applied in a simple parallel program •This program searches](https://reader036.vdocuments.net/reader036/viewer/2022062415/5fc56e0a281576426931ec4c/html5/thumbnails/9.jpg)
10
List<String> mPhrasesToFind =
TestDataFactory.getPhraseList
(sPHRASE_LIST_FILE);
...
Neither a borrower nor a lender be
Beware the Ides of March
Brevity is the soul of wit
All that glisters is not gold
Sit you down, father; rest you
my kingdom for a horse!
...
Each phrase appears on a separate line
• The program obtains the complete worksof Shakespeare & a list of phrases fromtwo text files
Overview of the ThreadJoinTest Program
![Page 10: Applying Java Functional Programming Features: Introduction · •Understand how Java functional programming features are applied in a simple parallel program •This program searches](https://reader036.vdocuments.net/reader036/viewer/2022062415/5fc56e0a281576426931ec4c/html5/thumbnails/10.jpg)
11
• Return the input data in the given file as a list of non-empty strings
static List<String> getInput(String file, String splitter) {
URI uri = ClassLoader.getSystemResource(file).toURI();
String bytes = new String(Files.readAllBytes
(Paths.get(uri)));
return Pattern
.compile(splitter)
.splitAsStream(bytes)
.filter(((Predicate<String>) String::isEmpty).negate())
.collect(toList());
}
Overview of the ThreadJoinTest Program
These methods also demonstrate some Java 8 streams features!
![Page 11: Applying Java Functional Programming Features: Introduction · •Understand how Java functional programming features are applied in a simple parallel program •This program searches](https://reader036.vdocuments.net/reader036/viewer/2022062415/5fc56e0a281576426931ec4c/html5/thumbnails/11.jpg)
12
• Return the input data in the given file as a list of non-empty strings
static List<String> getInput(String file, String splitter) {
URI uri = ClassLoader.getSystemResource(file).toURI();
String bytes = new String(Files.readAllBytes
(Paths.get(uri)));
return Pattern
.compile(splitter)
.splitAsStream(bytes)
.filter(((Predicate<String>) String::isEmpty).negate())
.collect(toList());
}
Convert the file name into a path name
Overview of the ThreadJoinTest Program
![Page 12: Applying Java Functional Programming Features: Introduction · •Understand how Java functional programming features are applied in a simple parallel program •This program searches](https://reader036.vdocuments.net/reader036/viewer/2022062415/5fc56e0a281576426931ec4c/html5/thumbnails/12.jpg)
13
• Return the input data in the given file as a list of non-empty strings
static List<String> getInput(String file, String splitter) {
URI uri = ClassLoader.getSystemResource(file).toURI();
String bytes = new String(Files.readAllBytes
(Paths.get(uri)));
return Pattern
.compile(splitter)
.splitAsStream(bytes)
.filter(((Predicate<String>) String::isEmpty).negate())
.collect(toList());
}
Open the file & read all the bytes
Overview of the ThreadJoinTest Program
![Page 13: Applying Java Functional Programming Features: Introduction · •Understand how Java functional programming features are applied in a simple parallel program •This program searches](https://reader036.vdocuments.net/reader036/viewer/2022062415/5fc56e0a281576426931ec4c/html5/thumbnails/13.jpg)
14
• Return the input data in the given file as a list of non-empty strings
static List<String> getInput(String file, String splitter) {
URI uri = ClassLoader.getSystemResource(file).toURI();
String bytes = new String(Files.readAllBytes
(Paths.get(uri)));
return Pattern
.compile(splitter)
.splitAsStream(bytes)
.filter(((Predicate<String>) String::isEmpty).negate())
.collect(toList());
}
Compile a regular expression used to split the file into a stream of strings
Overview of the ThreadJoinTest Program
See docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html
![Page 14: Applying Java Functional Programming Features: Introduction · •Understand how Java functional programming features are applied in a simple parallel program •This program searches](https://reader036.vdocuments.net/reader036/viewer/2022062415/5fc56e0a281576426931ec4c/html5/thumbnails/14.jpg)
15
• Return the input data in the given file as a list of non-empty strings
static List<String> getInput(String file, String splitter) {
URI uri = ClassLoader.getSystemResource(file).toURI();
String bytes = new String(Files.readAllBytes
(Paths.get(uri)));
return Pattern
.compile(splitter)
.splitAsStream(bytes)
.filter(((Predicate<String>) String::isEmpty).negate())
.collect(toList());
}
Filter out any empty strings in the stream
Overview of the ThreadJoinTest Program
![Page 15: Applying Java Functional Programming Features: Introduction · •Understand how Java functional programming features are applied in a simple parallel program •This program searches](https://reader036.vdocuments.net/reader036/viewer/2022062415/5fc56e0a281576426931ec4c/html5/thumbnails/15.jpg)
16
• Return the input data in the given file as a list of non-empty strings
static List<String> getInput(String file, String splitter) {
URI uri = ClassLoader.getSystemResource(file).toURI();
String bytes = new String(Files.readAllBytes
(Paths.get(uri)));
return Pattern
.compile(splitter)
.splitAsStream(bytes)
.filter(((Predicate<String>) String::isEmpty).negate())
.collect(toList());
}Collect the results into a list of strings
Overview of the ThreadJoinTest Program
![Page 16: Applying Java Functional Programming Features: Introduction · •Understand how Java functional programming features are applied in a simple parallel program •This program searches](https://reader036.vdocuments.net/reader036/viewer/2022062415/5fc56e0a281576426931ec4c/html5/thumbnails/16.jpg)
17
• Return the phrase list in the file as a list of non-empty strings
static List<String> getPhraseList(String file) {
return Files
.lines(Paths
.get(ClassLoader.getSystemResource(file).toURI()))
.filter(((Predicate<String>) String::isEmpty).negate())
.collect(toList());
}
Overview of the ThreadJoinTest Program
![Page 17: Applying Java Functional Programming Features: Introduction · •Understand how Java functional programming features are applied in a simple parallel program •This program searches](https://reader036.vdocuments.net/reader036/viewer/2022062415/5fc56e0a281576426931ec4c/html5/thumbnails/17.jpg)
18
• Return the phrase list in the file as a list of non-empty strings
static List<String> getPhraseList(String file) {
return Files
.lines(Paths
.get(ClassLoader.getSystemResource(file).toURI()))
.filter(((Predicate<String>) String::isEmpty).negate())
.collect(toList());
}
Read all lines from file into a stream
Overview of the ThreadJoinTest Program
![Page 18: Applying Java Functional Programming Features: Introduction · •Understand how Java functional programming features are applied in a simple parallel program •This program searches](https://reader036.vdocuments.net/reader036/viewer/2022062415/5fc56e0a281576426931ec4c/html5/thumbnails/18.jpg)
19
• Return the phrase list in the file as a list of non-empty strings
static List<String> getPhraseList(String file) {
return Files
.lines(Paths
.get(ClassLoader.getSystemResource(file).toURI()))
.filter(((Predicate<String>) String::isEmpty).negate())
.collect(toList());
}
Filter out any empty strings in the stream
Overview of the ThreadJoinTest Program
![Page 19: Applying Java Functional Programming Features: Introduction · •Understand how Java functional programming features are applied in a simple parallel program •This program searches](https://reader036.vdocuments.net/reader036/viewer/2022062415/5fc56e0a281576426931ec4c/html5/thumbnails/19.jpg)
20
• Return the phrase list in the file as a list of non-empty strings
static List<String> getPhraseList(String file) {
return Files
.lines(Paths
.get(ClassLoader.getSystemResource(file).toURI()))
.filter(((Predicate<String>) String::isEmpty).negate())
.collect(toList());
}
Collect the results into a list of strings
Overview of the ThreadJoinTest Program
![Page 20: Applying Java Functional Programming Features: Introduction · •Understand how Java functional programming features are applied in a simple parallel program •This program searches](https://reader036.vdocuments.net/reader036/viewer/2022062415/5fc56e0a281576426931ec4c/html5/thumbnails/20.jpg)
21
• The main program creates & runs an instance of SearchOneShotThreadJoin
public void main(String[] args) {
new SearchOneShotThreadJoin()
.run();
}
Create/run an object to search for all phrases in parallel
Overview of the ThreadJoinTest Program
See ThreadJoinTest/updated/src/main/java/ThreadJoinTest.java
![Page 21: Applying Java Functional Programming Features: Introduction · •Understand how Java functional programming features are applied in a simple parallel program •This program searches](https://reader036.vdocuments.net/reader036/viewer/2022062415/5fc56e0a281576426931ec4c/html5/thumbnails/21.jpg)
22
End of Applying Java Functional Programming Features: Introduction