SlideShare una empresa de Scribd logo
1 de 20
Descargar para leer sin conexión
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

Más contenido relacionado

La actualidad más candente

Concurrency Utilities in Java 8
Concurrency Utilities in Java 8Concurrency Utilities in Java 8
Concurrency Utilities in Java 8
Martin Toshev
 

La actualidad más candente (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
 

Similar a Internals of AsyncTask

mobile development with androiddfdgdfhdgfdhf.pptx
mobile development with androiddfdgdfhdgfdhf.pptxmobile development with androiddfdgdfhdgfdhf.pptx
mobile development with androiddfdgdfhdgfdhf.pptx
NgLQun
 
l1-reactnativeintroduction-160816150540.pdf
l1-reactnativeintroduction-160816150540.pdfl1-reactnativeintroduction-160816150540.pdf
l1-reactnativeintroduction-160816150540.pdf
Hương Trà Pé Xjnk
 

Similar a 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
 

Más de BlrDroid

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
BlrDroid
 
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
BlrDroid
 
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
BlrDroid
 
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
BlrDroid
 
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
 

Más 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
 

Último

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Último (20)

Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 

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