Mobile Application Development 11
Mobile Application Development 11
APPLICATION
DEVELOPMENT
Explore Android's threading model, including the UI
thread and worker threads
THREAD
A thread is a fundamental unit of execution that can run independently
within an application.
Thread is one of the important concepts in Android.
Thread is a lightweight sub-process that provides us a way to do
background operations without interrupting the User Interface (UI).
When an app is launched, it creates a single thread in which all app
components will run by default.
ANDROID'S THREADING
MODEL
Android's threading model is crucial for building responsive and smooth-
running applications.
At its core, Android uses a single-threaded model for UI operations,
commonly known as the Main or UI thread.
All UI interactions, such as user input, screen updates, and handling touch
events, are processed on this thread.
However, performing long-running tasks or heavy operations on the UI
thread can cause the UI to freeze or become unresponsive, resulting in a
poor user experience.
To prevent this, Android provides mechanisms for handling background
tasks using worker threads or asynchronous operations.
RULES - TYPES
Here are two rules to Android's single-thread model:
Don't block the UI thread.
Don't access the Android UI toolkit from outside the UI thread.
Runnable:
An interface representing a task that can be executed.
Used with threads or handlers for background processing.
AsyncTask (deprecated):
Divides tasks into onPreExecute, doInBackground, onProgressUpdate, and
onPostExecute methods.
Though deprecated, it was widely used for simplifying background tasks.
WHY THREADS ARE
IMPORTANT
Improve responsiveness: Threads prevent UI freezing by offloading long-
running tasks to worker threads, ensuring a smoother user experience.
Better performance: Threads enable efficient use of multi-core
processors by allowing tasks to run concurrently.
Handle background tasks: Threads are essential for tasks like network
calls, database operations, file I/O, and complex calculations that should
run in the background without interrupting the UI.
ACCESSING THREADS
Identifying the Current Thread:
Use Thread.currentThread() to get a reference to the thread where the code is
currently executing.
Checking for the UI Thread:
Use Looper.getMainLooper().isCurrentThread() to determine if you're on the main
thread (UI thread).
CREATING THREADS
1. Using Threads and Runnables:
Create a Thread object, passing a Runnable implementation as its argument.
The Runnable's run() method contains the code to be executed on the new
thread.
Start the thread with thread.start().
2. Using Handlers
Create a Handler object, often associated with a specific thread's looper
(message queue).
Post tasks to the handler's queue using handler.post() or
handler.postDelayed().
The handler will execute them on its associated thread
CONT.
3. Using AsyncTask (deprecated in API level 30):
Simplified mechanism for short background operations with automatic UI
updates.
Extend the AsyncTask class and override its methods for background work and UI
updates.