java executor : implementation choicesschmidt/cs891s/2020-pdfs/5... · •recognize the single...

12
Java Executor : Implementation Choices Douglas C. Schmidt [email protected] www.dre.vanderbilt.edu/~schmidt Professor of Computer Science Institute for Software Integrated Systems Vanderbilt University Nashville, Tennessee, USA

Upload: others

Post on 21-Aug-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Java Executor : Implementation Choicesschmidt/cs891s/2020-PDFs/5... · •Recognize the single simple feature provided by the Java Executor interface •Understand various implementation

Java Executor : Implementation Choices

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 Executor : Implementation Choicesschmidt/cs891s/2020-PDFs/5... · •Recognize the single simple feature provided by the Java Executor interface •Understand various implementation

2

Learning Objectives in this Part of the Lesson• Recognize the single simple feature provided by the Java Executor interface

• Understand various implementation choices for the Executor interface

Work-stealing

Thread Pool

Fixed-sized

Thread Pool

A Custom

Thread Pool

Cached

Thread Pool

Page 3: Java Executor : Implementation Choicesschmidt/cs891s/2020-PDFs/5... · •Recognize the single simple feature provided by the Java Executor interface •Understand various implementation

3

Implementation Choices for the Java Executor Interface

Page 4: Java Executor : Implementation Choicesschmidt/cs891s/2020-PDFs/5... · •Recognize the single simple feature provided by the Java Executor interface •Understand various implementation

4

• The Executor interface can be implemented via different types of thread pooling mechanisms

Overview of the Java Executor Interface

Work-stealing

Thread Pool

Fixed-sized

Thread Pool

A Custom

Thread Pool

Cached

Thread Pool

Page 5: Java Executor : Implementation Choicesschmidt/cs891s/2020-PDFs/5... · •Recognize the single simple feature provided by the Java Executor interface •Understand various implementation

5

• Executor configuration is often performed just onceto select the “execution policy” for tasks passed to it

Overview of the Java Executor Interface

Work-stealing

Thread PoolA Custom

Thread Pool

Fixed-sized

Thread PoolCached

Thread Pool

Page 6: Java Executor : Implementation Choicesschmidt/cs891s/2020-PDFs/5... · •Recognize the single simple feature provided by the Java Executor interface •Understand various implementation

6

• The “execution policy” for a group of tasks definesseveral properties

Overview of the Java Executor Interface

Page 7: Java Executor : Implementation Choicesschmidt/cs891s/2020-PDFs/5... · •Recognize the single simple feature provided by the Java Executor interface •Understand various implementation

7

• The “execution policy” for a group of tasks definesseveral properties, e.g.

• In which thread will a task be executed

• e.g., a existing thread in the pool, a new thread created/added to the pool, etc.

Overview of the Java Executor Interface

There’s even a single threaded implementation of Executor!

Page 8: Java Executor : Implementation Choicesschmidt/cs891s/2020-PDFs/5... · •Recognize the single simple feature provided by the Java Executor interface •Understand various implementation

8

• The “execution policy” for a group of tasks definesseveral properties, e.g.

• In which thread will a task be executed

• In which order will tasks be executed

• e.g., FIFO, LIFO, priority order, etc.

Overview of the Java Executor Interface

FIFO LIFO

Priority order

Page 9: Java Executor : Implementation Choicesschmidt/cs891s/2020-PDFs/5... · •Recognize the single simple feature provided by the Java Executor interface •Understand various implementation

9

• The “execution policy” for a group of tasks definesseveral properties, e.g.

• In which thread will a task be executed

• In which order will tasks be executed

• How many tasks can run concurrently

• e.g., is the maximum # of tasks limited by the # of CPU cores or by some other factor?

Overview of the Java Executor Interface

Page 10: Java Executor : Implementation Choicesschmidt/cs891s/2020-PDFs/5... · •Recognize the single simple feature provided by the Java Executor interface •Understand various implementation

10

• The “execution policy” for a group of tasks definesseveral properties, e.g.

• In which thread will a task be executed

• In which order will tasks be executed

• How many tasks can run concurrently

• If not all tasks can be executed due to system overload which task(s) should berejected & how should an app be notified

• e.g., should execute() fail silently vs.throw RejectedExecutionException

Overview of the Java Executor Interface

See docs.oracle.com/javase/8/docs/api/java/util/concurrent/RejectedExecutionException.html

Page 11: Java Executor : Implementation Choicesschmidt/cs891s/2020-PDFs/5... · •Recognize the single simple feature provided by the Java Executor interface •Understand various implementation

11

• The “execution policy” for a group of tasks definesseveral properties, e.g.

• In which thread will a task be executed

• In which order will tasks be executed

• How many tasks can run concurrently

• If not all tasks can be executed due to system overload which task(s) should berejected & how should an app be notified

• What actions (if any) should be performed before and/or after executing a task

• e.g., Android AsyncTask’s onPreExecute() & onPostExecute() hook methods

Overview of the Java Executor Interface

4. doInBackGround()

AsyncTask

1. execute(url)

3. execute(future)

2. onPreExecute()

5. onProgressUpdate()

6. onPostExecute()

Executor

See developer.android.com/reference/android/os/AsyncTask

Page 12: Java Executor : Implementation Choicesschmidt/cs891s/2020-PDFs/5... · •Recognize the single simple feature provided by the Java Executor interface •Understand various implementation

12

End of Java Executor: Implementation Choices