SlideShare a Scribd company logo
2
Most read
8
Most read
9
Most read
Android
Thread, Handler and AsyncTask
                        Hoang Ngo
Thread
   • Act much like usual Java Threads

   • Can’t act directly on external User
     Interface objects (throw the Exception
     CalledFromWrongThreadException: Only
     the original thread that created a view
     hierarchy can touch its views”)
   • Can’t be stopped by executing destroy()
     nor stop(). Use instead interrupt() or join()
     (by case)
Thread
   • Two main ways of having a Thread
     execute application code:
     – Providing a new class that extends Thread and
       overriding its run() method.
     – Providing a new Thread instance with a
       Runnable object during its creation. In both
       cases, the start() method must be called to
       actually execute the new Thread.
Thread example
Handler

   • Associated with a single thread and
     that thread's message queue
   • Bound to the thread / message
     queue of the thread that is creating it
   • Deliver messages and runnables to
     that message queue
   • Execute them as they come out of
     the message queue
Handler

   • Two main uses for a Handler:
     – To schedule messages and
       runnables to be executed as some
       point in the future
     – To add an action into a queue
       performed on a different thread
Handler example
AsyncTask
    • Created on the UI thread and can be executed
      only once

    • Run on a background thread and result is
      published on the UI thread

    • The three types used by an asynchronous task
      are the following
       – Params, the type of the parameters sent to the task
         upon execution
       – Progress, the type of the progress units published during
         the background computation
       – Result, the type of the result of the background
         computation

    • Extend AsyncTask<Void, Void, Void>
AsyncTask
   • Go through 4 steps:
     – onPreExecute(): invoked on the UI thread
       immediately after the task is executed
     – doInBackground(Param ...): invoked on the
       background thread immediately after
       onPreExecute() finishes executing
     – onProgressUpdate(Progress...): invoked on the
       UI thread after a call to
       publishProgress(Progress...)
     – onPostExecute(Result): invoked on the UI
       thread after the background computation
       finishes
AsyncTask example
•   Violate the single thread model: the Android UI toolkit is not
    thread-safe and must always be manipulated on the UI thread.
•   In this piece of code, the ImageView is manipulated on a
    worker thread, which can cause really weird problems. Tracking
    down and fixing such bugs can be difficult and time-consuming
•   Classes and methods also tend to make the code more
    complicated and more difficult to read.
•   It becomes even worse when our implement complex
    operations that require frequent UI updates
Android - Thread, Handler and AsyncTask
Android - Thread, Handler and AsyncTask

More Related Content

PPTX
Introduction to Android and Android Studio
PPTX
Android async task
PDF
Multithreading in Android
PDF
Android Programming Basics
PPTX
Android UI
PDF
Android fundamentals and tutorial for beginners
PDF
Anshul chechani android app development report
PPTX
Android User Interface
Introduction to Android and Android Studio
Android async task
Multithreading in Android
Android Programming Basics
Android UI
Android fundamentals and tutorial for beginners
Anshul chechani android app development report
Android User Interface

What's hot (20)

PPT
SQLITE Android
PPT
Mobile Application Development With Android
PDF
Linux Network Management
PPTX
Multithreading in java
PDF
Android resources
PPT
Mobile Application Development MAD J2ME
PPTX
Android activity lifecycle
PPTX
JAVA Platform Independence
PPTX
PPTX
Android Project Presentation
DOC
Client Server Architecture
PDF
Embedded Android : System Development - Part II (HAL)
PDF
Android Components & Manifest
ZIP
Android Application Development
PPTX
Android app development ppt
PPT
Introduction to JavaScript
PPT
CROSS PLATFORM APPLICATIONS DEVELOPMENT
PDF
Android media framework overview
PPTX
Introduction to Android development - Presentation
PPTX
Android AIDL Concept
SQLITE Android
Mobile Application Development With Android
Linux Network Management
Multithreading in java
Android resources
Mobile Application Development MAD J2ME
Android activity lifecycle
JAVA Platform Independence
Android Project Presentation
Client Server Architecture
Embedded Android : System Development - Part II (HAL)
Android Components & Manifest
Android Application Development
Android app development ppt
Introduction to JavaScript
CROSS PLATFORM APPLICATIONS DEVELOPMENT
Android media framework overview
Introduction to Android development - Presentation
Android AIDL Concept
Ad

Similar to Android - Thread, Handler and AsyncTask (20)

PPT
Tech talk
PDF
Android development training programme , Day 3
PDF
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
PDF
[Android] Multiple Background Threads
PDF
Android Threading
PPTX
Android session-5-sajib
PPTX
Introduction to Android - Session 3
ODP
Android App Development - 07 Threading
PPTX
Lecture #2 threading, networking &amp; permissions final version #2
PDF
Programming Sideways: Asynchronous Techniques for Android
PDF
Asynchronous Programming in Android
PPTX
Thread_Handlers_and_Executors_Presentation.pptx
PPTX
Async task, threads, pools, and executors oh my!
PPTX
Advanced #3 threading
PDF
Session 9 Android Web Services - Part 2.pdf
PPTX
Performance #6 threading
PDF
Internals of AsyncTask
PDF
Think Async: Understanding the Complexity of Multithreading - Avi Kabizon & A...
PDF
Android Concurrency Presentation
PPTX
Advanced #2 threading
Tech talk
Android development training programme , Day 3
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
[Android] Multiple Background Threads
Android Threading
Android session-5-sajib
Introduction to Android - Session 3
Android App Development - 07 Threading
Lecture #2 threading, networking &amp; permissions final version #2
Programming Sideways: Asynchronous Techniques for Android
Asynchronous Programming in Android
Thread_Handlers_and_Executors_Presentation.pptx
Async task, threads, pools, and executors oh my!
Advanced #3 threading
Session 9 Android Web Services - Part 2.pdf
Performance #6 threading
Internals of AsyncTask
Think Async: Understanding the Complexity of Multithreading - Avi Kabizon & A...
Android Concurrency Presentation
Advanced #2 threading
Ad

Recently uploaded (20)

PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Big Data Technologies - Introduction.pptx
PPTX
sap open course for s4hana steps from ECC to s4
PPTX
Cloud computing and distributed systems.
PDF
Approach and Philosophy of On baking technology
PDF
Electronic commerce courselecture one. Pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
MIND Revenue Release Quarter 2 2025 Press Release
DOCX
The AUB Centre for AI in Media Proposal.docx
PPT
Teaching material agriculture food technology
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Empathic Computing: Creating Shared Understanding
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Encapsulation theory and applications.pdf
PDF
KodekX | Application Modernization Development
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Big Data Technologies - Introduction.pptx
sap open course for s4hana steps from ECC to s4
Cloud computing and distributed systems.
Approach and Philosophy of On baking technology
Electronic commerce courselecture one. Pdf
Spectral efficient network and resource selection model in 5G networks
MIND Revenue Release Quarter 2 2025 Press Release
The AUB Centre for AI in Media Proposal.docx
Teaching material agriculture food technology
Programs and apps: productivity, graphics, security and other tools
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Empathic Computing: Creating Shared Understanding
Unlocking AI with Model Context Protocol (MCP)
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Encapsulation theory and applications.pdf
KodekX | Application Modernization Development

Android - Thread, Handler and AsyncTask

  • 1. Android Thread, Handler and AsyncTask Hoang Ngo
  • 2. Thread • Act much like usual Java Threads • Can’t act directly on external User Interface objects (throw the Exception CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views”) • Can’t be stopped by executing destroy() nor stop(). Use instead interrupt() or join() (by case)
  • 3. Thread • Two main ways of having a Thread execute application code: – Providing a new class that extends Thread and overriding its run() method. – Providing a new Thread instance with a Runnable object during its creation. In both cases, the start() method must be called to actually execute the new Thread.
  • 5. Handler • Associated with a single thread and that thread's message queue • Bound to the thread / message queue of the thread that is creating it • Deliver messages and runnables to that message queue • Execute them as they come out of the message queue
  • 6. Handler • Two main uses for a Handler: – To schedule messages and runnables to be executed as some point in the future – To add an action into a queue performed on a different thread
  • 8. AsyncTask • Created on the UI thread and can be executed only once • Run on a background thread and result is published on the UI thread • The three types used by an asynchronous task are the following – Params, the type of the parameters sent to the task upon execution – Progress, the type of the progress units published during the background computation – Result, the type of the result of the background computation • Extend AsyncTask<Void, Void, Void>
  • 9. AsyncTask • Go through 4 steps: – onPreExecute(): invoked on the UI thread immediately after the task is executed – doInBackground(Param ...): invoked on the background thread immediately after onPreExecute() finishes executing – onProgressUpdate(Progress...): invoked on the UI thread after a call to publishProgress(Progress...) – onPostExecute(Result): invoked on the UI thread after the background computation finishes
  • 11. Violate the single thread model: the Android UI toolkit is not thread-safe and must always be manipulated on the UI thread. • In this piece of code, the ImageView is manipulated on a worker thread, which can cause really weird problems. Tracking down and fixing such bugs can be difficult and time-consuming
  • 12. Classes and methods also tend to make the code more complicated and more difficult to read. • It becomes even worse when our implement complex operations that require frequent UI updates