0% found this document useful (0 votes)
5 views12 pages

Mobile Application Development 11

Uploaded by

afeefazia84
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views12 pages

Mobile Application Development 11

Uploaded by

afeefazia84
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

MOBILE

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.

Types of threads in Android:


 Main Thread (UI Thread): Responsible for handling UI events and
drawing the UI. It's crucial to keep it responsive to avoid app freezes.
 Worker Threads: Created by the app to handle long-running tasks or
background operations to keep the UI thread unblocked
UI THREAD (MAIN THREAD)
 The UI thread is responsible for handling the user interface, including drawing
to the screen, receiving user input, and dispatching touch events.
 When Android app is launched one thread is created.
 This thread is called Main Thread or UI Thread.
 Avoid doing time consuming tasks in UI Thread since it leads to app
that does not respond quickly
 Long-running tasks should not be executed directly on this thread to prevent
ANR (Application Not Responding) errors.
 User interaction , system callback & life cycle methods handled in the UI
thread.
WORKER THREADS
 Worker threads, also referred to as background threads or non-UI threads,
are used for executing time-consuming tasks without affecting the UI
responsiveness.
 By offloading intensive operations to worker threads, the UI thread remains
free to handle user interactions.
 There are several ways to create and manage worker threads in Android:
 AsyncTask: A deprecated class (as of API level 30) that simplifies performing
background operations and updating the UI. However, other approaches like
Executor or Thread are recommended for newer applications.
 Thread and Handler: Creating and managing threads manually using Java's
Thread class and coordinating with a Handler to post updates to the UI thread.
 Executor Framework: Utilizing classes like ThreadPoolExecutor, Executors, or
ExecutorService for managing thread pools and executing tasks concurrently.
MAIN TECHNIQUES FOR
BACKGROUND PROCESSING
Handler:
 Used to communicate between the UI thread and background threads.
 It allows you to post Runnable instances to execute on 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.

You might also like