internals of asynctask

20
Async is not as simple as we think DEEP DIVE INTO ANDROID ASYNCTASK Amrit Sanjeev Bangalore Android User Group

Upload: blrdroid

Post on 12-May-2015

393 views

Category:

Technology


0 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Internals of AsyncTask

Async is not as simple as we think

DEEP DIVE INTO �ANDROID ASYNCTASK

Amrit Sanjeev Bangalore Android User Group

Page 2: Internals of AsyncTask

Agenda

•  About me •  Blrdroid GDG •  Some of my thoughts •  Q&A

Page 3: Internals of AsyncTask

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

Page 4: Internals of AsyncTask

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

Page 5: Internals of AsyncTask

How threading in android works �most general principles apply well here too

Page 6: Internals of AsyncTask

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.

Page 7: Internals of AsyncTask

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 .

Page 8: Internals of AsyncTask

Process ranking and priority

Page 9: Internals of AsyncTask

Threads – General guidelines

Do not block the UI thread

Do not access the Android

UI toolkit from outside the UI

thread

Page 10: Internals of AsyncTask

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

Page 11: Internals of AsyncTask

Here’s where AsyncTask comes �Just the basics

Page 12: Internals of AsyncTask

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

Page 13: Internals of AsyncTask

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.

Page 14: Internals of AsyncTask

Pros and cons

Pros

• easy to use

• android specific

• easy UI-interaction

Cons

• Different behavior in different versions of android

Page 15: Internals of AsyncTask

Deep dive into Asynctask �its more complicated than it seems

Page 16: Internals of AsyncTask

Quick question

How many of you are confident implementing concurrent code in java ??

Page 17: Internals of AsyncTask

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

Page 18: Internals of AsyncTask

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); } } }

Page 19: Internals of AsyncTask

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()

Page 20: Internals of AsyncTask