Async is not as simple as we think
DEEP DIVE INTO
ANDROID ASYNCTASK
Amrit Sanjeev
Bangalore Android User Group
Agenda
•  About me
•  Blrdroid GDG
•  Some of my thoughts
•  Q&A
About me
•  Organizer, Bangalore Android User Group
( www.blrdroid.org )
•  Staff engineer for Mobile at Digital Insight (...
Bangalore Android User Group ( www.BlrDroid.org)
•  Largest open Android developer community in the India
•  Over 5100+ me...
How threading in android works
most general principles apply well here too
Processes and threads
•  Android system starts a new Linux process for the
application with a single thread of execution
•...
Processes
android:process attribute that can specify a process
in which that component should run
Android tries to shut pr...
Process ranking and priority
Threads – General guidelines 
Do not block
the UI thread
Do not access
the Android
UI toolkit from
outside the UI
thread
Threads types
•  Main
– important because it is in charge of dispatching
events to the appropriate user interface.
– also ...
Here’s where AsyncTask comes
Just the basics
What is an Asynctask
Android implements a single thread model
Abstract class provided to help UI Thread
properly
Perform l...
Steps
•  doInBackground: Code performing long running
operation goes in this method. 
•  onPostExecute: This method is cal...
Pros and cons
Pros
• easy to use
• android specific
• easy UI-
interaction
Cons
• Different
behavior in
different
versions...
Deep dive into Asynctask
its more complicated than it seems
Quick question
How many of you are confident implementing
concurrent code in java ??
Changes over time 
Before Donut
• All tasks execute serially.
• Before a task can execute, all the previous tasks have to ...
Standard coding pattern – Concurrent execution
public class ConcurrentAsyncTask { 
public static void execute(AsyncTask as...
Cancelling a task
A task can be cancelled at any time by invoking cancel().
Invoking this method will cause subsequent cal...
Internals of AsyncTask
Upcoming SlideShare
Loading in …5
×

Internals of AsyncTask

841 views
698 views

Published on

Amrit Sanjeev talks about how AsyncTask works internally and how you can tweak it to work it better or understand it better

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
841
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
31
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Internals of AsyncTask

  1. 1. Async is not as simple as we think DEEP DIVE INTO ANDROID ASYNCTASK Amrit Sanjeev Bangalore Android User Group
  2. 2. Agenda •  About me •  Blrdroid GDG •  Some of my thoughts •  Q&A
  3. 3. About me •  Organizer, Bangalore Android User Group ( www.blrdroid.org ) •  Staff engineer for Mobile at Digital Insight (formerly Intuit) •  Previous companies – Philips research, IBM. •  Active in the local developer community •  Mentor at 10,000 startups (www.10000startups.com) •  Part of Intel Influencer program
  4. 4. Bangalore Android User Group ( www.BlrDroid.org) •  Largest open Android developer community in the India •  Over 5100+ members •  4.5 years and completely free. •  53 meet-ups •  5 hackathons •  Blrdroid teach – College edition more than 2400+ students from over 35 colleges participated. •  Active participation in events like Droidcon, Global Android Developer hackathon, Google Bizdroid etc
  5. 5. How threading in android works most general principles apply well here too
  6. 6. Processes and threads •  Android system starts a new Linux process for the application with a single thread of execution •  All components of the same application run in the same process and thread (called the "main" thread) •  you can arrange for different components in your application to run in separate processes, and you can create additional threads for any process.
  7. 7. Processes android:process attribute that can specify a process in which that component should run Android tries to shut processes before activities when it wants memory. A process is started again for those components when there's again work for them to do. Use this when you want to isolate component execution from each other .
  8. 8. Process ranking and priority
  9. 9. Threads – General guidelines Do not block the UI thread Do not access the Android UI toolkit from outside the UI thread
  10. 10. Threads types •  Main – important because it is in charge of dispatching events to the appropriate user interface. – also the thread in which your application interacts with components from the Android UI toolkit •  Worker threads – operations to perform that are not instantaneous – code can get complicated
  11. 11. Here’s where AsyncTask comes Just the basics
  12. 12. What is an Asynctask Android implements a single thread model Abstract class provided to help UI Thread properly Perform long operation in background without locking the UI thread
  13. 13. Steps •  doInBackground: Code performing long running operation goes in this method. •  onPostExecute: This method is called after doInBackground method completes processing. Result from doInBackground is passed to this method. •  onPreExecute: This method is called before doInBackground method is called. •  onProgressUpdate: This method is invoked by calling publishProgress anytime from doInBackground call this method.
  14. 14. Pros and cons Pros • easy to use • android specific • easy UI- interaction Cons • Different behavior in different versions of android
  15. 15. Deep dive into Asynctask its more complicated than it seems
  16. 16. Quick question How many of you are confident implementing concurrent code in java ??
  17. 17. Changes over time Before Donut • All tasks execute serially. • Before a task can execute, all the previous tasks have to be finished • Bad throughput From Donut to GingerBread • Each task was executed on a separate thread • Concurrency issues Honeycomb and onwards • Execution was switched back to the sequential implementation • executeOnExecutor(Executor) was added if parallel execution was needed. • SERIAL_EXECUTOR & THREAD_POOL_EXECUTOR
  18. 18. Standard coding pattern – Concurrent execution public class ConcurrentAsyncTask { public static void execute(AsyncTask asyncTask) { if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB_MR1) { asyncTask.execute(); } else { asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } } }
  19. 19. Cancelling a task A task can be cancelled at any time by invoking cancel(). Invoking this method will cause subsequent calls to isCancelled() to return true. After invoking this method, onCancelled(), instead of onPostExecute() will be invoked after doInBackground() returns. To ensure that a task is cancelled as quickly as possible, you should always check the return value of isCancelled() periodically from doInBackground()

×