the asynctask framework: structure & functionalityschmidt/cs891s/2019-pdfs/...• white-box...

46
Douglas C. Schmidt [email protected] www.dre.vanderbilt.edu/~schmidt Institute for Software Integrated Systems Vanderbilt University Nashville, Tennessee, USA The AsyncTask Framework: Structure & Functionality

Upload: others

Post on 31-Dec-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

Douglas C. [email protected]

www.dre.vanderbilt.edu/~schmidt

Institute for Software

Integrated Systems

Vanderbilt University

Nashville, Tennessee, USA

The AsyncTask Framework:

Structure & Functionality

Page 2: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

2

Learning Objectives in this Part of the Lesson• Recognize the capabilities provided by the Android AsyncTask framework

• Know which methods are provided by AsyncTask class

• Understand what black-box & white-box framework are…

See www.dre.vanderbilt.edu/~schmidt/PDF/DRC.pdf

White-boxBlack-box

Page 3: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

3

• Recognize the capabilities provided by the Android AsyncTask framework

• Know which methods are provided by AsyncTask class

• Understand what black-box & white-box framework are… & how AsyncTaskimplements both types of frameworks

Learning Objectives in this Part of the Lesson

Black-box White-box

Page 4: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

4

Common Types of Frameworks

Page 5: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

5

Common Types of Frameworks• Black-box frameworks only

require understanding external interfaces of objects

See wiki.c2.com/?BlackBoxFramework

Page 6: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

6

Common Types of Frameworks• Black-box frameworks only

require understanding external interfaces of objects

• Framework elements typically reused by parameterizing & assembling objects

Page 7: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

7

Common Types of Frameworks• Black-box frameworks only

require understanding external interfaces of objects

• Framework elements typically reused by parameterizing & assembling objects

• White-box frameworks require understanding some parts of the framework implementation

See wiki.c2.com/?WhiteBoxFramework

Page 8: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

8

Common Types of Frameworks• Black-box frameworks only

require understanding external interfaces of objects

• Framework elements typically reused by parameterizing & assembling objects

• White-box frameworks require understanding some parts of the framework implementation

• Framework elements typically reused by subclassing & overridding

Page 9: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

9

Common Types of Frameworks

• Each category of OO framework uses different sets of patterns

• Black-box frameworks only require understanding external interfaces of objects

• White-box frameworks require understanding some parts of the framework implementation

See www.dre.vanderbilt.edu/~schmidt/PDF/DRC.pdf

Page 10: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

10

Common Types of Frameworks

• Each category of OO framework uses different sets of patterns

See en.wikipedia.org/wiki/Strategy_pattern & en.wikipedia.org/wiki/Decorator_pattern

• Black-box frameworks only require understanding external interfaces of objects

• White-box frameworks require understanding some parts of the framework implementation

Black-box frameworks apply patterns that emphasize extensibility via object composition

Page 11: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

11

Common Types of Frameworks

See en.wikipedia.org/wiki/Template_method & en.wikipedia.org/wiki/State_pattern

• Each category of OO framework uses different sets of patterns

• Black-box frameworks only require understanding external interfaces of objects

• White-box frameworks require understanding some parts of the framework implementation

White-box frameworks apply patterns that emphasize

extensibility via subclassing

Page 12: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

12

Common Types of Frameworks• Black-box frameworks only

require understanding external interfaces of objects

• White-box frameworks require understanding some parts of the framework implementation

• Each category of OO framework uses different sets of patterns

Android’s AsyncTask framework embodies both black-box & white-box frameworks

Page 13: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

13

White-box Elements of the AsyncTask Framework

Page 14: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

14

: Download

Task

: Worker

Runnable

: Default

Executor

: Threaded

Download

doInBackground()

execute()

execute()onPreExecute()call()

onPostExecute()postResult()

UI

Thread

White-box Elements of the AsyncTask Framework • White-box framework

elements enable long

duration operations to

interact with UI thread

See en.wikipedia.org/wiki/Template_method

Background

Thread

Page 15: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

15

: Worker

Runnable

: Default

Executor

doInBackground()

execute()

execute()onPreExecute()call()

onPostExecute()postResult()

UI

Thread

Template method

• White-box framework

elements enable long

duration operations to

interact with UI thread

Background

Thread

: Download

Task

: Threaded

Download

See xp123.com/wwake/fw/ch12-bb.htm for more on framework design

White-box Elements of the AsyncTask Framework

Page 16: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

16

: Worker

Runnable

: Default

Executor

doInBackground()

execute()

execute()onPreExecute()call()

onPostExecute()postResult()

UI

Thread

• Framework dictates

control flow via hook

method callbacks

Background

Thread

: Download

Task

: Threaded

Download

White-box Elements of the AsyncTask Framework

See en.wikipedia.org/wiki/Callback_(computer_programming)

Page 17: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

17

: Worker

Runnable

: Default

Executor

doInBackground()

execute()

execute()onPreExecute()call()

onPostExecute()postResult()

UI

Thread

Background

Thread

Initialization actions

• Framework dictates

control flow via hook

method callbacks

: Download

Task

: Threaded

Download

White-box Elements of the AsyncTask Framework

Page 18: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

18

: Worker

Runnable

: Default

Executor

doInBackground()

execute()

execute()onPreExecute()call()

onPostExecute()postResult()

UI

Thread

Perform long-running task

• Framework dictates

control flow via hook

method callbacks

Background

Thread

: Download

Task

: Threaded

Download

White-box Elements of the AsyncTask Framework

Page 19: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

19

: Worker

Runnable

: Default

Executor

doInBackground()

execute()

execute()onPreExecute()call()

onPostExecute()postResult()

UI

ThreadDisplay

results in UI

• Framework dictates

control flow via hook

method callbacks

Background

Thread

: Download

Task

: Threaded

Download

White-box Elements of the AsyncTask Framework

Page 20: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

20

: Worker

Runnable

: Default

Executor

doInBackground()

execute()

execute()onPreExecute()call()

onPostExecute()postResult()

UI

Thread

• Framework dictates

control flow via hook

method callbacks

Background

Thread

: Download

Task

: Threaded

Download

White-box Elements of the AsyncTask Framework

Page 21: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

21

: Worker

Runnable

: Default

Executor

doInBackground()

execute()

execute()onPreExecute()call()

onPostExecute()postResult()

UI

Thread

• Framework dictates

control flow via hook

method callbacks

Background

Thread

ThisTemplate Method variant allows hook methods to run in different threads

: Download

Task

: Threaded

Download

White-box Elements of the AsyncTask Framework

Page 22: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

22

Black-box Elements of the AsyncTask Framework

Page 23: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

23

• Black-box framework

elements control the

background thread(s)

Black-box Elements of the AsyncTask Framework

: Download

Task

: Worker

Runnable

: Executor: Threaded

Download

doInBackground()

execute()

execute()onPreExecute()call()

postResult()

UI

Thread

Background

Threads

See en.wikipedia.org/wiki/Strategy_pattern

onPostExecute()

Page 24: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

24

• AsyncTask can be

configured via a # of

Executor strategies

: Download

Task

: Worker

Runnable

: Executor: Threaded

Download

doInBackground()

execute()

execute()call()

postResult()

UI

Thread

Background

Thread

sDefault

Executor = SERIAL_EXECUTOR

onPreExecute()

onPostExecute()

See developer.android.com/reference/java/util/concurrent/Executor.html

Black-box Elements of the AsyncTask Framework

Page 25: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

25

• AsyncTask can be

configured via a # of

Executor strategies

: Download

Task

: Worker

Runnable

: Executor: Threaded

Download

doInBackground()

execute()

execute()call()

postResult()

UI

Thread

Background

Thread

onPreExecute()

onPostExecute()

Black-box Elements of the AsyncTask Framework

sDefault

Executor = SERIAL_EXECUTOR

SERIAL_EXECUTOR executes tasks one at a time in serial order

Page 26: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

26

• AsyncTask can be

configured via a # of

Executor strategies

: Download

Task

: Worker

Runnable

: Executor: Threaded

Download

Black-box Elements of the AsyncTask Framework

sDefault

Executor = SERIAL_EXECUTOR

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

class SerialExecutor implements Executor {

final ArrayDeque<Runnable> mTasks = new ArrayDeque<>();

Runnable mActive;

public synchronized void execute(final Runnable r) {

mTasks.offer(() -> { try { r.run(); }

finally { scheduleNext(); }}});

if (mActive == null) scheduleNext();

}

protected synchronized void scheduleNext() {

if ((mActive = mTasks.poll()) != null)

THREAD_POOL_EXECUTOR.execute(mActive);

}

}

Page 27: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

27

• AsyncTask can be

configured via a # of

Executor strategies

: Download

Task

: Worker

Runnable

: Executor: Threaded

Download

Black-box Elements of the AsyncTask Framework

sDefault

Executor = SERIAL_EXECUTOR

class SerialExecutor implements Executor {

final ArrayDeque<Runnable> mTasks = new ArrayDeque<>();

Runnable mActive;

public synchronized void execute(final Runnable r) {

mTasks.offer(() -> { try { r.run(); }

finally { scheduleNext(); }}});

if (mActive == null) scheduleNext();

}

protected synchronized void scheduleNext() {

if ((mActive = mTasks.poll()) != null)

THREAD_POOL_EXECUTOR.execute(mActive);

}

}

Queue up waiting tasks

Page 28: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

28

• AsyncTask can be

configured via a # of

Executor strategies

: Download

Task

: Worker

Runnable

: Executor: Threaded

Download

Black-box Elements of the AsyncTask Framework

sDefault

Executor = SERIAL_EXECUTOR

class SerialExecutor implements Executor {

final ArrayDeque<Runnable> mTasks = new ArrayDeque<>();

Runnable mActive;

public synchronized void execute(final Runnable r) {

mTasks.offer(() -> { try { r.run(); }

finally { scheduleNext(); }}});

if (mActive == null) scheduleNext();

}

protected synchronized void scheduleNext() {

if ((mActive = mTasks.poll()) != null)

THREAD_POOL_EXECUTOR.execute(mActive);

}

}

Currently active task

Page 29: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

29

• AsyncTask can be

configured via a # of

Executor strategies

: Download

Task

: Worker

Runnable

: Executor: Threaded

Download

Black-box Elements of the AsyncTask Framework

sDefault

Executor = SERIAL_EXECUTOR

class SerialExecutor implements Executor {

final ArrayDeque<Runnable> mTasks = new ArrayDeque<>();

Runnable mActive;

public synchronized void execute(final Runnable r) {

mTasks.offer(() -> { try { r.run(); }

finally { scheduleNext(); }}});

if (mActive == null) scheduleNext();

}

protected synchronized void scheduleNext() {

if ((mActive = mTasks.poll()) != null)

THREAD_POOL_EXECUTOR.execute(mActive);

}

}

Execute a task

Page 30: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

30

• AsyncTask can be

configured via a # of

Executor strategies

: Download

Task

: Worker

Runnable

: Executor: Threaded

Download

Black-box Elements of the AsyncTask Framework

sDefault

Executor = SERIAL_EXECUTOR

class SerialExecutor implements Executor {

final ArrayDeque<Runnable> mTasks = new ArrayDeque<>();

Runnable mActive;

public synchronized void execute(final Runnable r) {

mTasks.offer(() -> { try { r.run(); }

finally { scheduleNext(); }}});

if (mActive == null) scheduleNext();

}

protected synchronized void scheduleNext() {

if ((mActive = mTasks.poll()) != null)

THREAD_POOL_EXECUTOR.execute(mActive);

}

}

Enqueue a lambda that runs the task

Page 31: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

31

• AsyncTask can be

configured via a # of

Executor strategies

: Download

Task

: Worker

Runnable

: Executor: Threaded

Download

Black-box Elements of the AsyncTask Framework

sDefault

Executor = SERIAL_EXECUTOR

class SerialExecutor implements Executor {

final ArrayDeque<Runnable> mTasks = new ArrayDeque<>();

Runnable mActive;

public synchronized void execute(final Runnable r) {

mTasks.offer(() -> { try { r.run(); }

finally { scheduleNext(); }}});

if (mActive == null) scheduleNext();

}

protected synchronized void scheduleNext() {

if ((mActive = mTasks.poll()) != null)

THREAD_POOL_EXECUTOR.execute(mActive);

}

}

Schedule a new task if there isn’t one currently running

Page 32: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

32

• AsyncTask can be

configured via a # of

Executor strategies

: Download

Task

: Worker

Runnable

: Executor: Threaded

Download

Black-box Elements of the AsyncTask Framework

sDefault

Executor = SERIAL_EXECUTOR

class SerialExecutor implements Executor {

final ArrayDeque<Runnable> mTasks = new ArrayDeque<>();

Runnable mActive;

public synchronized void execute(final Runnable r) {

mTasks.offer(() -> { try { r.run(); }

finally { scheduleNext(); }}});

if (mActive == null) scheduleNext();

}

protected synchronized void scheduleNext() {

if ((mActive = mTasks.poll()) != null)

THREAD_POOL_EXECUTOR.execute(mActive);

}

}

Schedule a new task for execution if one’s available

Page 33: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

33

• AsyncTask can be

configured via a # of

Executor strategies

: Download

Task

: Worker

Runnable

: Executor: Threaded

Download

Black-box Elements of the AsyncTask Framework

sDefault

Executor = SERIAL_EXECUTOR

class SerialExecutor implements Executor {

final ArrayDeque<Runnable> mTasks = new ArrayDeque<>();

Runnable mActive;

public synchronized void execute(final Runnable r) {

mTasks.offer(() -> { try { r.run(); }

finally { scheduleNext(); }}});

if (mActive == null) scheduleNext();

}

protected synchronized void scheduleNext() {

if ((mActive = mTasks.poll()) != null)

THREAD_POOL_EXECUTOR.execute(mActive);

}

}

Schedule a new task for execution if one’s available

Page 34: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

34

• AsyncTask can be

configured via a # of

Executor strategies

: Download

Task

: Worker

Runnable

: Executor: Threaded

Download

doInBackground()

execute()

execute()onPreExecute()call()

onPostExecute()postResult()

UI

Thread

Background

Thread

Some apps need to run AsyncTask objects in parallel instead of serially

sDefault

Executor = SERIAL_EXECUTOR

Black-box Elements of the AsyncTask Framework

Page 35: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

35

• AsyncTask can be

configured via a # of

Executor strategies

: Download

Task

: Worker

Runnable

: Executor: Threaded

Download

doInBackground()

execute()call()

postResult()

UI

Thread

Background

Threads

Allows multiple long duration tasks to run in parallel within a process

onPreExecute()

onPostExecute()

executeOnExecutor(THREAD_POOL_EXECUTOR,

params)

Black-box Elements of the AsyncTask Framework

Page 36: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

36

• AsyncTask can be

configured via a # of

Executor strategies

: Download

Task

: Worker

Runnable

: Executor: Threaded

Download

doInBackground()

execute()call()

postResult()

UI

Thread

onPreExecute()

onPostExecute()

executeOnExecutor(THREAD_POOL_EXECUTOR,

params)

Black-box Elements of the AsyncTask Framework

THREAD_POOL_EXECUTOR can be used to execute tasks in parallel

static final int CPU_COUNT =

Runtime.getRuntime().availableProcessors();

static final int CORE_POOL_SIZE = CPU_COUNT + 1;

static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1;

static final int KEEP_ALIVE = 1;

static final BlockingQueue<Runnable> sPoolWorkQueue =

new LinkedBlockingQueue<Runnable>(128);

static Executor THREAD_POOL_EXECUTOR = new

ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE,

KEEP_ALIVE, TimeUnit.SECONDS,

sPoolWorkQueue, sThreadFactory);

Page 37: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

37

• AsyncTask can be

configured via a # of

Executor strategies

: Download

Task

: Worker

Runnable

: Executor: Threaded

Download

doInBackground()

execute()call()

postResult()

UI

Thread

onPreExecute()

onPostExecute()

executeOnExecutor(THREAD_POOL_EXECUTOR,

params)

Black-box Elements of the AsyncTask Framework

static final int CPU_COUNT =

Runtime.getRuntime().availableProcessors();

static final int CORE_POOL_SIZE = CPU_COUNT + 1;

static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1;

static final int KEEP_ALIVE = 1;

static final BlockingQueue<Runnable> sPoolWorkQueue =

new LinkedBlockingQueue<Runnable>(128);

static Executor THREAD_POOL_EXECUTOR = new

ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE,

KEEP_ALIVE, TimeUnit.SECONDS,

sPoolWorkQueue, sThreadFactory);

Determine the “core” & “max” pool sizes

Page 38: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

38

• AsyncTask can be

configured via a # of

Executor strategies

: Download

Task

: Worker

Runnable

: Executor: Threaded

Download

doInBackground()

execute()call()

postResult()

UI

Thread

onPreExecute()

onPostExecute()

executeOnExecutor(THREAD_POOL_EXECUTOR,

params)

Black-box Elements of the AsyncTask Framework

static final int CPU_COUNT =

Runtime.getRuntime().availableProcessors();

static final int CORE_POOL_SIZE = CPU_COUNT + 1;

static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1;

static final int KEEP_ALIVE = 1;

static final BlockingQueue<Runnable> sPoolWorkQueue =

new LinkedBlockingQueue<Runnable>(128);

static Executor THREAD_POOL_EXECUTOR = new

ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE,

KEEP_ALIVE, TimeUnit.SECONDS,

sPoolWorkQueue, sThreadFactory);

Create a queue of up to 128 tasks

Page 39: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

39

• AsyncTask can be

configured via a # of

Executor strategies

: Download

Task

: Worker

Runnable

: Executor: Threaded

Download

doInBackground()

execute()call()

postResult()

UI

Thread

onPreExecute()

onPostExecute()

executeOnExecutor(THREAD_POOL_EXECUTOR,

params)

Black-box Elements of the AsyncTask Framework

static final int CPU_COUNT =

Runtime.getRuntime().availableProcessors();

static final int CORE_POOL_SIZE = CPU_COUNT + 1;

static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1;

static final int KEEP_ALIVE = 1;

static final BlockingQueue<Runnable> sPoolWorkQueue =

new LinkedBlockingQueue<Runnable>(128);

static Executor THREAD_POOL_EXECUTOR = new

ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE,

KEEP_ALIVE, TimeUnit.SECONDS,

sPoolWorkQueue, sThreadFactory);

Create a properly configured thread pool

Page 40: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

40

• AsyncTask can be

configured via a # of

Executor strategies

: Download

Task

: Worker

Runnable

: Executor: Threaded

Download

doInBackground()

execute()call()

postResult()

UI

Thread

Background

Threads

onPreExecute()

onPostExecute()

executeOnExecutor(executorStrategy,

params)

Black-box Elements of the AsyncTask Framework

See developer.android.com/reference/java/util/concurrent/ExecutorService.html

It’s also possible to create a custom executor strategy

Page 41: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

41

• AsyncTask can be

configured via a # of

Executor strategies

: Download

Task

: Worker

Runnable

: Executor: Threaded

Download

doInBackground()

execute()call()

postResult()

UI

Thread

Background

ThreadsSERIAL_EXECUTOR,

THREAD_POOL_EXECUTOR,

or custom executor strategy

onPreExecute()

onPostExecute()

executeOnExecutor(executorStrategy,

params)

Black-box Elements of the AsyncTask Framework

Page 42: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

42

• AsyncTask can be

configured via a # of

Executor strategies

: Download

Task

: Worker

Runnable

: Executor: Threaded

Download

doInBackground()

execute()call()

postResult()

UI

Thread

Background

Threads

onPreExecute()

onPostExecute()

executeOnExecutor(executorStrategy,

params)Executor treated as a black-box

Black-box Elements of the AsyncTask Framework

Page 43: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

43

• AsyncTask ensures

certain operations are

safe without explicit

synchronizations

: Download

Task

: Worker

Runnable

: Executor: Threaded

Download

doInBackground()

execute()call()

postResult()

UI

Thread

Background

Threads

onPreExecute()

onPostExecute()

executeOnExecutor(executorStrategy,

params)

Black-box Elements of the AsyncTask Framework

See en.wikipedia.org/wiki/Happened-before

Page 44: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

44

• AsyncTask ensures

certain operations are

safe without explicit

synchronizations, e.g.

• Set fields in the

AsyncTask ctor

or onPreExecute(),

& refer to them in

doInBackground()

: Download

Task

: Worker

Runnable

: Executor: Threaded

Download

doInBackground()

execute()call()

postResult()

UI

Thread

Background

Threads

onPostExecute()

onPreExecute()

executeOnExecutor(executorStrategy,

params)

Black-box Elements of the AsyncTask Framework

Page 45: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

45

• AsyncTask ensures

certain operations are

safe without explicit

synchronizations, e.g.

• Set fields in the

AsyncTask ctor

or onPreExecute(),

& refer to them in

doInBackground()

• Set member fields

in doInBackground()

& refer to them in

onProgressUpdate()

& onPostExecute()

: Download

Task

: Worker

Runnable

: Executor: Threaded

Download

doInBackground()

execute()call()

postResult()

UI

Thread

Background

Threads

onPreExecute()

onPostExecute()

executeOnExecutor(executorStrategy,

params)

Black-box Elements of the AsyncTask Framework

Page 46: The AsyncTask Framework: Structure & Functionalityschmidt/cs891s/2019-PDFs/...• White-box frameworks require understanding some parts of the framework implementation Black-box frameworks

46

End of The AsyncTaskFramework: Structure

& Functionality