The History of Concurrency & Parallelism Support in Java
Transcript of The History of Concurrency & Parallelism Support in Java
![Page 1: The History of Concurrency & Parallelism Support in Java](https://reader030.fdocuments.net/reader030/viewer/2022041208/6250e1255485ee10d7731588/html5/thumbnails/1.jpg)
The History of Concurrency
& Parallelism Support in Java
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: The History of Concurrency & Parallelism Support in Java](https://reader030.fdocuments.net/reader030/viewer/2022041208/6250e1255485ee10d7731588/html5/thumbnails/2.jpg)
2
Learning Objectives in this Part of the Lesson
Additional Frameworks & Languages
Applications
Operating System Kernel
System Libraries
Java Execution Environment (e.g., JVM)
Threading & Synchronization Packages
Java
/JN
IC+
+/C
C
JAVA HISTORY
• Learn the history of Java concurrency & parallelism
![Page 3: The History of Concurrency & Parallelism Support in Java](https://reader030.fdocuments.net/reader030/viewer/2022041208/6250e1255485ee10d7731588/html5/thumbnails/3.jpg)
3
Learning Objectives in this Part of the Lesson
Hopefully, you’ll already know some of this!!!
• Learn the history of Java concurrency & parallelism
Additional Frameworks & Languages
Applications
Operating System Kernel
System Libraries
Java Execution Environment (e.g., JVM)
Threading & Synchronization Packages
Java
/JN
IC+
+/C
C
![Page 4: The History of Concurrency & Parallelism Support in Java](https://reader030.fdocuments.net/reader030/viewer/2022041208/6250e1255485ee10d7731588/html5/thumbnails/4.jpg)
4
A Brief History of Concurrency in Java
![Page 5: The History of Concurrency & Parallelism Support in Java](https://reader030.fdocuments.net/reader030/viewer/2022041208/6250e1255485ee10d7731588/html5/thumbnails/5.jpg)
5
A Brief History of Concurrency in Java• Foundational concurrency support
Additional Frameworks & Languages
Applications
Operating System Kernel
System Libraries
Java Execution Environment (e.g., JVM)
Threading & Synchronization Packages
Java
/JN
IC+
+/C
CSee en.wikipedia.org/wiki/Java_version_history#JDK_1.0
e.g., Java threads & built-in monitor objects were available in Java 1
![Page 6: The History of Concurrency & Parallelism Support in Java](https://reader030.fdocuments.net/reader030/viewer/2022041208/6250e1255485ee10d7731588/html5/thumbnails/6.jpg)
6
A Brief History of Concurrency in Java• Foundational concurrency support
• Focus on basic multi-threading& synchronization primitives
See docs.oracle.com/javase/tutorial/essential/concurrency
![Page 7: The History of Concurrency & Parallelism Support in Java](https://reader030.fdocuments.net/reader030/viewer/2022041208/6250e1255485ee10d7731588/html5/thumbnails/7.jpg)
7
A Brief History of Concurrency in Java• Foundational concurrency support
• Focus on basic multi-threading& synchronization primitives
See github.com/douglascraigschmidt/LiveLessons/tree/master/SimpleBlockingQueue
SimpleBlockingBoundedQueue<Integer>
simpleQueue = new
SimpleBlockingBoundedQueue<>();
Thread[] threads = new Thread[] {
new Thread(new Producer<>
(simpleQueue)),
new Thread(new Consumer<>
(simpleQueue))
};
for (Thread thread : threads)
thread.start();
for (Thread thread : threads)
thread.join();
Allow multiple threads to communicate & interact
via a bounded buffer
![Page 8: The History of Concurrency & Parallelism Support in Java](https://reader030.fdocuments.net/reader030/viewer/2022041208/6250e1255485ee10d7731588/html5/thumbnails/8.jpg)
8
A Brief History of Concurrency in Java• Foundational concurrency support
• Focus on basic multi-threading& synchronization primitives
See github.com/douglascraigschmidt/LiveLessons/tree/master/SimpleBlockingQueue
Start & join these multiple threads
SimpleBlockingBoundedQueue<Integer>
simpleQueue = new
SimpleBlockingBoundedQueue<>();
Thread[] threads = new Thread[] {
new Thread(new Producer<>
(simpleQueue)),
new Thread(new Consumer<>
(simpleQueue))
};
for (Thread thread : threads)
thread.start();
for (Thread thread : threads)
thread.join();
![Page 9: The History of Concurrency & Parallelism Support in Java](https://reader030.fdocuments.net/reader030/viewer/2022041208/6250e1255485ee10d7731588/html5/thumbnails/9.jpg)
9
A Brief History of Concurrency in Java• Foundational concurrency support
• Focus on basic multi-threading& synchronization primitives
See github.com/douglascraigschmidt/LiveLessons/tree/master/SimpleBlockingQueue
class SimpleBlockingBoundedQueue
<E> {
public E take() ...{
synchronized(this) {
while (mList.isEmpty())
wait();
notifyAll();
return mList.poll();
}
}
Built-in monitor object mutual exclusion &
coordination primitives
![Page 10: The History of Concurrency & Parallelism Support in Java](https://reader030.fdocuments.net/reader030/viewer/2022041208/6250e1255485ee10d7731588/html5/thumbnails/10.jpg)
10
A Brief History of Concurrency in Java• Foundational concurrency support
• Focus on basic multi-threading& synchronization primitives
• Efficient, but low-level & very limited in capabilities
![Page 11: The History of Concurrency & Parallelism Support in Java](https://reader030.fdocuments.net/reader030/viewer/2022041208/6250e1255485ee10d7731588/html5/thumbnails/11.jpg)
11
A Brief History of Concurrency in Java• Foundational concurrency support
• Focus on basic multi-threading& synchronization primitives
• Efficient, but low-level & very limited in capabilities
• Many accidental complexities
See en.wikipedia.org/wiki/No_Silver_Bullet
Accidental complexities arise from limitations with software techniques, tools, & methods
![Page 12: The History of Concurrency & Parallelism Support in Java](https://reader030.fdocuments.net/reader030/viewer/2022041208/6250e1255485ee10d7731588/html5/thumbnails/12.jpg)
12
A Brief History of Concurrency in Java• Advanced concurrency support
Additional Frameworks & Languages
Applications
Operating System Kernel
System Libraries
Java Execution Environment (e.g., JVM)
Threading & Synchronization Packages
Java
/JN
IC+
+/C
CSee en.wikipedia.org/wiki/Java_version_history#J2SE_5.0
e.g., Java executor framework, synchronizers, blocking queues, atomics, & concurrent collectionsthat became available in Java 5+
![Page 13: The History of Concurrency & Parallelism Support in Java](https://reader030.fdocuments.net/reader030/viewer/2022041208/6250e1255485ee10d7731588/html5/thumbnails/13.jpg)
13
ThreadPoolExecutor
3.take()
4.run()
A Brief History of Concurrency in Java• Advanced concurrency support
• Focus on course-grained “task parallelism”
See en.wikipedia.org/wiki/Task_parallelism
WorkerThreads
execute() run()
runnable
runnableFuture
Future
Future
Future
Completion
Queue
runnable
WorkQueue
2.offer()
ExecutorCompletionService
submit()
take()
5.add()
1.submit(task)
6.take()
![Page 14: The History of Concurrency & Parallelism Support in Java](https://reader030.fdocuments.net/reader030/viewer/2022041208/6250e1255485ee10d7731588/html5/thumbnails/14.jpg)
14
ThreadPoolExecutor
3.take()
4.run()
A Brief History of Concurrency in Java• Advanced concurrency support
• Focus on course-grained “task parallelism”
• e.g., concurrently run various tasks at the same time
WorkerThreads
execute() run()
runnable
runnableFuture
Future
Future
Future
Completion
Queue
runnable
WorkQueue
2.offer()
ExecutorCompletionService
submit()
take()
5.add()
1.submit(task)
6.take()
![Page 15: The History of Concurrency & Parallelism Support in Java](https://reader030.fdocuments.net/reader030/viewer/2022041208/6250e1255485ee10d7731588/html5/thumbnails/15.jpg)
15
A Brief History of Concurrency in Java
See github.com/douglascraigschmidt/LiveLessons/tree/master/PalantiriManagerApplication
Create a fixed-sized thread pool & also coordinate the starting & stopping of multiple tasks that
acquire/release shared resources
• Advanced concurrency support
• Focus on course-grained “task parallelism”
• e.g., concurrently run various tasks at the same time
ExecutorService executor =
Executors.newFixedThreadPool
(numOfBeings,
mThreadFactory);
...
CyclicBarrier entryBarrier =
new CyclicBarrier(numOfBeings+1);
CountDownLatch exitBarrier =
new CountDownLatch(numOfBeings);
for (int i=0; i < beingCount; ++i)
executor.execute
(makeBeingRunnable(i,
entryBarrier,
exitBarrier));
![Page 16: The History of Concurrency & Parallelism Support in Java](https://reader030.fdocuments.net/reader030/viewer/2022041208/6250e1255485ee10d7731588/html5/thumbnails/16.jpg)
16
A Brief History of Concurrency in Java• Advanced concurrency support
• Focus on course-grained “task parallelism”
• Feature-rich & optimized, but also tedious & error-prone to program
![Page 17: The History of Concurrency & Parallelism Support in Java](https://reader030.fdocuments.net/reader030/viewer/2022041208/6250e1255485ee10d7731588/html5/thumbnails/17.jpg)
17
A Brief History of Parallelism in Java
![Page 18: The History of Concurrency & Parallelism Support in Java](https://reader030.fdocuments.net/reader030/viewer/2022041208/6250e1255485ee10d7731588/html5/thumbnails/18.jpg)
18
A Brief History of Parallelism in Java• Foundational parallelism support
Additional Frameworks & Languages
Applications
Operating System Kernel
System Libraries
Java Execution Environment (e.g., JVM)
Threading & Synchronization Packages
Java
/JN
IC+
+/C
CSee en.wikipedia.org/wiki/Java_version_history#Java_SE_7
e.g., Java fork-join pool was released in Java 7
![Page 19: The History of Concurrency & Parallelism Support in Java](https://reader030.fdocuments.net/reader030/viewer/2022041208/6250e1255485ee10d7731588/html5/thumbnails/19.jpg)
19
A Brief History of Parallelism in Java• Foundational parallelism support
• Focus on finer-grained data parallelism
See en.wikipedia.org/wiki/Data_parallelism
join join
join
Processsequentially
Processsequentially
Processsequentially
Processsequentially
Sub-task1.1 Sub-task1.2 Sub-task2.1 Sub-task2.2
Sub-task1 Sub-task2
fork()
Task
fork() fork()
![Page 20: The History of Concurrency & Parallelism Support in Java](https://reader030.fdocuments.net/reader030/viewer/2022041208/6250e1255485ee10d7731588/html5/thumbnails/20.jpg)
20
A Brief History of Parallelism in Java• Foundational parallelism support
• Focus on finer-grained data parallelism
• e.g., runs the same task on different elements of data by using the “split-apply-combine” model
See www.jstatsoft.org/article/view/v040i01/v40i01.pdf
join join
join
Processsequentially
Processsequentially
Processsequentially
Processsequentially
Sub-task1.1 Sub-task1.2 Sub-task2.1 Sub-task2.2
Sub-task1 Sub-task2
fork()
Task
fork() fork()
![Page 21: The History of Concurrency & Parallelism Support in Java](https://reader030.fdocuments.net/reader030/viewer/2022041208/6250e1255485ee10d7731588/html5/thumbnails/21.jpg)
21See github.com/douglascraigschmidt/LiveLessons/tree/master/SearchForkJoin
A Brief History of Parallelism in Java• Foundational parallelism support
• Focus on finer-grained data parallelism
• e.g., runs the same task on different elements of data by using the “split-apply-combine” model
List<List<SearchResults>>
listOfListOfSearchResults =
ForkJoinPool
.commonPool()
.invoke(new
SearchWithForkJoinTask
(inputList,
mPhrasesToFind, ...));
Use a common fork-join pool to search input strings to locate phrases that match
45,000+ phrases
Search Phrases
Input Strings to Search
…
![Page 22: The History of Concurrency & Parallelism Support in Java](https://reader030.fdocuments.net/reader030/viewer/2022041208/6250e1255485ee10d7731588/html5/thumbnails/22.jpg)
22
A Brief History of Parallelism in Java• Foundational parallelism support
• Focus on data parallelism
• Powerful & scalable, but tedious to program directly
join join
join
Processsequentially
Processsequentially
Processsequentially
Processsequentially
Sub-task1.1 Sub-task1.2 Sub-task2.1 Sub-task2.2
Sub-task1 Sub-task2
fork()
Task
fork() fork()
![Page 23: The History of Concurrency & Parallelism Support in Java](https://reader030.fdocuments.net/reader030/viewer/2022041208/6250e1255485ee10d7731588/html5/thumbnails/23.jpg)
23
A Brief History of Parallelism in Java• Advanced parallelism support
Additional Frameworks & Languages
Applications
Operating System Kernel
System Libraries
Java Execution Environment (e.g., JVM)
Threading & Synchronization Packages
Java
/JN
IC+
+/C
CSee en.wikipedia.org/wiki/Java_version_history#Java_SE_8
e.g., Java parallel streams & completable futures
made available in Java 8
![Page 24: The History of Concurrency & Parallelism Support in Java](https://reader030.fdocuments.net/reader030/viewer/2022041208/6250e1255485ee10d7731588/html5/thumbnails/24.jpg)
24
A Brief History of Parallelism in Java
filter(not(this::urlCached))
collect(toList())
Parallel Streams
…
map(this::downloadImage)
flatMap(this::applyFilters)
• Advanced parallelism support
• Focus on fine-grained functional programming for data parallelism
See en.wikipedia.org/wiki/Data_parallelism
![Page 25: The History of Concurrency & Parallelism Support in Java](https://reader030.fdocuments.net/reader030/viewer/2022041208/6250e1255485ee10d7731588/html5/thumbnails/25.jpg)
25
A Brief History of Parallelism in Java• Advanced parallelism support
• Focus on fine-grained functional programming for data parallelism & reactive asynchrony
/page\ =
supplyAsync
(getStartPage())
/imgNum2\ = /page\
.thenComposeAsync
(crawlHyperLinks
(page))
/imgNum1\ = /page\
.thenApplyAsync
(countImages(page))
.thenApply(List::size)
/imgNum1\.thenCombine(/imgNum2\,
(imgNum1, imgNum2) ->
Integer::sum)
See gist.github.com/staltz/868e7e9bc2a7b8c1f754
![Page 26: The History of Concurrency & Parallelism Support in Java](https://reader030.fdocuments.net/reader030/viewer/2022041208/6250e1255485ee10d7731588/html5/thumbnails/26.jpg)
26
A Brief History of Parallelism in Java• Advanced parallelism support
• Focus on fine-grained functional programming for data parallelism & reactive asynchrony
See github.com/douglascraigschmidt/LiveLessons/tree/master/ImageStreamGang
Synchronously download images that aren’t already cached from a list of URLs
& process/store the images in parallel
List<Image> images =
urls
.parallelStream()
.filter(not(this::urlCached))
.map(this::downloadImage)
.flatMap(this::applyFilters)
.collect(toList());
![Page 27: The History of Concurrency & Parallelism Support in Java](https://reader030.fdocuments.net/reader030/viewer/2022041208/6250e1255485ee10d7731588/html5/thumbnails/27.jpg)
27
A Brief History of Parallelism in Java• Advanced parallelism support
• Focus on fine-grained functional programming for data parallelism & reactive asynchrony
See github.com/douglascraigschmidt/LiveLessons/tree/master/ImageStreamGang
CompletableFuture<Stream<Image>>
resultsFuture = urls
.stream()
.map(this::checkUrlCachedAsync)
.map(this::downloadImageAsync)
.flatMap(this::applyFiltersAsync)
.collect(toFuture())
.thenApply(stream ->
log(stream.flatMap
(Optional::stream),
urls.size()))
.join();
Combines streams & completable futures to asynchronously download images that aren’t already cached from a list of URLs
& process/store the images in parallel
![Page 28: The History of Concurrency & Parallelism Support in Java](https://reader030.fdocuments.net/reader030/viewer/2022041208/6250e1255485ee10d7731588/html5/thumbnails/28.jpg)
28
A Brief History of Parallelism in Java• Advanced parallelism support
• Focus on fine-grained functional programming for data parallelism & reactive asynchrony
• Focus on pub/sub reactivestreams frameworks
Additional Frameworks & Languages
Applications
Operating System Kernel
System Libraries
Java Execution Environment (e.g., JVM)
Threading & Synchronization Packages
Java
/JN
IC+
+/C
CSee en.wikipedia.org/wiki/Java_version_history#Java_SE_9
e.g., Java reactive streams made available in Java 9 have enabled the RxJava & Project Reactor frameworks
![Page 29: The History of Concurrency & Parallelism Support in Java](https://reader030.fdocuments.net/reader030/viewer/2022041208/6250e1255485ee10d7731588/html5/thumbnails/29.jpg)
29
A Brief History of Parallelism in Java• Advanced parallelism support
• Focus on fine-grained functional programming for data parallelism & reactive asynchrony
• Focus on pub/sub reactive streams frameworks
See github.com/douglascraigschmidt/LiveLessons/tree/master/ImageStreamGang
List<Image> filteredImages =
ReactorUtils
.fromIterableParallel(urls)
.filter(url -> !urlCached(url))
.map(this::blockingDownload)
.flatMap(this::applyFilters)
.sequential()
.collectList()
.block();
Applies RxJava & Project Reactor reactive streams to asynchronously download images
that aren’t already cached from a list of URLs & process/store the images in parallel
![Page 30: The History of Concurrency & Parallelism Support in Java](https://reader030.fdocuments.net/reader030/viewer/2022041208/6250e1255485ee10d7731588/html5/thumbnails/30.jpg)
30
A Brief History of Parallelism in Java• Advanced parallelism support
• Focus on fine-grained functional programming for data parallelism & reactive asynchrony
• Focus on pub/sub reactive streams frameworks
• Strikes an effective balance between productivity & performance
![Page 31: The History of Concurrency & Parallelism Support in Java](https://reader030.fdocuments.net/reader030/viewer/2022041208/6250e1255485ee10d7731588/html5/thumbnails/31.jpg)
31
A Brief History of Parallelism in Java• Advanced parallelism support
• Focus on fine-grained functional programming for data parallelism & reactive asynchrony
• Focus on pub/sub reactive streams frameworks
• Strikes an effective balance between productivity & performance
• However, may be overly prescriptive
![Page 32: The History of Concurrency & Parallelism Support in Java](https://reader030.fdocuments.net/reader030/viewer/2022041208/6250e1255485ee10d7731588/html5/thumbnails/32.jpg)
32
The Evolution of Java from Concurrency to Parallelism
![Page 33: The History of Concurrency & Parallelism Support in Java](https://reader030.fdocuments.net/reader030/viewer/2022041208/6250e1255485ee10d7731588/html5/thumbnails/33.jpg)
33
• Brian Goetz has an excellent talkabout the evolution of Java fromconcurrent to parallel computing
See www.youtube.com/watch?v=NsDE7E8sIdQ
The Evolution of Java from Concurrency to Parallelism
![Page 34: The History of Concurrency & Parallelism Support in Java](https://reader030.fdocuments.net/reader030/viewer/2022041208/6250e1255485ee10d7731588/html5/thumbnails/34.jpg)
34See www.infoq.com/presentations/parallel-java-se-8
• Brian Goetz has an excellent talkabout the evolution of Java fromconcurrent to parallel computing
His talk emphasizes that Java 8 combines functional programming with fine-grained data parallelism to leverage many-core processors
The Evolution of Java from Concurrency to Parallelism
![Page 35: The History of Concurrency & Parallelism Support in Java](https://reader030.fdocuments.net/reader030/viewer/2022041208/6250e1255485ee10d7731588/html5/thumbnails/35.jpg)
35See www.youtube.com/watch?v=cN_DpYBzKso
• Rob Pike also has a good talk thatexplains the differences betweenconcurrency & parallelism
The Evolution of Java from Concurrency to Parallelism
His talk emphasizes that concurrency is about dealing with lots of things at once,
whereas parallelism is about doing lots of things at once
![Page 36: The History of Concurrency & Parallelism Support in Java](https://reader030.fdocuments.net/reader030/viewer/2022041208/6250e1255485ee10d7731588/html5/thumbnails/36.jpg)
36
End of the History of Concurrency & Parallelism
Support in Java