java executorcompletionservice: introductionschmidt/cs891s/2020-pdfs/7...7 •completionservice...
TRANSCRIPT
![Page 1: Java ExecutorCompletionService: Introductionschmidt/cs891s/2020-PDFs/7...7 •CompletionService fixes this problem via an “async future” processing model that combines an executor](https://reader034.vdocument.in/reader034/viewer/2022042216/5ebe344df51e84080b18b674/html5/thumbnails/1.jpg)
Java ExecutorCompletionService:
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: Java ExecutorCompletionService: Introductionschmidt/cs891s/2020-PDFs/7...7 •CompletionService fixes this problem via an “async future” processing model that combines an executor](https://reader034.vdocument.in/reader034/viewer/2022042216/5ebe344df51e84080b18b674/html5/thumbnails/2.jpg)
2
Learning Objectives in this Part of the Lesson• Understand how Java CompletionService’s interface defines a framework for
submitting async taks & handling their completion
-completion
Queue
0..n
-executor
![Page 3: Java ExecutorCompletionService: Introductionschmidt/cs891s/2020-PDFs/7...7 •CompletionService fixes this problem via an “async future” processing model that combines an executor](https://reader034.vdocument.in/reader034/viewer/2022042216/5ebe344df51e84080b18b674/html5/thumbnails/3.jpg)
3
Learning Objectives in this Part of the Lesson• Understand how Java CompletionService’s interface defines a framework for
submitting async taks & handling their completion
• Know how to instantiate the JavaExecutorCompletionService
mExecutorService =
Executors.newFixedThreadPool
(Runtime
.getRuntime
.availableProcessors());
mExecutorCompletionService =
new ExecutorCompletionService<>
(mExecutorService);
![Page 4: Java ExecutorCompletionService: Introductionschmidt/cs891s/2020-PDFs/7...7 •CompletionService fixes this problem via an “async future” processing model that combines an executor](https://reader034.vdocument.in/reader034/viewer/2022042216/5ebe344df51e84080b18b674/html5/thumbnails/4.jpg)
4
Motivating the Java CompletionService Interface
![Page 5: Java ExecutorCompletionService: Introductionschmidt/cs891s/2020-PDFs/7...7 •CompletionService fixes this problem via an “async future” processing model that combines an executor](https://reader034.vdocument.in/reader034/viewer/2022042216/5ebe344df51e84080b18b674/html5/thumbnails/5.jpg)
5
• One problem with the ExecutorService implementation of the PrimeCheckerapp is that the future submit() returned must be handled synchronously
Motivating the Java CompletionService Interface
...
private class FutureRunnable
implements Runnable {
List<Future<PrimeCallable.PrimeResult>>
mFutures;
MainActivity mActivity; ...
public void run() {
mFutures.forEach(future -> {
PrimeCallable.PrimeResult pr =
rethrowSupplier(future::get)
.get();
...
This blocking problem is common w/the “synchronous future” processing model
future::get may block the thread, even if some other
futures may have completed
![Page 6: Java ExecutorCompletionService: Introductionschmidt/cs891s/2020-PDFs/7...7 •CompletionService fixes this problem via an “async future” processing model that combines an executor](https://reader034.vdocument.in/reader034/viewer/2022042216/5ebe344df51e84080b18b674/html5/thumbnails/6.jpg)
6
• CompletionService fixes this problem via an “async future” processing model that combines an executor with an (internal) blocking queue
ThreadPoolExecutor
WorkerThreads
execute() run()
3.take()
4.run()
5.done()
Future
Future
Future
Future
Completion
Queue
Queueing
Future
WorkQueue
Queueing
Future
Queueing
Future
2.offer()
ExecutorCompletionService
submit()
take()
6.add()
1.submit(task)
7.take()
Queueing
Future
Two-way task results are stored in a completion queue & can be processed immediately
Motivating the Java CompletionService Interface
![Page 7: Java ExecutorCompletionService: Introductionschmidt/cs891s/2020-PDFs/7...7 •CompletionService fixes this problem via an “async future” processing model that combines an executor](https://reader034.vdocument.in/reader034/viewer/2022042216/5ebe344df51e84080b18b674/html5/thumbnails/7.jpg)
7
• CompletionService fixes this problem via an “async future” processing model that combines an executor with an (internal) blocking queue
ThreadPoolExecutor
WorkerThreads
execute() run()
3.take()
4.run()
5.done()
Future
Future
Future
Future
Completion
Queue
Queueing
Future
WorkQueue
Queueing
Future
Queueing
Future
2.offer()
ExecutorCompletionService
submit()
take()
6.add()
1.submit(task)
7.take()
Queueing
Future
1+ client threads can submit tasks & 1+ client threads can process their results
Motivating the Java CompletionService Interface
![Page 8: Java ExecutorCompletionService: Introductionschmidt/cs891s/2020-PDFs/7...7 •CompletionService fixes this problem via an “async future” processing model that combines an executor](https://reader034.vdocument.in/reader034/viewer/2022042216/5ebe344df51e84080b18b674/html5/thumbnails/8.jpg)
8
Overview of the Java CompletionService Interface
![Page 9: Java ExecutorCompletionService: Introductionschmidt/cs891s/2020-PDFs/7...7 •CompletionService fixes this problem via an “async future” processing model that combines an executor](https://reader034.vdocument.in/reader034/viewer/2022042216/5ebe344df51e84080b18b674/html5/thumbnails/9.jpg)
9
Overview of the Java CompletionService Interface
See docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletionService.html
-completion
Queue
0..n
-executor
• The CompletionService interface decouples async task invocation from the processing of completed task results
![Page 10: Java ExecutorCompletionService: Introductionschmidt/cs891s/2020-PDFs/7...7 •CompletionService fixes this problem via an “async future” processing model that combines an executor](https://reader034.vdocument.in/reader034/viewer/2022042216/5ebe344df51e84080b18b674/html5/thumbnails/10.jpg)
10
• The CompletionService interface decouples async task invocation from the processing of completed task results
• Implemented via the ExecutorCompletionService class
Overview of the Java CompletionService Interface
-completion
Queue
0..n
-executor
See docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorCompletionService.html
![Page 11: Java ExecutorCompletionService: Introductionschmidt/cs891s/2020-PDFs/7...7 •CompletionService fixes this problem via an “async future” processing model that combines an executor](https://reader034.vdocument.in/reader034/viewer/2022042216/5ebe344df51e84080b18b674/html5/thumbnails/11.jpg)
11
-completion
Queue
0..n
-executor
This class contains both an Executor & a BlockingQueue
Overview of the Java CompletionService Interface• The CompletionService interface decouples async task
invocation from the processing of completed task results
• Implemented via the ExecutorCompletionService class
![Page 12: Java ExecutorCompletionService: Introductionschmidt/cs891s/2020-PDFs/7...7 •CompletionService fixes this problem via an “async future” processing model that combines an executor](https://reader034.vdocument.in/reader034/viewer/2022042216/5ebe344df51e84080b18b674/html5/thumbnails/12.jpg)
12See docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executor.html
Overview of the Java CompletionService Interface
-completion
Queue
0..n
-executor
• The CompletionService interface decouples async task invocation from the processing of completed task results
• Implemented via the ExecutorCompletionService class
An Executor runs tasks in a pool of threads
![Page 13: Java ExecutorCompletionService: Introductionschmidt/cs891s/2020-PDFs/7...7 •CompletionService fixes this problem via an “async future” processing model that combines an executor](https://reader034.vdocument.in/reader034/viewer/2022042216/5ebe344df51e84080b18b674/html5/thumbnails/13.jpg)
13
Overview of the Java CompletionService Interface
-completion
Queue
0..n
-executor
• The CompletionService interface decouples async task invocation from the processing of completed task results
• Implemented via the ExecutorCompletionService class
Completed tasks are put blocking queue accessed via take()/poll()
See docs.oracle.com/javase/8/docs/api/java/util/concurrent/BlockingQueue.html
![Page 14: Java ExecutorCompletionService: Introductionschmidt/cs891s/2020-PDFs/7...7 •CompletionService fixes this problem via an “async future” processing model that combines an executor](https://reader034.vdocument.in/reader034/viewer/2022042216/5ebe344df51e84080b18b674/html5/thumbnails/14.jpg)
14See src/share/classes/java/util/concurrent/ExecutorCompletionService.java
Overview of the Java CompletionService Interface
-completion
Queue
0..n
-executor
• The CompletionService interface decouples async task invocation from the processing of completed task results
• Implemented via the ExecutorCompletionService class
Extends FutureTask to queue a task when it’s “done”
![Page 15: Java ExecutorCompletionService: Introductionschmidt/cs891s/2020-PDFs/7...7 •CompletionService fixes this problem via an “async future” processing model that combines an executor](https://reader034.vdocument.in/reader034/viewer/2022042216/5ebe344df51e84080b18b674/html5/thumbnails/15.jpg)
15
• CompletionService can implement the Proactor pattern
See en.wikipedia.org/wiki/Proactor_pattern
Overview of the Java CompletionService Interface
Supports demultiplexing & dispatching of event handlers that are triggered by the completion of async events
![Page 16: Java ExecutorCompletionService: Introductionschmidt/cs891s/2020-PDFs/7...7 •CompletionService fixes this problem via an “async future” processing model that combines an executor](https://reader034.vdocument.in/reader034/viewer/2022042216/5ebe344df51e84080b18b674/html5/thumbnails/16.jpg)
16
Instantiating the JavaExecutorCompletionService
![Page 17: Java ExecutorCompletionService: Introductionschmidt/cs891s/2020-PDFs/7...7 •CompletionService fixes this problem via an “async future” processing model that combines an executor](https://reader034.vdocument.in/reader034/viewer/2022042216/5ebe344df51e84080b18b674/html5/thumbnails/17.jpg)
17
Instantiating the Java ExecutorCompletionService• ExecutorCompletionService implements CompletionService
& uses an executor to execute tasks placed on a blocking queue when they complete
See docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorCompletionService.html
-completion
Queue
0..n
-executor
![Page 18: Java ExecutorCompletionService: Introductionschmidt/cs891s/2020-PDFs/7...7 •CompletionService fixes this problem via an “async future” processing model that combines an executor](https://reader034.vdocument.in/reader034/viewer/2022042216/5ebe344df51e84080b18b674/html5/thumbnails/18.jpg)
18
• A program typically creates an Executor (or ExecutorService) instance & then associates it with a new ExecutorCompletionService
mExecutorService =
Executors.newFixedThreadPool(Runtime.getRuntime()
.availableProcessors());
mExecutorCompletionService =
new ExecutorCompletionService<>(mExecutorService);
Instantiating the Java ExecutorCompletionService
![Page 19: Java ExecutorCompletionService: Introductionschmidt/cs891s/2020-PDFs/7...7 •CompletionService fixes this problem via an “async future” processing model that combines an executor](https://reader034.vdocument.in/reader034/viewer/2022042216/5ebe344df51e84080b18b674/html5/thumbnails/19.jpg)
19
• A program typically creates an Executor (or ExecutorService) instance & then associates it with a new ExecutorCompletionService
mExecutorService =
Executors.newFixedThreadPool(Runtime.getRuntime()
.availableProcessors());
mExecutorCompletionService =
new ExecutorCompletionService<>(mExecutorService);
Create an executor service whose thread pool size matches the # of cores
Instantiating the Java ExecutorCompletionService
![Page 20: Java ExecutorCompletionService: Introductionschmidt/cs891s/2020-PDFs/7...7 •CompletionService fixes this problem via an “async future” processing model that combines an executor](https://reader034.vdocument.in/reader034/viewer/2022042216/5ebe344df51e84080b18b674/html5/thumbnails/20.jpg)
20
• A program typically creates an Executor (or ExecutorService) instance & then associates it with a new ExecutorCompletionService
mExecutorService =
Executors.newFixedThreadPool(Runtime.getRuntime()
.availableProcessors());
mExecutorCompletionService =
new ExecutorCompletionService<>(mExecutorService);
Associate ExecutorCompletionService with executor service
Instantiating the Java ExecutorCompletionService
![Page 21: Java ExecutorCompletionService: Introductionschmidt/cs891s/2020-PDFs/7...7 •CompletionService fixes this problem via an “async future” processing model that combines an executor](https://reader034.vdocument.in/reader034/viewer/2022042216/5ebe344df51e84080b18b674/html5/thumbnails/21.jpg)
21
End of Java Executor CompletionService:
Introduction