SlideShare une entreprise Scribd logo
1  sur  20
Télécharger pour lire hors ligne
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()
Internals of AsyncTask

Contenu connexe

Tendances

Android App Development - 07 Threading
Android App Development - 07 ThreadingAndroid App Development - 07 Threading
Android App Development - 07 ThreadingDiego Grancini
 
10 ways to improve your Android app performance
10 ways to improve your Android app performance10 ways to improve your Android app performance
10 ways to improve your Android app performanceBoris Farber
 
Angular 2 Migration - JHipster Meetup 6
Angular 2 Migration - JHipster Meetup 6Angular 2 Migration - JHipster Meetup 6
Angular 2 Migration - JHipster Meetup 6William Marques
 
Lecture #2 threading, networking &amp; permissions final version #2
Lecture #2  threading, networking &amp; permissions final version #2Lecture #2  threading, networking &amp; permissions final version #2
Lecture #2 threading, networking &amp; permissions final version #2Vitali Pekelis
 
Web Performance & Latest in React
Web Performance & Latest in ReactWeb Performance & Latest in React
Web Performance & Latest in ReactTalentica Software
 
Hello, React Hooks!
Hello, React Hooks!Hello, React Hooks!
Hello, React Hooks!Heejong Ahn
 
Working With Concurrency In Java 8
Working With Concurrency In Java 8Working With Concurrency In Java 8
Working With Concurrency In Java 8Heartin Jacob
 
The evolution of Angular 2 @ AngularJS Munich Meetup #5
The evolution of Angular 2 @ AngularJS Munich Meetup #5The evolution of Angular 2 @ AngularJS Munich Meetup #5
The evolution of Angular 2 @ AngularJS Munich Meetup #5Johannes Weber
 
Android - Preventing common memory leaks
Android - Preventing common memory leaksAndroid - Preventing common memory leaks
Android - Preventing common memory leaksAli Muzaffar
 
Angular 2... so can I use it now??
Angular 2... so can I use it now??Angular 2... so can I use it now??
Angular 2... so can I use it now??Laurent Duveau
 
Angular1x and Angular 2 for Beginners
Angular1x and Angular 2 for BeginnersAngular1x and Angular 2 for Beginners
Angular1x and Angular 2 for BeginnersOswald Campesato
 
Multithreading in Android
Multithreading in AndroidMultithreading in Android
Multithreading in Androidcoolmirza143
 
Concurrency Utilities in Java 8
Concurrency Utilities in Java 8Concurrency Utilities in Java 8
Concurrency Utilities in Java 8Martin Toshev
 
Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooksMaulik Shah
 
Angular 2: What's New?
Angular 2: What's New?Angular 2: What's New?
Angular 2: What's New?jbandi
 
Building Universal Applications with Angular 2
Building Universal Applications with Angular 2Building Universal Applications with Angular 2
Building Universal Applications with Angular 2Minko Gechev
 
Understanding Framework Architecture using Eclipse
Understanding Framework Architecture using EclipseUnderstanding Framework Architecture using Eclipse
Understanding Framework Architecture using Eclipseanshunjain
 

Tendances (20)

Android App Development - 07 Threading
Android App Development - 07 ThreadingAndroid App Development - 07 Threading
Android App Development - 07 Threading
 
10 ways to improve your Android app performance
10 ways to improve your Android app performance10 ways to improve your Android app performance
10 ways to improve your Android app performance
 
Angular 2 Migration - JHipster Meetup 6
Angular 2 Migration - JHipster Meetup 6Angular 2 Migration - JHipster Meetup 6
Angular 2 Migration - JHipster Meetup 6
 
JAVA Threads explained
JAVA Threads explained JAVA Threads explained
JAVA Threads explained
 
Lecture #2 threading, networking &amp; permissions final version #2
Lecture #2  threading, networking &amp; permissions final version #2Lecture #2  threading, networking &amp; permissions final version #2
Lecture #2 threading, networking &amp; permissions final version #2
 
Node
NodeNode
Node
 
Web Performance & Latest in React
Web Performance & Latest in ReactWeb Performance & Latest in React
Web Performance & Latest in React
 
Hello, React Hooks!
Hello, React Hooks!Hello, React Hooks!
Hello, React Hooks!
 
Working With Concurrency In Java 8
Working With Concurrency In Java 8Working With Concurrency In Java 8
Working With Concurrency In Java 8
 
The evolution of Angular 2 @ AngularJS Munich Meetup #5
The evolution of Angular 2 @ AngularJS Munich Meetup #5The evolution of Angular 2 @ AngularJS Munich Meetup #5
The evolution of Angular 2 @ AngularJS Munich Meetup #5
 
Android - Preventing common memory leaks
Android - Preventing common memory leaksAndroid - Preventing common memory leaks
Android - Preventing common memory leaks
 
Angular 2... so can I use it now??
Angular 2... so can I use it now??Angular 2... so can I use it now??
Angular 2... so can I use it now??
 
Angular1x and Angular 2 for Beginners
Angular1x and Angular 2 for BeginnersAngular1x and Angular 2 for Beginners
Angular1x and Angular 2 for Beginners
 
Multithreading in Android
Multithreading in AndroidMultithreading in Android
Multithreading in Android
 
Concurrency Utilities in Java 8
Concurrency Utilities in Java 8Concurrency Utilities in Java 8
Concurrency Utilities in Java 8
 
Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooks
 
React hooks
React hooksReact hooks
React hooks
 
Angular 2: What's New?
Angular 2: What's New?Angular 2: What's New?
Angular 2: What's New?
 
Building Universal Applications with Angular 2
Building Universal Applications with Angular 2Building Universal Applications with Angular 2
Building Universal Applications with Angular 2
 
Understanding Framework Architecture using Eclipse
Understanding Framework Architecture using EclipseUnderstanding Framework Architecture using Eclipse
Understanding Framework Architecture using Eclipse
 

Similaire à Internals of AsyncTask

mobile development with androiddfdgdfhdgfdhf.pptx
mobile development with androiddfdgdfhdgfdhf.pptxmobile development with androiddfdgdfhdgfdhf.pptx
mobile development with androiddfdgdfhdgfdhf.pptxNgLQun
 
Eca online-seminar-session-1.pptx
Eca online-seminar-session-1.pptxEca online-seminar-session-1.pptx
Eca online-seminar-session-1.pptxGoran Djonovic
 
Android workshop
Android workshopAndroid workshop
Android workshopSagar Patel
 
What is cool with Domino V10, Proton and Node.JS, and why would I use it in ...
What is cool with Domino V10, Proton and Node.JS, and why would I use it in ...What is cool with Domino V10, Proton and Node.JS, and why would I use it in ...
What is cool with Domino V10, Proton and Node.JS, and why would I use it in ...Heiko Voigt
 
Android Application Development
Android Application DevelopmentAndroid Application Development
Android Application DevelopmentAzfar Siddiqui
 
Testing on Android
Testing on AndroidTesting on Android
Testing on AndroidAri Lacenski
 
Orientation pdf_merged.pdf
Orientation pdf_merged.pdfOrientation pdf_merged.pdf
Orientation pdf_merged.pdfDSCIITPatna
 
l1-reactnativeintroduction-160816150540.pdf
l1-reactnativeintroduction-160816150540.pdfl1-reactnativeintroduction-160816150540.pdf
l1-reactnativeintroduction-160816150540.pdfHương Trà Pé Xjnk
 
Building Large Mobile Apps
Building Large Mobile AppsBuilding Large Mobile Apps
Building Large Mobile AppsAdam Magaña
 
From Containerization to Modularity
From Containerization to ModularityFrom Containerization to Modularity
From Containerization to Modularityoasisfeng
 
Griffon for the Enterprise
Griffon for the EnterpriseGriffon for the Enterprise
Griffon for the EnterpriseJames Williams
 
AngularJS One Day Workshop
AngularJS One Day WorkshopAngularJS One Day Workshop
AngularJS One Day WorkshopShyam Seshadri
 
Engineering and Industrial Mobile Application (APP) Development
Engineering and Industrial Mobile Application (APP) DevelopmentEngineering and Industrial Mobile Application (APP) Development
Engineering and Industrial Mobile Application (APP) DevelopmentLiving Online
 

Similaire à Internals of AsyncTask (20)

Android development first steps
Android development   first stepsAndroid development   first steps
Android development first steps
 
Intro to android (gdays)
Intro to android (gdays)Intro to android (gdays)
Intro to android (gdays)
 
mobile development with androiddfdgdfhdgfdhf.pptx
mobile development with androiddfdgdfhdgfdhf.pptxmobile development with androiddfdgdfhdgfdhf.pptx
mobile development with androiddfdgdfhdgfdhf.pptx
 
Eca online-seminar-session-1.pptx
Eca online-seminar-session-1.pptxEca online-seminar-session-1.pptx
Eca online-seminar-session-1.pptx
 
Android workshop
Android workshopAndroid workshop
Android workshop
 
What is cool with Domino V10, Proton and Node.JS, and why would I use it in ...
What is cool with Domino V10, Proton and Node.JS, and why would I use it in ...What is cool with Domino V10, Proton and Node.JS, and why would I use it in ...
What is cool with Domino V10, Proton and Node.JS, and why would I use it in ...
 
Android Application Development
Android Application DevelopmentAndroid Application Development
Android Application Development
 
Android app devolopment
Android app devolopmentAndroid app devolopment
Android app devolopment
 
Testing on Android
Testing on AndroidTesting on Android
Testing on Android
 
Android class provider in mumbai
Android class provider in mumbaiAndroid class provider in mumbai
Android class provider in mumbai
 
Orientation pdf_merged.pdf
Orientation pdf_merged.pdfOrientation pdf_merged.pdf
Orientation pdf_merged.pdf
 
SE Unit-1.pptx
SE Unit-1.pptxSE Unit-1.pptx
SE Unit-1.pptx
 
l1-reactnativeintroduction-160816150540.pdf
l1-reactnativeintroduction-160816150540.pdfl1-reactnativeintroduction-160816150540.pdf
l1-reactnativeintroduction-160816150540.pdf
 
Building Large Mobile Apps
Building Large Mobile AppsBuilding Large Mobile Apps
Building Large Mobile Apps
 
From Containerization to Modularity
From Containerization to ModularityFrom Containerization to Modularity
From Containerization to Modularity
 
Griffon for the Enterprise
Griffon for the EnterpriseGriffon for the Enterprise
Griffon for the Enterprise
 
AngularJS One Day Workshop
AngularJS One Day WorkshopAngularJS One Day Workshop
AngularJS One Day Workshop
 
Engineering and Industrial Mobile Application (APP) Development
Engineering and Industrial Mobile Application (APP) DevelopmentEngineering and Industrial Mobile Application (APP) Development
Engineering and Industrial Mobile Application (APP) Development
 
Android Thread
Android ThreadAndroid Thread
Android Thread
 
Explore Android Internals
Explore Android InternalsExplore Android Internals
Explore Android Internals
 

Plus de BlrDroid

Post I/O 2014 Meetup : Google I/O '14 recap- Amrit Sanjeev
Post I/O 2014 Meetup : Google I/O '14 recap- Amrit SanjeevPost I/O 2014 Meetup : Google I/O '14 recap- Amrit Sanjeev
Post I/O 2014 Meetup : Google I/O '14 recap- Amrit SanjeevBlrDroid
 
June 2014 - Android wear
June 2014 - Android wearJune 2014 - Android wear
June 2014 - Android wearBlrDroid
 
June 2014 - IPC in android
June 2014 - IPC in androidJune 2014 - IPC in android
June 2014 - IPC in androidBlrDroid
 
June 2014 - Building Rabbit MQ based chat on Android
June 2014 - Building Rabbit MQ based chat on AndroidJune 2014 - Building Rabbit MQ based chat on Android
June 2014 - Building Rabbit MQ based chat on AndroidBlrDroid
 
Challenges in writing roboelectric tests
Challenges in writing roboelectric tests Challenges in writing roboelectric tests
Challenges in writing roboelectric tests BlrDroid
 
How to leverage cloud for QA process
How to leverage cloud for QA processHow to leverage cloud for QA process
How to leverage cloud for QA processBlrDroid
 
Usability Testing Made Easy
Usability Testing Made EasyUsability Testing Made Easy
Usability Testing Made EasyBlrDroid
 
How Mobile Developers Could Leverage On Big Data and Data Points to understan...
How Mobile Developers Could Leverage On Big Data and Data Points to understan...How Mobile Developers Could Leverage On Big Data and Data Points to understan...
How Mobile Developers Could Leverage On Big Data and Data Points to understan...BlrDroid
 
Increasing downloads, ratings and revenues
Increasing downloads, ratings and revenues Increasing downloads, ratings and revenues
Increasing downloads, ratings and revenues BlrDroid
 
March 2014 Meetup - Nokia X Tech Session
March 2014 Meetup - Nokia X Tech SessionMarch 2014 Meetup - Nokia X Tech Session
March 2014 Meetup - Nokia X Tech SessionBlrDroid
 
March 2014 Meetup Baug Android and Google App Engine
March 2014 Meetup Baug Android and Google App EngineMarch 2014 Meetup Baug Android and Google App Engine
March 2014 Meetup Baug Android and Google App EngineBlrDroid
 
Android Security - Common Security Pitfalls in Android Applications
Android Security - Common Security Pitfalls in Android ApplicationsAndroid Security - Common Security Pitfalls in Android Applications
Android Security - Common Security Pitfalls in Android ApplicationsBlrDroid
 
High performance graphics and computation - OpenGL ES and RenderScript
High performance graphics and computation - OpenGL ES and RenderScript High performance graphics and computation - OpenGL ES and RenderScript
High performance graphics and computation - OpenGL ES and RenderScript BlrDroid
 
Dexetra Labs - Building Apps that can get featured
Dexetra Labs - Building Apps that can get featuredDexetra Labs - Building Apps that can get featured
Dexetra Labs - Building Apps that can get featuredBlrDroid
 
July 2013 Meetup : Introduction To App Publish - Ujjwal Kabra
July 2013 Meetup : Introduction To App Publish - Ujjwal KabraJuly 2013 Meetup : Introduction To App Publish - Ujjwal Kabra
July 2013 Meetup : Introduction To App Publish - Ujjwal KabraBlrDroid
 
July2013 Meetup : App Store Optimization - Shankar soma
July2013 Meetup : App Store Optimization - Shankar somaJuly2013 Meetup : App Store Optimization - Shankar soma
July2013 Meetup : App Store Optimization - Shankar somaBlrDroid
 
June2013 Meetup : Activity Recognition API - Walkmeter - Michal Depa
June2013 Meetup : Activity Recognition API - Walkmeter - Michal DepaJune2013 Meetup : Activity Recognition API - Walkmeter - Michal Depa
June2013 Meetup : Activity Recognition API - Walkmeter - Michal DepaBlrDroid
 
June2013 Meetup : In-App Billing by Soham & Senthil
June2013 Meetup : In-App Billing by Soham & SenthilJune2013 Meetup : In-App Billing by Soham & Senthil
June2013 Meetup : In-App Billing by Soham & SenthilBlrDroid
 
June2013 Meetup : IO13 Deep Dive-Location_api_AmritSanjeev
June2013 Meetup : IO13 Deep Dive-Location_api_AmritSanjeev June2013 Meetup : IO13 Deep Dive-Location_api_AmritSanjeev
June2013 Meetup : IO13 Deep Dive-Location_api_AmritSanjeev BlrDroid
 

Plus de BlrDroid (20)

Post I/O 2014 Meetup : Google I/O '14 recap- Amrit Sanjeev
Post I/O 2014 Meetup : Google I/O '14 recap- Amrit SanjeevPost I/O 2014 Meetup : Google I/O '14 recap- Amrit Sanjeev
Post I/O 2014 Meetup : Google I/O '14 recap- Amrit Sanjeev
 
June 2014 - Android wear
June 2014 - Android wearJune 2014 - Android wear
June 2014 - Android wear
 
June 2014 - IPC in android
June 2014 - IPC in androidJune 2014 - IPC in android
June 2014 - IPC in android
 
June 2014 - Building Rabbit MQ based chat on Android
June 2014 - Building Rabbit MQ based chat on AndroidJune 2014 - Building Rabbit MQ based chat on Android
June 2014 - Building Rabbit MQ based chat on Android
 
Challenges in writing roboelectric tests
Challenges in writing roboelectric tests Challenges in writing roboelectric tests
Challenges in writing roboelectric tests
 
How to leverage cloud for QA process
How to leverage cloud for QA processHow to leverage cloud for QA process
How to leverage cloud for QA process
 
Usability Testing Made Easy
Usability Testing Made EasyUsability Testing Made Easy
Usability Testing Made Easy
 
How Mobile Developers Could Leverage On Big Data and Data Points to understan...
How Mobile Developers Could Leverage On Big Data and Data Points to understan...How Mobile Developers Could Leverage On Big Data and Data Points to understan...
How Mobile Developers Could Leverage On Big Data and Data Points to understan...
 
Increasing downloads, ratings and revenues
Increasing downloads, ratings and revenues Increasing downloads, ratings and revenues
Increasing downloads, ratings and revenues
 
March 2014 Meetup - Nokia X Tech Session
March 2014 Meetup - Nokia X Tech SessionMarch 2014 Meetup - Nokia X Tech Session
March 2014 Meetup - Nokia X Tech Session
 
March 2014 Meetup Baug Android and Google App Engine
March 2014 Meetup Baug Android and Google App EngineMarch 2014 Meetup Baug Android and Google App Engine
March 2014 Meetup Baug Android and Google App Engine
 
Android Security - Common Security Pitfalls in Android Applications
Android Security - Common Security Pitfalls in Android ApplicationsAndroid Security - Common Security Pitfalls in Android Applications
Android Security - Common Security Pitfalls in Android Applications
 
High performance graphics and computation - OpenGL ES and RenderScript
High performance graphics and computation - OpenGL ES and RenderScript High performance graphics and computation - OpenGL ES and RenderScript
High performance graphics and computation - OpenGL ES and RenderScript
 
Dexetra Labs - Building Apps that can get featured
Dexetra Labs - Building Apps that can get featuredDexetra Labs - Building Apps that can get featured
Dexetra Labs - Building Apps that can get featured
 
July 2013 Meetup : Introduction To App Publish - Ujjwal Kabra
July 2013 Meetup : Introduction To App Publish - Ujjwal KabraJuly 2013 Meetup : Introduction To App Publish - Ujjwal Kabra
July 2013 Meetup : Introduction To App Publish - Ujjwal Kabra
 
July2013 Meetup : App Store Optimization - Shankar soma
July2013 Meetup : App Store Optimization - Shankar somaJuly2013 Meetup : App Store Optimization - Shankar soma
July2013 Meetup : App Store Optimization - Shankar soma
 
June2013 Meetup : Activity Recognition API - Walkmeter - Michal Depa
June2013 Meetup : Activity Recognition API - Walkmeter - Michal DepaJune2013 Meetup : Activity Recognition API - Walkmeter - Michal Depa
June2013 Meetup : Activity Recognition API - Walkmeter - Michal Depa
 
June2013 Meetup : In-App Billing by Soham & Senthil
June2013 Meetup : In-App Billing by Soham & SenthilJune2013 Meetup : In-App Billing by Soham & Senthil
June2013 Meetup : In-App Billing by Soham & Senthil
 
June2013 Meetup : IO13 Deep Dive-Location_api_AmritSanjeev
June2013 Meetup : IO13 Deep Dive-Location_api_AmritSanjeev June2013 Meetup : IO13 Deep Dive-Location_api_AmritSanjeev
June2013 Meetup : IO13 Deep Dive-Location_api_AmritSanjeev
 
IO13 Recap
IO13 RecapIO13 Recap
IO13 Recap
 

Dernier

Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DaySri Ambati
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Scott Andery
 

Dernier (20)

Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
 

Internals of AsyncTask

  • 1. Async is not as simple as we think DEEP DIVE INTO ANDROID ASYNCTASK Amrit Sanjeev Bangalore Android User Group
  • 2. Agenda •  About me •  Blrdroid GDG •  Some of my thoughts •  Q&A
  • 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. 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. How threading in android works most general principles apply well here too
  • 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. 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 .
  • 9. Threads – General guidelines Do not block the UI thread Do not access the Android UI toolkit from outside the UI thread
  • 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. Here’s where AsyncTask comes Just the basics
  • 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. 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. Pros and cons Pros • easy to use • android specific • easy UI- interaction Cons • Different behavior in different versions of android
  • 15. Deep dive into Asynctask its more complicated than it seems
  • 16. Quick question How many of you are confident implementing concurrent code in java ??
  • 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. 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. 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()