SlideShare ist ein Scribd-Unternehmen logo
1 von 21
Today’s Manifest
Concurrency
● Android’s thread model
● Using concurrency
o AsyncTasks
o Handlers
● Adverse effects
Android Thread Model
● Android uses a single thread to do all work
● That single thread, runs the user interface
o Thus, its called the UI Thread
● All code we write in Activity lifecycle events /
Broadcasts / Services are running in the UI
Thread
Android Thread model
● Doing extensive work on the UI thread will
slow down UI components
o e.g. When the thread does a heavy database query,
clicking the buttons cannot be done.!
● UI will become unresponsive
● Happens because UI thread can’t refresh
items while doing other work on itself.
Android Thread model
● When the UI becomes unresponsive,
Android takes action
● First is to show the “Application Not
Responding” dialog (ANR)
● Happens within 5 seconds of unresponsive
UI
Android Thread model
● If the user choses to wait
o Android tolerates for another 20s
● The message will continue to occur
● To prevent that, we need to use any one of
multiple concurrency methods in Android
Lets start coding
Stage 1
● Setup simple task in the form of a
Thread.sleep()
● Try getting an output from that task and
putting it to UI
● Try clicking UI items while at it.
o See the ANR?
Stage 2
● So the UI thread is the problem!
● Create another Thread. Java to the rescue.!
● Try setting the UI.
o Yes, its supposed to be that way. Don’t panic
Stage 3
● UI components can be updated in UI thread
● So use runOnUiThread()
● Problem solved. Or...is it?
● Try to get repetitive tasks to run with an
update.
o Code looks ugly? Indeed.
Stage 4
● Use AsyncTask for the same process
o Click
o Notify
o Update
o Finish
AsyncTask
● Executes in a different Thread than the UI
thread.
● Suitable for small, couple of second
operations
● All AsyncTasks run in one Thread at the OS
level.
AsyncTask
● Four segments
o Pre execute
o Do in background
o On update
o Post execute
● All others except doInBackground() are
run on the UI thread.
● doInBackground() is run in the parallel
AsyncTask thread.
Stage 4
● Done deal.? Not so fast
● AsyncTask cannot be used in every case.
● Long running executions should be done
using ThreadPoolExecutor and
FutureTask
o Better left for another session
● To make matters worse, AsyncTask has
some issues as well.
Issues with AsyncTask
● Run the same app, but this time, rotate the
screen while the task is going.!
● In order to prevent that, we need to either
o Lock screen orientation (at least while in task)
o Use Fragments
● Of course we’re going to use Fragments.
Who asked that question?
Stage 5
● Create a dummy (blank) Fragment with no
UI, but our AsyncTask
● Make sure to call setRetainInstance(true);
● Use a local variable to re-initiate the
progress dialogs if needed.
Handler
● A Handler can be used to receive messages
from other threads
● The handler resides in the UI thread and
updates the UI components accordingly
Stage 6
● Use the usual Activity with a new Thread
● Run the Thread and pass a message to a
Handler in the Activity
Confused?
you should be….!
is here
https://github.com/tdevinda/DialogAndroidIntro_3
Check branches asynctask and handler
(you will need git)
Final Code
Introduction to Android - Session 3

Weitere ähnliche Inhalte

Ähnlich wie Introduction to Android - Session 3

Background threads, async communication and vaadin
Background threads, async communication and vaadinBackground threads, async communication and vaadin
Background threads, async communication and vaadinPetter Holmström
 
Insights on Protractor testing
Insights on Protractor testingInsights on Protractor testing
Insights on Protractor testingDejan Toteff
 
Shake that-fud-vrs5
Shake that-fud-vrs5Shake that-fud-vrs5
Shake that-fud-vrs5wimjongman
 
Full stack development
Full stack developmentFull stack development
Full stack developmentArnav Gupta
 
Lecture #2 threading, networking & permissions final version #2
Lecture #2  threading, networking & permissions final version #2Lecture #2  threading, networking & permissions final version #2
Lecture #2 threading, networking & permissions final version #2Vitali Pekelis
 
[Android] Multiple Background Threads
[Android] Multiple Background Threads[Android] Multiple Background Threads
[Android] Multiple Background ThreadsNikmesoft Ltd
 
Threading model in windows store apps
Threading model in windows store appsThreading model in windows store apps
Threading model in windows store appsMirco Vanini
 
Android Connecting to internet Part 2
Android  Connecting to internet Part 2Android  Connecting to internet Part 2
Android Connecting to internet Part 2Paramvir Singh
 
Improving Cross Desktop Standard by Cedric BAIL (GNOME Asia Summit 2013)
Improving Cross Desktop Standard by Cedric BAIL (GNOME Asia Summit 2013)Improving Cross Desktop Standard by Cedric BAIL (GNOME Asia Summit 2013)
Improving Cross Desktop Standard by Cedric BAIL (GNOME Asia Summit 2013)Daniel Juyung Seo
 
TMPA-2017: Unity Application Testing Automation with Appium and Image Recogni...
TMPA-2017: Unity Application Testing Automation with Appium and Image Recogni...TMPA-2017: Unity Application Testing Automation with Appium and Image Recogni...
TMPA-2017: Unity Application Testing Automation with Appium and Image Recogni...Iosif Itkin
 
Campkde 2010: KDE Plasma netbook
Campkde 2010: KDE Plasma netbookCampkde 2010: KDE Plasma netbook
Campkde 2010: KDE Plasma netbookMarco Martin
 
Introduction to React Native
Introduction to React NativeIntroduction to React Native
Introduction to React NativeDamian Zbrożek
 
A sip of elixir
A sip of elixirA sip of elixir
A sip of elixirUttam Kini
 
Introduction to Continuous Delivery
Introduction to Continuous DeliveryIntroduction to Continuous Delivery
Introduction to Continuous DeliveryGiovanni Toraldo
 
An overview of node.js
An overview of node.jsAn overview of node.js
An overview of node.jsvaluebound
 
New generation Linux distributions that companion with Geeko : opencocon and...
New generation Linux distributions that companion with Geeko :  opencocon and...New generation Linux distributions that companion with Geeko :  opencocon and...
New generation Linux distributions that companion with Geeko : opencocon and...shimadah
 
All Aboard The Stateful Train
All Aboard The Stateful TrainAll Aboard The Stateful Train
All Aboard The Stateful TrainSmartLogic
 

Ähnlich wie Introduction to Android - Session 3 (20)

Background threads, async communication and vaadin
Background threads, async communication and vaadinBackground threads, async communication and vaadin
Background threads, async communication and vaadin
 
Insights on Protractor testing
Insights on Protractor testingInsights on Protractor testing
Insights on Protractor testing
 
Shake that-fud-vrs5
Shake that-fud-vrs5Shake that-fud-vrs5
Shake that-fud-vrs5
 
Full stack development
Full stack developmentFull stack development
Full stack development
 
Lecture #2 threading, networking & permissions final version #2
Lecture #2  threading, networking & permissions final version #2Lecture #2  threading, networking & permissions final version #2
Lecture #2 threading, networking & permissions final version #2
 
[Android] Multiple Background Threads
[Android] Multiple Background Threads[Android] Multiple Background Threads
[Android] Multiple Background Threads
 
Mobi Vision 2.0
Mobi Vision 2.0Mobi Vision 2.0
Mobi Vision 2.0
 
Threading model in windows store apps
Threading model in windows store appsThreading model in windows store apps
Threading model in windows store apps
 
Android Connecting to internet Part 2
Android  Connecting to internet Part 2Android  Connecting to internet Part 2
Android Connecting to internet Part 2
 
Improving Cross Desktop Standard by Cedric BAIL (GNOME Asia Summit 2013)
Improving Cross Desktop Standard by Cedric BAIL (GNOME Asia Summit 2013)Improving Cross Desktop Standard by Cedric BAIL (GNOME Asia Summit 2013)
Improving Cross Desktop Standard by Cedric BAIL (GNOME Asia Summit 2013)
 
TMPA-2017: Unity Application Testing Automation with Appium and Image Recogni...
TMPA-2017: Unity Application Testing Automation with Appium and Image Recogni...TMPA-2017: Unity Application Testing Automation with Appium and Image Recogni...
TMPA-2017: Unity Application Testing Automation with Appium and Image Recogni...
 
Campkde 2010: KDE Plasma netbook
Campkde 2010: KDE Plasma netbookCampkde 2010: KDE Plasma netbook
Campkde 2010: KDE Plasma netbook
 
Introduction to React Native
Introduction to React NativeIntroduction to React Native
Introduction to React Native
 
Node.js basics
Node.js basicsNode.js basics
Node.js basics
 
A sip of elixir
A sip of elixirA sip of elixir
A sip of elixir
 
Introduction to Continuous Delivery
Introduction to Continuous DeliveryIntroduction to Continuous Delivery
Introduction to Continuous Delivery
 
An overview of node.js
An overview of node.jsAn overview of node.js
An overview of node.js
 
New generation Linux distributions that companion with Geeko : opencocon and...
New generation Linux distributions that companion with Geeko :  opencocon and...New generation Linux distributions that companion with Geeko :  opencocon and...
New generation Linux distributions that companion with Geeko : opencocon and...
 
Background Thread
Background ThreadBackground Thread
Background Thread
 
All Aboard The Stateful Train
All Aboard The Stateful TrainAll Aboard The Stateful Train
All Aboard The Stateful Train
 

Kürzlich hochgeladen

Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfMarharyta Nedzelska
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfDrew Moseley
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentationvaddepallysandeep122
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsSafe Software
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Rob Geurden
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...OnePlan Solutions
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Cizo Technology Services
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...confluent
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfInnovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfYashikaSharma391629
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 

Kürzlich hochgeladen (20)

Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdf
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentation
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data Streams
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfInnovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 

Introduction to Android - Session 3

  • 1.
  • 2. Today’s Manifest Concurrency ● Android’s thread model ● Using concurrency o AsyncTasks o Handlers ● Adverse effects
  • 3. Android Thread Model ● Android uses a single thread to do all work ● That single thread, runs the user interface o Thus, its called the UI Thread ● All code we write in Activity lifecycle events / Broadcasts / Services are running in the UI Thread
  • 4. Android Thread model ● Doing extensive work on the UI thread will slow down UI components o e.g. When the thread does a heavy database query, clicking the buttons cannot be done.! ● UI will become unresponsive ● Happens because UI thread can’t refresh items while doing other work on itself.
  • 5. Android Thread model ● When the UI becomes unresponsive, Android takes action ● First is to show the “Application Not Responding” dialog (ANR) ● Happens within 5 seconds of unresponsive UI
  • 6. Android Thread model ● If the user choses to wait o Android tolerates for another 20s ● The message will continue to occur ● To prevent that, we need to use any one of multiple concurrency methods in Android
  • 8. Stage 1 ● Setup simple task in the form of a Thread.sleep() ● Try getting an output from that task and putting it to UI ● Try clicking UI items while at it. o See the ANR?
  • 9. Stage 2 ● So the UI thread is the problem! ● Create another Thread. Java to the rescue.! ● Try setting the UI. o Yes, its supposed to be that way. Don’t panic
  • 10. Stage 3 ● UI components can be updated in UI thread ● So use runOnUiThread() ● Problem solved. Or...is it? ● Try to get repetitive tasks to run with an update. o Code looks ugly? Indeed.
  • 11. Stage 4 ● Use AsyncTask for the same process o Click o Notify o Update o Finish
  • 12. AsyncTask ● Executes in a different Thread than the UI thread. ● Suitable for small, couple of second operations ● All AsyncTasks run in one Thread at the OS level.
  • 13. AsyncTask ● Four segments o Pre execute o Do in background o On update o Post execute ● All others except doInBackground() are run on the UI thread. ● doInBackground() is run in the parallel AsyncTask thread.
  • 14. Stage 4 ● Done deal.? Not so fast ● AsyncTask cannot be used in every case. ● Long running executions should be done using ThreadPoolExecutor and FutureTask o Better left for another session ● To make matters worse, AsyncTask has some issues as well.
  • 15. Issues with AsyncTask ● Run the same app, but this time, rotate the screen while the task is going.! ● In order to prevent that, we need to either o Lock screen orientation (at least while in task) o Use Fragments ● Of course we’re going to use Fragments. Who asked that question?
  • 16. Stage 5 ● Create a dummy (blank) Fragment with no UI, but our AsyncTask ● Make sure to call setRetainInstance(true); ● Use a local variable to re-initiate the progress dialogs if needed.
  • 17. Handler ● A Handler can be used to receive messages from other threads ● The handler resides in the UI thread and updates the UI components accordingly
  • 18. Stage 6 ● Use the usual Activity with a new Thread ● Run the Thread and pass a message to a Handler in the Activity
  • 20. is here https://github.com/tdevinda/DialogAndroidIntro_3 Check branches asynctask and handler (you will need git) Final Code