61
Overview of the AsyncTask Framework (Part 1) Douglas C. Schmidt [email protected] www.dre.vanderbilt.edu/~schmidt Institute for Software Integrated Systems Vanderbilt University Nashville, Tennessee, USA

Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

Overview of the AsyncTask Framework (Part 1)

Douglas C. [email protected]

www.dre.vanderbilt.edu/~schmidt

Institute for Software Integrated Systems

Vanderbilt University Nashville, Tennessee, USA

Page 2: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

2

AsyncTaskHandler

3. execute(future)

2. onPreExecute()

4. doInBackGround()

FutureTask

Executor

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

UI Thread(main thread)

Loop

er Message

Message

Message

Message

Message

Message Queue

Message

5. onProgressUpdate()

6. onPostExecute()

1. execute(url)

Allows apps to perform background operations & publish results on UI thread without manipulating threads, handlers, messages, or runnables

Page 3: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

3

Overview of the AsyncTask Framework

Page 4: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

4

Overview of the AsyncTask Framework

Loop

er Message

Message

Message

Message

Message Queue

Message

• Classes in HaMeR framework are loosely connected

UI Thread(main thread)

Runnable

Message

Background Thread A

Handler

Handler

Background Thread B

Message

e.g., it’s not clear that the classes in the HaMeR framework are related

Page 5: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

5

Loop

er Message

Message

Message

Message

Message

Message Queue

Message

• Classes in HaMeR framework are loosely connected• This flexibility works well for

simple concurrency use cases

UI Thread(main thread)

Overview of the AsyncTask Framework

Runnable

Handler

Background Thread B

Page 6: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

6

Loop

er Message

Message

Message

Message

Message

Message Queue

Message

• Classes in HaMeR framework are loosely connected• This flexibility works well for

simple concurrency use cases

UI Thread(main thread)

Overview of the AsyncTask Framework

Runnable

Handler

Background Thread B

e.g., where a background thread posts a runnable to the UI thread…

Page 7: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

7

Loop

er Message

Message

Message

Message

Message

Message Queue

Message

• Classes in HaMeR framework are loosely connected• This flexibility works well for

simple concurrency use cases

UI Thread(main thread)

Overview of the AsyncTask Framework

Runnable

Handler

Background Thread B

… & the UI thread dispatches the run() hook method of the runnable

Page 8: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

8

Loop

er Message

Message

Message

Message

Message Queue

Message

• However, there are drawbacks to the HaMeR concurrency framework

UI Thread(main thread)

Runnable

Message

Background Thread A

Handler

Handler

Background Thread B

Overview of the AsyncTask Framework

Message

Page 9: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

9

Loop

er Message

Message

Message

Message

Message Queue

Message

• However, there are drawbacks to the HaMeR concurrency framework• Must understand patterns to use

this framework effectively

UI Thread(main thread)

Runnable

Message

Background Thread A

Handler

Handler

Background Thread B

Overview of the AsyncTask Framework

Message

See en.wikipedia.org/wiki/Active_object & www.dre.vanderbilt.edu/~schmidt/CommandProcessor.pdf

Page 10: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

10

Loop

er Message

Message

Message

Message

Message Queue

Message

• However, there are drawbacks to the HaMeR concurrency framework• Must understand patterns to use

this framework effectively• Tedious & error-prone to use

Runnable

Message

Background Thread A

Handler

Handler

Background Thread B

UI Thread(main thread)

Overview of the AsyncTask Framework

Message

e.g., apps must understand how to manage the lifecycle of messages

handleMessage()

Page 11: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

11

• However, there are drawbacks to the HaMeR concurrency framework• Must understand patterns to use

this framework effectively• Tedious & error-prone to use• All communication between

threads must be explicitly programmed

Overview of the AsyncTask Framework

mHandler.post(runnable)

mHandler.sendMessage(message)

Page 12: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

12

• However, there are drawbacks to the HaMeR concurrency framework• Must understand patterns to use

this framework effectively• Tedious & error-prone to use• All communication between

threads must be explicitly programmed

• Likewise, any “pre” and/or “post” processing must be explicitly programmed • e.g., starting & stopping a

progress dialog box

Overview of the AsyncTask Framework

downloading via AsyncTask

Page 13: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

13

• However, there are drawbacks to the HaMeR concurrency framework• Must understand patterns to use

this framework effectively• Tedious & error-prone to use• All communication between

threads must be explicitly programmed

• Likewise, any “pre” and/or “post” processing must be explicitly programmed

• Performance can’t be scaled up transparently

Overview of the AsyncTask Framework

Loop

er Message

Message

Message

Message

Message Queue

MessageUI Thread(main thread)

Message

Executor

Page 14: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

14

• In contrast, AsyncTask framework classes are more strongly connected

AsyncTask

ExecutorFutu

reTa

sk Handler

Loop

er Message

Message

Message

Message

Message

Message Queue

MessageUI Thread(main thread)

Overview of the AsyncTask Framework

Page 15: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

15

• In contrast, AsyncTask framework classes are more strongly connected• Complex framework details

hidden via Façade pattern

Overview of the AsyncTask Framework

See en.wikipedia.org/wiki/Facade_pattern

AsyncTask1. execute(url)

Page 16: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

16

• In contrast, AsyncTask framework classes are more strongly connected• Complex framework details

hidden via Façade pattern• Encapsulates a complicated

subsystem or framework with a simpler interface

AsyncTaskHandler

3. execute(future)

2. onPreExecute()

4. doInBackGround()

FutureTask

Executor

5. onProgressUpdate()

6. onPostExecute()

1. execute(url)

Overview of the AsyncTask Framework

Page 17: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

17

• In contrast, AsyncTask framework classes are more strongly connected• Complex framework details

hidden via Façade pattern• Yields a smaller “surface area”

AsyncTask

ExecutorFutu

reTa

sk Handler

Loop

er Message

Message

Message

Message

Message

Message Queue

MessageUI Thread(main thread)

Overview of the AsyncTask Framework

i.e., programmers can focus on the “what” not the “how”

Page 18: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

18

• In contrast, AsyncTask framework classes are more strongly connected• Complex framework details

hidden via Façade pattern• Yields a smaller “surface area”• Run concurrently, without directly

manipulating threads, handlers, messages, or runnables

AsyncTask

ExecutorFutu

reTa

sk

Loop

er Message

Message

Message

Message

Message

Message Queue

MessageUI Thread(main thread)

Overview of the AsyncTask Framework

See en.wikipedia.org/wiki/Template_Method_pattern

Handler

Page 19: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

19

• In contrast, AsyncTask framework classes are more strongly connected• Complex framework details

hidden via Façade pattern• Yields a smaller “surface area”• Run concurrently, without directly

manipulating threads, handlers, messages, or runnables

• Hook methods can perform pre-processing, post-processing, & communication between the background & UI threads

AsyncTaskHandler

3. execute(future)

2. onPreExecute()

4. doInBackGround()

FutureTask

Executor

5. onProgressUpdate()

6. onPostExecute()

1. execute(url)

Overview of the AsyncTask Framework

Page 20: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

20

AsyncTask

Executor

• Likewise, AsyncTask performance can be scaled up transparently

Futu

reTa

sk Handler

Loop

er Message

Message

Message

Message

Message

Message Queue

MessageUI Thread(main thread)

Overview of the AsyncTask Framework

Page 21: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

21

AsyncTask

Executor

• Likewise, AsyncTask performance can be scaled up transparently, e.g.• Contains a thread pool that can be

specified by programmers

Futu

reTa

sk Handler

Loop

er Message

Message

Message

Message

Message

Message Queue

MessageUI Thread(main thread)

Overview of the AsyncTask Framework

See en.wikipedia.org/wiki/Thread_pool

Page 22: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

22

AsyncTask

Executor

• Likewise, AsyncTask performance can be scaled up transparently, e.g.• Contains a thread pool that can be

specified by programmers• This thread pool can be implemented

via the Java Executor framework

Futu

reTa

sk Handler

Loop

er Message

Message

Message

Message

Message

Message Queue

MessageUI Thread(main thread)

Overview of the AsyncTask Framework

See docs.oracle.com/javase/tutorial/essential/concurrency/executors.html

Page 23: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

23

End of Overview of the AsyncTask Framework

(Part 1)

Page 24: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

Overview of the AsyncTask Framework (Part 2)

Douglas C. [email protected]

www.dre.vanderbilt.edu/~schmidt

Institute for Software Integrated Systems

Vanderbilt University Nashville, Tennessee, USA

Page 25: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

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

AsyncTaskHandler

3. execute(future)

2. onPreExecute()

4. doInBackGround()

FutureTask

Executor

UI Thread(main thread)

Loop

er Message

Message

Message

Message

Message

Message Queue

Message

5. onProgressUpdate()

6. onPostExecute()

1. execute(url)

Page 26: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

3

Categories of Methods in AsyncTask

Page 27: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

4

Categories of Methods in the AsyncTask Class• The AsyncTask class has two

types of methods

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

Page 28: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

5

• The AsyncTask class has two types of methods• Public methods

• Invoked by apps

AsyncTask<Params, Progress, Result> execute(Params... params)

• Executes the task with the specified parameters

AsyncTask<Params, Progress, Result> executeOnExecutor(Executor exec,

Params... params)

• Executes the task with the specified parameters on the specified Executor

static void execute(Runnable runnable)

• Convenience version of execute(Object) for use with a simple Runnable object

boolean cancel(boolean mayInterruptIfRunning)

• Attempts to cancel execution of this task...

Categories of Methods in the AsyncTask Class

Page 29: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

6

• The AsyncTask class has two types of methods• Public methods

• Invoked by apps

AsyncTask<Params, Progress, Result> execute(Params... params)

• Executes the task with the specified parameters

AsyncTask<Params, Progress, Result> executeOnExecutor(Executor exec,

Params... params)

• Executes the task with the specified parameters on the specified Executor

static void execute(Runnable runnable)

• Convenience version of execute(Object) for use with a simple Runnable object

boolean cancel(boolean mayInterruptIfRunning)

• Attempts to cancel execution of this task...

Categories of Methods in the AsyncTask Class

execute() runs each AsyncTask one-at-a-time (serially) in a background thread within a process

Page 30: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

7

• The AsyncTask class has two types of methods• Public methods

• Invoked by apps

AsyncTask<Params, Progress, Result> execute(Params... params)

• Executes the task with the specified parameters

AsyncTask<Params, Progress, Result> executeOnExecutor(Executor exec,

Params... params)

• Executes the task with the specified parameters on the specified Executor

static void execute(Runnable runnable)

• Convenience version of execute(Object) for use with a simple Runnable object

boolean cancel(boolean mayInterruptIfRunning)

• Attempts to cancel execution of this task...

Categories of Methods in the AsyncTask Class

executeOnExecutor() can run multiple AsyncTasksconcurrently in a pool of threads within a process

Page 31: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

8

• The AsyncTask class has two types of methods• Public methods

• Invoked by apps

AsyncTask<Params, Progress, Result> execute(Params... params)

• Executes the task with the specified parameters

AsyncTask<Params, Progress, Result> executeOnExecutor(Executor exec,

Params... params)

• Executes the task with the specified parameters on the specified Executor

static void execute(Runnable runnable)

• Convenience version of execute(Object) for use with a simple Runnable object

boolean cancel(boolean mayInterruptIfRunning)

• Attempts to cancel execution of this task...

Categories of Methods in the AsyncTask Class

This method is simply a front-end to the underlying Executor

Page 32: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

9

• The AsyncTask class has two types of methods• Public methods

• Invoked by apps

AsyncTask<Params, Progress, Result> execute(Params... params)

• Executes the task with the specified parameters

AsyncTask<Params, Progress, Result> executeOnExecutor(Executor exec,

Params... params)

• Executes the task with the specified parameters on the specified Executor

static void execute(Runnable runnable)

• Convenience version of execute(Object) for use with a simple Runnable object

boolean cancel(boolean mayInterruptIfRunning)

• Attempts to cancel execution of this task...

Categories of Methods in the AsyncTask Class

cancel() requires cooperation by the AsyncTask, i.e., it’s voluntary

Page 33: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

10

• The AsyncTask class has two types of methods• Public methods• Protected hook methods

• Overridden by apps &invoked by the AsyncTask framework

void onPreExecute()• Runs on UI thread before doInBackground()

abstract Result doInBackground(Params... params)

• Override this method to perform a computation in a background thread

void onProgressUpdate(Progress... values)

• Runs on UI thread after publishProgress() called

void onPostExecute(Result result)• Runs on UI thread after doInBackground()

void onCancelled(Result result)• Runs on UI thread after cancel() is invoked &

doInBackground() has finished...

Categories of Methods in the AsyncTask Class

Page 34: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

11

• The AsyncTask class has two types of methods• Public methods• Protected hook methods

• Overridden by apps &invoked by the AsyncTask framework

Categories of Methods in the AsyncTask Class

AsyncTaskHandler

3. execute(future)

2. onPreExecute()

4. doInBackGround()

FutureTask

Executor

5. onProgressUpdate()

6. onPostExecute()

1. execute(url)

UI Thread(main thread)

The AsyncTask framework invokes these methods at

different points of time & in different thread contexts

Page 35: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

12

• The AsyncTask class has two types of methods• Public methods• Protected hook methods

• Overridden by apps &invoked by the AsyncTask framework

void onPreExecute()• Runs on UI thread before doInBackground()

abstract Result doInBackground(Params... params)

• Override this method to perform a computation in a background thread

void onProgressUpdate(Progress... values)

• Runs on UI thread after publishProgress() called

void onPostExecute(Result result)• Runs on UI thread after doInBackground()

void onCancelled(Result result)• Runs on UI thread after cancel() is invoked &

doInBackground() has finished...

Categories of Methods in the AsyncTask Class

Called in UI thread after execute() called, i.e., prior to any other processing

Page 36: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

13

• The AsyncTask class has two types of methods• Public methods• Protected hook methods

• Overridden by apps &invoked by the AsyncTask framework

void onPreExecute()• Runs on UI thread before doInBackground()

abstract Result doInBackground(Params... params)

• Override this method to perform a computation in a background thread

void onProgressUpdate(Progress... values)

• Runs on UI thread after publishProgress() called

void onPostExecute(Result result)• Runs on UI thread after doInBackground()

void onCancelled(Result result)• Runs on UI thread after cancel() is invoked &

doInBackground() has finished...

Categories of Methods in the AsyncTask Class

Runs in the background thread to perform the computation

Page 37: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

14Called in UI thread to convey incremental results sent from a background thread

• The AsyncTask class has two types of methods• Public methods• Protected hook methods

• Overridden by apps &invoked by the AsyncTask framework

void onPreExecute()• Runs on UI thread before doInBackground()

abstract Result doInBackground(Params... params)

• Override this method to perform a computation in a background thread

void onProgressUpdate(Progress... values)

• Runs on UI thread after publishProgress() called

void onPostExecute(Result result)• Runs on UI thread after doInBackground()

void onCancelled(Result result)• Runs on UI thread after cancel() is invoked &

doInBackground() has finished...

Categories of Methods in the AsyncTask Class

Page 38: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

15Called in UI thread after all background processing is finished successfully

• The AsyncTask class has two types of methods• Public methods• Protected hook methods

• Overridden by apps &invoked by the AsyncTask framework

void onPreExecute()• Runs on UI thread before doInBackground()

abstract Result doInBackground(Params... params)

• Override this method to perform a computation in a background thread

void onProgressUpdate(Progress... values)

• Runs on UI thread after publishProgress() called

void onPostExecute(Result result)• Runs on UI thread after doInBackground()

void onCancelled(Result result)• Runs on UI thread after cancel() is invoked &

doInBackground() has finished...

Categories of Methods in the AsyncTask Class

Page 39: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

16Called in UI thread after background processing has been cancelled

• The AsyncTask class has two types of methods• Public methods• Protected hook methods

• Overridden by apps &invoked by the AsyncTask framework

void onPreExecute()• Runs on UI thread before doInBackground()

abstract Result doInBackground(Params... params)

• Override this method to perform a computation in a background thread

void onProgressUpdate(Progress... values)

• Runs on UI thread after publishProgress() called

void onPostExecute(Result result)• Runs on UI thread after doInBackground()

void onCancelled(Result result)• Runs on UI thread after cancel() is invoked &

doInBackground() has finished...

Categories of Methods in the AsyncTask Class

Page 40: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

17

Overriding Hook Methods in the AsyncTask Class

Page 41: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

18

• AsyncTask must be extended& one or more of its hookmethods overridden

Overriding Hook Methods in the AsyncTask Class

ImageDownloadTaskonPreExecute()doInBackground()onProgressUpdate()onPostExecute()onCancelled()

AsyncTaskexecuteOnExecutor()execute()cancel()onPreExecute()doInBackground()onProgressUpdate()onPostExecute()onCancelled()

Page 42: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

19

• AsyncTask must be extended& one or more of its hookmethods overridden

Overriding Hook Methods in the AsyncTask Class

ImageDownloadTaskonPreExecute()doInBackground()onProgressUpdate()onPostExecute()onCancelled()

AsyncTaskexecuteOnExecutor()execute()cancel()onPreExecute()doInBackground()onProgressUpdate()onPostExecute()onCancelled()

The doInBackground() method must be overridden

Page 43: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

20

ImageDownloadTaskonPreExecute()doInBackground()onProgressUpdate()onPostExecute()onCancelled()

AsyncTaskexecuteOnExecutor()execute()cancel()onPreExecute()doInBackground()onProgressUpdate()onPostExecute()onCancelled()

• AsyncTask must be extended& one or more of its hookmethods overridden

Can only be called once per instance of AsyncTaskby code in the UI thread

Overriding Hook Methods in the AsyncTask Class

Page 44: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

21

ImageDownloadTaskonPreExecute()doInBackground()onProgressUpdate()onPostExecute()onCancelled()

AsyncTaskexecuteOnExecutor()execute()cancel()onPreExecute()doInBackground()onProgressUpdate()onPostExecute()onCancelled()

• AsyncTask must be extended& one or more of its hookmethods overridden

See en.wikipedia.org/wiki/Template_method_pattern

Implemented as a variant of the Template

Method pattern

Overriding Hook Methods in the AsyncTask Class

Page 45: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

22

ImageDownloadTaskonPreExecute()doInBackground()onProgressUpdate()onPostExecute()onCancelled()

AsyncTaskexecuteOnExecutor()execute()cancel()onPreExecute()doInBackground()onProgressUpdate()onPostExecute()onCancelled()

• AsyncTask must be extended& one or more of its hookmethods overridden

Is passed an Executor used to run multiple AsyncTask

objects concurrently

Overriding Hook Methods in the AsyncTask Class

Page 46: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

23

• AsyncTask must be extended& one or more of its hookmethods overridden

Overriding Hook Methods in the AsyncTask Class

ImageDownloadTaskonPreExecute()doInBackground()onProgressUpdate()onPostExecute()onCancelled()

AsyncTaskexecuteOnExecutor()execute()cancel()onPreExecute()doInBackground()onProgressUpdate()onPostExecute()onCancelled()

Invoked by framework in the UI thread to perform

initialization actions

Page 47: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

24

• AsyncTask must be extended& one or more of its hookmethods overridden

See www.androiddesignpatterns.com/2014/01/thread-scheduling-in-android.html

Overriding Hook Methods in the AsyncTask Class

ImageDownloadTaskonPreExecute()doInBackground()onProgressUpdate()onPostExecute()onCancelled()

AsyncTaskexecuteOnExecutor()execute()cancel()onPreExecute()doInBackground()onProgressUpdate()onPostExecute()onCancelled()

Invoked by framework in a to perform long duration operations at

the “background” thread priority

Page 48: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

25

• AsyncTask must be extended& one or more of its hookmethods overridden

Overriding Hook Methods in the AsyncTask Class

ImageDownloadTaskonPreExecute()doInBackground()onProgressUpdate()onPostExecute()onCancelled()

AsyncTaskexecuteOnExecutor()execute()cancel()onPreExecute()doInBackground()onProgressUpdate()onPostExecute()onCancelled()

Invoked by framework in UI thread when background

thread calls publishProgress()

Page 49: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

26

ImageDownloadTaskonPreExecute()doInBackground()onProgressUpdate()onPostExecute()onCancelled()

AsyncTaskexecuteOnExecutor()execute()cancel()onPreExecute()doInBackground()onProgressUpdate()onPostExecute()onCancelled()

• AsyncTask must be extended& one or more of its hookmethods overridden

Invoked by framework in UI thread when doInBackground() returns its result

Overriding Hook Methods in the AsyncTask Class

Page 50: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

27

ImageDownloadTaskonPreExecute()doInBackground()onProgressUpdate()onPostExecute()onCancelled()

AsyncTaskexecuteOnExecutor()execute()cancel()onPreExecute()doInBackground()onProgressUpdate()onPostExecute()onCancelled()

• AsyncTask must be extended& one or more of its hookmethods overridden

Called by application to attempt to stop the execution of the task

Overriding Hook Methods in the AsyncTask Class

Page 51: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

28

ImageDownloadTaskonPreExecute()doInBackground()onProgressUpdate()onPostExecute()onCancelled()

AsyncTaskexecuteOnExecutor()execute()cancel()onPreExecute()doInBackground()onProgressUpdate()onPostExecute()onCancelled()

• AsyncTask must be extended& one or more of its hookmethods overridden

Invoked by framework in UI Thread after cancel() is called &

doInBackground() is finished

If onCancelled() is called then onPostExecute() is not called & vice versa

Overriding Hook Methods in the AsyncTask Class

Page 52: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

29

ImageDownloadTaskonPreExecute()doInBackground()onProgressUpdate()onPostExecute()onCancelled()

AsyncTaskexecuteOnExecutor()execute()cancel()onPreExecute()doInBackground()onProgressUpdate()onPostExecute()onCancelled()

• AsyncTask must be extended& one or more of its hookmethods overridden

Can periodically call isCancelled() to check if it’s been cancelled

Similar to using the Java interrupt() method to voluntarily shutdown threads

Overriding Hook Methods in the AsyncTask Class

Page 53: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

30

ImageDownloadTaskonPreExecute()doInBackground()onProgressUpdate()onPostExecute()onCancelled()

AsyncTaskexecuteOnExecutor()execute()cancel()onPreExecute()doInBackground()onProgressUpdate()onPostExecute()onCancelled()

• AsyncTask is also parameterized with three types used by itshook methods

Params, Progress, Result

• Params – Type used in background work

• Progress – Type used when indicating progress

• Result – Type of result

Overriding Hook Methods in the AsyncTask Class

Page 54: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

31

ImageDownloadTaskonPreExecute()doInBackground()onProgressUpdate()onPostExecute()onCancelled()

AsyncTaskexecuteOnExecutor()execute()cancel()onPreExecute()doInBackground()onProgressUpdate()onPostExecute()onCancelled()

• AsyncTask is also parameterized with three types used by itshook methods

Params, Progress, Result

• Params – Type used in background work

• Progress – Type used when indicating progress

• Result – Type of result

Overriding Hook Methods in the AsyncTask Class

Page 55: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

32

ImageDownloadTaskonPreExecute()doInBackground()onProgressUpdate()onPostExecute()onCancelled()

AsyncTaskexecuteOnExecutor()execute()cancel()onPreExecute()doInBackground()onProgressUpdate()onPostExecute()onCancelled()

• AsyncTask is also parameterized with three types used by itshook methods

Params, Progress, Result

• Params – Type used in background work

• Progress – Type used when indicating progress

• Result – Type of result

Overriding Hook Methods in the AsyncTask Class

Page 56: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

33

ImageDownloadTaskonPreExecute()doInBackground()onProgressUpdate()onPostExecute()onCancelled()

AsyncTask• AsyncTask is also parameterized

with three types used by itshook methods

Params, Progress, Result

Overriding Hook Methods in the AsyncTask Class

executeOnExecutor()execute()cancel()onPreExecute()doInBackground()onProgressUpdate()onPostExecute()onCancelled()

• Params – Type used in background work

• Progress – Type used when indicating progress

• Result – Type of result

Page 57: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

34

• Apps must customize theAsyncTask class to meet their concurrency needs

class DownloadTask extends AsyncTask<Uri, Integer, Long> {

protected Long doInBackground(Uri... urls)

{ /* Download files */ }

protected void onProgressUpdate(Integer... progress)

{ setProgressPercent(progress[0]); }

protected void onPostExecute(Long result)

{ showDialog("Downloaded " + result + " bytes"); }

}

new DownloadTask().execute(downloadURL);

Overriding Hook Methods in the AsyncTask Class

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

Page 58: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

35

• Apps must customize theAsyncTask class to meet their concurrency needs

class DownloadTask extends AsyncTask<Uri, Integer, Long> {

protected Long doInBackground(Uri... urls)

{ /* Download files */ }

protected void onProgressUpdate(Integer... progress)

{ setProgressPercent(progress[0]); }

protected void onPostExecute(Long result)

{ showDialog("Downloaded " + result + " bytes"); }

}

new DownloadTask().execute(downloadURL);

Overriding Hook Methods in the AsyncTask Class

Extend AsyncTask & fill in generic parameters

Page 59: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

36

• Apps must customize theAsyncTask class to meet their concurrency needs

class DownloadTask extends AsyncTask<Uri, Integer, Long> {

protected Long doInBackground(Uri... urls)

{ /* Download files */ }

protected void onProgressUpdate(Integer... progress)

{ setProgressPercent(progress[0]); }

protected void onPostExecute(Long result)

{ showDialog("Downloaded " + result + " bytes"); }

}

new DownloadTask().execute(downloadURL);

Overriding Hook Methods in the AsyncTask Class

This template method initiates async processing

Page 60: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

37

• Apps must customize theAsyncTask class to meet their concurrency needs

class DownloadTask extends AsyncTask<Uri, Integer, Long> {

protected Long doInBackground(Uri... urls)

{ /* Download files */ }

protected void onProgressUpdate(Integer... progress)

{ setProgressPercent(progress[0]); }

protected void onPostExecute(Long result)

{ showDialog("Downloaded " + result + " bytes"); }

}

new DownloadTask().execute(downloadURL);

Overriding Hook Methods in the AsyncTask Class

These hook methods are dispatched by the Async

Task framework in several different thread contexts

Page 61: Overview of the AsyncTask Framework (Part 1)schmidt/cs891s/2018-PDFs/L1...22 AsyncTask Executor • Likewise, AsyncTask performance can be scaled up transparently, e.g. • Contains

38

End of Overview of the AsyncTask Framework

(Part 2)