AsyncTask pt1
AsyncTask pt1
Douglas C. Schmidt
[email protected]
www.dre.vanderbilt.edu/~schmidt
Institute for Software
Integrated Systems
Vanderbilt University
Nashville, Tennessee, USA
4. doInBackGround()
Looper
FutureTask
Executor
Message
Message
Message
5. onProgressUpdate()
6. onPostExecute()
3. execute(future)
Message
Message
Message
UI Thread
2. onPreExecute()
Handler
AsyncTask
1. execute(url)
(main thread)
Overview of the
AsyncTask Framework
Background
Thread A
Message
Queue
Message
Handler
Looper
Handler
Message
Runnable
Message
Message
Message
Message
Message
UI Thread
(main thread)
Background
Thread B
Message
Queue
Looper
Handler
Message
Runnable
Message
Message
Message
Message
Message
UI Thread
(main thread)
Background
Thread B
Message
Queue
Looper
Handler
Message
Runnable
Message
Message
Message
Message
Message
UI Thread
(main thread)
Background
Thread B
Message
Queue
Looper
Handler
Message
Runnable
Message
Message
Message
Message
Message
UI Thread
(main thread)
Background
Thread B
Background
Thread A
Message
Queue
Message
Handler
Looper
Handler
Message
Runnable
Message
Message
Message
Message
Message
UI Thread
(main thread)
Background
Thread B
Background
Thread A
Message
Queue
Message
Handler
Looper
Handler
Message
Runnable
Message
Message
Background
Thread B
Message
Message
Message
UI Thread
(main thread)
Background
Thread A
Message
Queue
Message
Handler
Looper
Handler
Message
Runnable
Message
Message
Message
Message
Message
UI Thread
(main thread)
10
Background
Thread B
Message
Queue
Message
Handler
handleMessage()
Handler
Looper
Message
Runnable
Message
Message
Message
Message
Message
UI Thread
(main thread)
Background
Thread B
Message
Message
Message
Message
Message
Message
UI Thread
(main thread)
See en.wikipedia.org/wiki/
12
Template_Method_pattern
FutureTask
Looper
Message
Queue
Handler
Executor
AsyncTask
Message
Message
Message
Message
Message
Message
UI Thread
(main thread)
13
FutureTask
Looper
Message
Queue
Handler
Executor
AsyncTask
Message
Message
Message
Message
Message
Message
UI Thread
(main thread)
14
FutureTask
Looper
Message
Queue
Handler
Executor
AsyncTask
Message
Message
Message
Message
Message
Message
UI Thread
(main thread)
15
FutureTask
Looper
Message
Queue
Handler
Executor
AsyncTask
Message
Message
Message
Message
Message
Message
UI Thread
(main thread)
16
FutureTask
Looper
Message
Queue
Handler
Executor
AsyncTask
See en.wikipedia.org/
17
wiki/Facade_pattern
4. doInBackGround()
FutureTask
Executor
5. onProgressUpdate()
6. onPostExecute()
3. execute(future)
Handler
2. onPreExecute()
AsyncTask
1. execute(url)
18
Categories of Methods
in AsyncTask
19
See developer.android.com/
20
reference/android/os/AsyncTask.html
void onPreExecute()
Runs on UI thread before doInBackground()
abstract Result doInBackground
(Params... params)
Override this method to perform a
computation on a background thread
void onPostExecute(Result result)
Runs on UI thread after doInBackground()
void onProgressUpdate(Progress...
values)
Runs on UI thread after publishProgress()
called
void onCancelled()
Runs on UI thread after cancel() is invoked &
doInBackground() has finished
...
26
void onPreExecute()
Runs on UI thread before doInBackground()
abstract Result doInBackground
(Params... params)
Override this method to perform a
computation on a background thread
void onPostExecute(Result result)
Runs on UI thread after doInBackground()
void onProgressUpdate(Progress...
values)
Runs on UI thread after publishProgress()
called
void onCancelled()
Runs on UI thread after cancel() is invoked &
doInBackground() has finished
...
27
void onPreExecute()
Runs on UI thread before doInBackground()
abstract Result doInBackground
(Params... params)
Override this method to perform a
computation on a background thread
void onPostExecute(Result result)
Runs on UI thread after doInBackground()
void onProgressUpdate(Progress...
values)
Runs on UI thread after publishProgress()
called
void onCancelled(Result result)
Runs on UI thread after cancel() is invoked &
doInBackground() has finished
...
28
AsyncTask
execute()
cancel()
onPreExecute()
doInBackground()
onProgressUpdate()
onPostExecute()
onCancelled()
ImageDownloadTask
onPreExecute()
doInBackground()
onProgressUpdate()
onPostExecute()
onCancelled()
29
AsyncTask
execute()
cancel()
onPreExecute()
doInBackground()
onProgressUpdate()
onPostExecute()
onCancelled()
ImageDownloadTask
onPreExecute()
doInBackground()
onProgressUpdate()
onPostExecute()
onCancelled()
30
AsyncTask
execute()
cancel()
onPreExecute()
doInBackground()
onProgressUpdate()
onPostExecute()
onCancelled()
Implemented as a
variant of the Template
Method pattern
ImageDownloadTask
onPreExecute()
doInBackground()
onProgressUpdate()
onPostExecute()
onCancelled()
See en.wikipedia.org/wiki/
31
Template_method_pattern
AsyncTask
execute()
cancel()
onPreExecute()
doInBackground()
onProgressUpdate()
onPostExecute()
onCancelled()
ImageDownloadTask
onPreExecute()
doInBackground()
onProgressUpdate()
onPostExecute()
onCancelled()
Invoked by framework in
the UI Thread to perform
initialization actions
32
AsyncTask
execute()
cancel()
onPreExecute()
doInBackground()
onProgressUpdate()
onPostExecute()
onCancelled()
ImageDownloadTask
onPreExecute()
doInBackground()
onProgressUpdate()
onPostExecute()
onCancelled()
Invoked by framework in a
background Thread to perform
long duration operations
See www.androiddesignpatterns.com/
33
2014/01/thread-scheduling-in-android.html
AsyncTask
execute()
cancel()
onPreExecute()
doInBackground()
onProgressUpdate()
onPostExecute()
onCancelled()
ImageDownloadTask
onPreExecute()
doInBackground()
onProgressUpdate()
onPostExecute()
onCancelled()
Invoked by framework in UI
Thread when background
Thread calls publishProgress()
34
AsyncTask
execute()
cancel()
onPreExecute()
doInBackground()
onProgressUpdate()
onPostExecute()
onCancelled()
ImageDownloadTask
onPreExecute()
doInBackground()
onProgressUpdate()
onPostExecute()
onCancelled()
35
AsyncTask
execute()
cancel()
onPreExecute()
doInBackground()
onProgressUpdate()
onPostExecute()
onCancelled()
ImageDownloadTask
onPreExecute()
doInBackground()
onProgressUpdate()
onPostExecute()
onCancelled()
36
Called by application
to attempt to stop the
execution of the task
AsyncTask
execute()
cancel()
onPreExecute()
doInBackground()
onProgressUpdate()
onPostExecute()
onCancelled()
ImageDownloadTask
onPreExecute()
doInBackground()
onProgressUpdate()
onPostExecute()
onCancelled()
Invoked by framework in UI
Thread after cancel() is called &
doInBackground() is finished
AsyncTask
execute()
cancel()
onPreExecute()
doInBackground()
onProgressUpdate()
onPostExecute()
onCancelled()
ImageDownloadTask
onPreExecute()
doInBackground()
onProgressUpdate()
onPostExecute()
onCancelled()
AsyncTask
execute()
cancel()
onPreExecute()
doInBackground()
onProgressUpdate()
onPostExecute()
P aram s Type used in
onCancelled()
background work
P rogress Type used
when indicating progress
R esult Type of result
ImageDownloadTask
onPreExecute()
doInBackground()
onProgressUpdate()
onPostExecute()
onCancelled()
39
AsyncTask
execute()
cancel()
onPreExecute()
doInBackground()
onProgressUpdate()
onPostExecute()
P aram s Type used in
onCancelled()
background work
P rogress Type used
when indicating progress
R esult Type of result
ImageDownloadTask
onPreExecute()
doInBackground()
onProgressUpdate()
onPostExecute()
onCancelled()
40
AsyncTask
execute()
cancel()
onPreExecute()
doInBackground()
onProgressUpdate()
onPostExecute()
P aram s Type used in
onCancelled()
background work
P rogress Type used
when indicating progress
R esult Type of result
ImageDownloadTask
onPreExecute()
doInBackground()
onProgressUpdate()
onPostExecute()
onCancelled()
41
AsyncTask
execute()
cancel()
onPreExecute()
doInBackground()
onProgressUpdate()
onPostExecute()
P aram s Type used in
onCancelled()
background work
P rogress Type used
when indicating progress
R esult Type of result
ImageDownloadTask
onPreExecute()
doInBackground()
onProgressUpdate()
onPostExecute()
onCancelled()
42
43
44
45
See developer.android.com/
46
reference/android/os/AsyncTask.html
Programming
with AsyncTask
47
See github.com/douglascraigschmidt/POSA48
15/tree/master/ex/SimpleImageDownloads
49
50
51
See ThreadedDownloads/src/edu/vuum/
52
mocca/ImageDownloadsActivity.java
53
Message
Message
Message
Message
Message
Message
UI Thread
(main thread)
54
FutureTask
Looper
execute()
Handler
Executor
AsyncTask
Message
Message
Message
Message
Message
Message
UI Thread
(main thread)
55
FutureTask
Looper
execute()
Handler
Executor
AsyncTask
Message
Message
Message
Message
Message
Message
UI Thread
(main thread)
56
FutureTask
Looper
execute()
Handler
Executor
AsyncTask
Message
Message
Message
Message
Message
Message
UI Thread
(main thread)
57
FutureTask
Looper
execute()
Handler
Executor
AsyncTask
Message
Message
Message
Message
Message
Message
UI Thread
(main thread)
58
FutureTask
Looper
execute()
Handler
Executor
AsyncTask
Background
Thread A
Handler
Looper
Message
Message
Runnable
Message
Handler
Message
Message
Message
Message
UI Thread
(main thread)
59
Background
Thread B
Message
Message
Message
Message
Message
Message
UI Thread
(main thread)
See en.wikipedia.org/wiki/
60
Template_method_pattern
FutureTask
Looper
execute()
Handler
Executor
AsyncTask
Message
Message
Message
Message
Message
Message
UI Thread
(main thread)
61
FutureTask
Looper
execute()
Handler
Executor
AsyncTask
AsyncTask
Usage
Considerations
62
Message
Queue
FutureTask
Looper
Executor
Message
Message
5. onProgressUpdate()
6. onPostExecute()
Message
3. execute(future)
Message
Message
Handler
2. onPreExecute()
AsyncTask
Message
UI Thread
(main thread)
63
1. execute(url)
Message
Queue
FutureTask
Looper
Executor
Message
Message
5. onProgressUpdate()
6. onPostExecute()
Message
3. execute(future)
Message
Message
Handler
2. onPreExecute()
AsyncTask
Message
UI Thread
(main thread)
64
1. execute(url)
Message
Queue
FutureTask
Looper
Executor
Message
Message
5. onProgressUpdate()
6. onPostExecute()
Message
3. execute(future)
Message
Message
Handler
2. onPreExecute()
AsyncTask
Message
UI Thread
(main thread)
65
1. execute(url)
Message
Queue
FutureTask
Looper
Executor
Message
Message
5. onProgressUpdate()
6. onPostExecute()
Message
3. execute(future)
Message
Message
Handler
2. onPreExecute()
AsyncTask
Message
UI Thread
(main thread)
66
1. execute(url)
Message
Queue
FutureTask
Looper
Executor
Message
Message
5. onProgressUpdate()
6. onPostExecute()
Message
3. execute(future)
Message
Message
Handler
2. onPreExecute()
AsyncTask
Message
UI Thread
(main thread)
67
1. execute(url)
Message
Queue
FutureTask
Looper
Executor
Message
Message
5. onProgressUpdate()
6. onPostExecute()
Message
3. execute(future)
Message
Message
Handler
2. onPreExecute()
AsyncTask
Message
UI Thread
(main thread)
68
1. execute(url)
Message
Queue
FutureTask
Looper
Executor
Message
Message
5. onProgressUpdate()
6. onPostExecute()
Message
3. execute(future)
Message
Message
2. onPreExecute()
Handler
AsyncTask
Message
UI Thread
1. execute(url)
(main thread)
Message
Queue
FutureTask
Looper
Executor
Message
Message
5. onProgressUpdate()
6. onPostExecute()
Message
3. execute(future)
Message
Message
Handler
2. onPreExecute()
AsyncTask
Message
UI Thread
(main thread)
70
1. execute(url)
Message
Queue
FutureTask
Looper
Executor
Message
Message
5. onProgressUpdate()
6. onPostExecute()
Message
3. execute(future)
Message
Message
Handler
2. onPreExecute()
AsyncTask
Message
UI Thread
(main thread)
71
1. execute(url)
AsyncTask
Threads to communicate
It embodies key framework
characteristics, e.g.
Inversion of control
Domain-specific
structure & functionality
Semi-complete portions
of apps
execute()
cancel()
onPreExecute()
doInBackground()
onProgressUpdate()
onPostExecute()
onCancelled()
ImageDownloadTask
onPreExecute()
doInBackground()
onProgressUpdate()
onPostExecute()
onCancelled()
72
AsyncTask
Threads to communicate
It embodies key framework
characteristics, e.g.
Inversion of control
Domain-specific
structure & functionality
Semi-complete portions
of apps
execute()
cancel()
onPreExecute()
doInBackground()
onProgressUpdate()
onPostExecute()
onCancelled()
ImageDownloadTask
onPreExecute()
doInBackground()
onProgressUpdate()
onPostExecute()
onCancelled()
73
AsyncTask
Threads to communicate
It embodies key framework
characteristics, e.g.
Inversion of control
Domain-specific
structure & functionality
Semi-complete portions
of apps
execute()
cancel()
onPreExecute()
doInBackground()
onProgressUpdate()
onPostExecute()
onCancelled()
ImageDownloadTask
onPreExecute()
doInBackground()
onProgressUpdate()
onPostExecute()
onCancelled()
74
Threads to communicate
It embodies key framework
characteristics
AsyncTask has traps & pitfalls
See bon-app-etit.blogspot.com/2013/
75
04/the-dark-side-of-asynctask.html
Threads to communicate
It embodies key framework
characteristics
AsyncTask has traps & pitfalls
Cancellation
Cancellation is voluntary, just
like Thread.interrupt()
Do
Disturb
76
Threads to communicate
It embodies key framework
characteristics
AsyncTask has traps & pitfalls
Cancellation
Dependency on Activity
Memory leaks occur if theres a strong
references to enclosing Activity
77
Threads to communicate
It embodies key framework
characteristics
AsyncTask has traps & pitfalls
Cancellation
Dependency on Activity
Losing results if/when runtime
configurations change
e.g., Activity associated with an
AsyncTask may be destroyed
78
Threads to communicate
It embodies key framework
characteristics
AsyncTask has traps & pitfalls
Cancellation
Dependency on Activity
Losing results if/when runtime
configurations change
Portability
Concurrency semantics of
AsyncTask execute() have
changed over time
79
Threads to communicate
It embodies key framework
characteristics
AsyncTask has traps & pitfalls
Cancellation
Dependency on Activity
Losing results if/when runtime
configurations change
Portability
Threads to communicate
It embodies key framework
characteristics
AsyncTask has traps & pitfalls
Cancellation
Dependency on Activity
Losing results if/when runtime
configurations change
Portability
run()
executeOnExecutor()
2.offer()
runnable
runnable
WorkerThreads
runnable
1.executeOnExecutor()
Thread
(main thread)
runnable
3.take()
4.run()
WorkQueue
runnable
ThreadPoolExecutor
AsyncTask
Threads to communicate
It embodies key framework
characteristics
AsyncTask has traps & pitfalls
AsyncTask used throughout Android
frameworks/base/core/java/android/content/AsyncTaskLoader.java
frameworks/base/core/java/android/content/CursorLoader.java
frameworks/base/core/java/android/os/AsyncTask.java
packages/apps/Browser/src/com/android/browser/UrlHandler.java
packages/apps/Calendar/src/com/android/calendar/CalendarController.java
packages/apps/Gallery/src/com/android/camera/ReverseGeocoderTask.java
packages/apps/Nfc/src/com/android/nfc/NfcService.java
packages/apps/Mms/src/com/android/mms/transaction/PushReceiver.java
packages/apps/Phone/src/com/android/phone/CallLogAsync.java
packages/apps/VideoEditor/src/com/android/videoeditor/BaseAdapterWithImages.java
...
82
Threads to communicate
It embodies key framework
characteristics
AsyncTask has traps & pitfalls
AsyncTask used throughout Android
onProgressUpdate() is not widely used
frameworks/base/media/java/android/media/videoeditor/MediaArtistNativeHelper.java
frameworks/base/packages/SystemUI/src/com/android/systemui/recent/
RecentTasksLoader.java
packages/apps/Email/emailcommon/src/com/android/emailcommon/utility/
EmailAsyncTask.java
packages/apps/Email/src/com/android/email/activity/setup/
AccountCheckSettingsFragment.java
packages/apps/Gallery2/src/com/android/gallery3d/app/ManageCachePage.java
packages/apps/Gallery2/src/com/android/gallery3d/ui/ImportCompleteListener.java
packages/apps/Gallery2/src/com/android/gallery3d/ui/MenuExecutor.java
packages/apps/Settings/src/com/android/settings/TrustedCredentialsSettings.java
83