Download - Internals of AsyncTask
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 (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
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
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
• 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.
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 .
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 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
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 long operation in background without locking the UI thread
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.
Pros and cons
Pros
• easy to use
• android specific
• easy UI-interaction
Cons
• Different behavior in different versions of android
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 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
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); } } }
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()
�