0% found this document useful (0 votes)
18 views16 pages

Asyn Task

The document provides an overview of asynchronous programming, emphasizing its benefits for improving efficiency and responsiveness in I/O-bound tasks. It introduces AsyncTask in Android for managing background operations and discusses its structure, advantages, and limitations. Additionally, it covers the use of Handler and Thread for background processing, detailing implementation steps and key concepts for managing concurrent execution.

Uploaded by

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

Asyn Task

The document provides an overview of asynchronous programming, emphasizing its benefits for improving efficiency and responsiveness in I/O-bound tasks. It introduces AsyncTask in Android for managing background operations and discusses its structure, advantages, and limitations. Additionally, it covers the use of Handler and Thread for background processing, detailing implementation steps and key concepts for managing concurrent execution.

Uploaded by

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

V2V EdTech LLP

ASYNCHRONOUS
PROGRAMMING

BY DARSHAN SIR
V2V EdTech LLP

Understanding Asynchronous Programming


Introduction to asynchronous programming concepts.

Asynchronous programming is a paradigm that allows a program to perform tasks


concurrently, improving efficiency and responsiveness.
This is particularly useful for I/O-bound and high-latency operations such as file access,
network requests, or database queries.
V2V EdTech LLP

Understanding Asynchronous Programming


Introduction to asynchronous programming concepts.

Synchronous Programming:

Tasks are executed sequentially, one after another.


Each task waits for the previous one to complete.
Can lead to blocking, where a slow task delays the execution of subsequent tasks.

Asynchronous Programming:

Tasks can be executed concurrently.


A task can be paused to wait for an external operation (like I/O) to complete, allowing
other tasks to run in the meantime.
Reduces blocking and can improve the efficiency and responsiveness of applications.
V2V EdTech LLP

AsyncTask
Introduction to AsyncTask for asynchronous operations on the UI thread.

AsyncTask was a class in Android designed to handle short-lived background operations and publish
results on the UI thread without having to manually manage threads.

Key Concepts of AsyncTask

1. Basic Structure

doInBackground(Params... params): Performs the background computation.


onPreExecute(): Runs on the UI thread before the background computation starts.
onPostExecute(Result result): Runs on the UI thread after the background computation
finishes.
onProgressUpdate(Progress... values): Runs on the UI thread when publishProgress is called
from doInBackground.
V2V EdTech LLP

AsyncTask
Introduction to AsyncTask for asynchronous operations on the UI thread.

2. Type Parameters
Params: The type of input parameters sent to doInBackground.
Progress: The type of progress units published during background computation.
Result: The type of result returned by doInBackground and passed to onPostExecute

3. Lifecycle Methods
onPreExecute(): Prepares the task, runs on the UI thread.
doInBackground(Params... params): Executes the background task, runs on a background thread.
onPostExecute(Result result): Handles the result of the background task, runs on the UI thread.
onProgressUpdate(Progress... values): Updates progress, runs on the UI thread when publishProgress
is called.
V2V EdTech LLP

AsyncTask
Introduction to AsyncTask for asynchronous operations on the UI thread.

EXAMPLE
V2V EdTech LLP

AsyncTask
Introduction to AsyncTask for asynchronous operations on the UI thread.

Advantages:
Simplicity: Provides a simple way to perform background operations and update the UI without
handling threads manually.
UI Thread: Automatically manages threading and posting results to the UI thread.

Limitations:
Memory Leaks: If not handled properly, it can cause memory leaks, especially if the AsyncTask
holds a reference to an Activity or Context.
Short-Lived Tasks: Suitable only for short-lived tasks; for long-running tasks, other mechanisms
like WorkManager or IntentService are preferred.
Lifecycle Awareness: Not lifecycle-aware, meaning it doesn't handle configuration changes like
screen rotations well.
V2V EdTech LLP

AsyncTask
Implementing AsyncTask for long-running tasks (e.g., network requests).

Step 1: Create a new AsyncTask Subclass


Create a new class that extends AsyncTask. In this example, we are fetching data from a network.

Step 2: Define Type Parameters


Params: The type of parameters sent to the task upon execution.
Progress: The type of progress units published during background computation.
Result: The type of the result of the background computation.

Step 3: Override the Necessary Methods


Override onPreExecute, doInBackground, onProgressUpdate, and onPostExecute methods to handle
the lifecycle of the task.

Step 4: Execute the AsyncTask


Execute the AsyncTask to start the operation.
V2V EdTech LLP

AsyncTask
Implementing AsyncTask for long-running tasks (e.g., network requests).

EXAMPLE
V2V EdTech LLP

Handlers and Threads


Using Handler and Thread for background processing.

Using Handler and Thread for background processing in Android is a common and flexible
way to manage background tasks and communicate results back to the UI thread.
This approach involves creating a separate Thread for the background work and using a
Handler to post results back to the main thread.

Key Components
Thread: Executes the background task.
Handler: Posts messages and runnables back to the main thread.
V2V EdTech LLP

Handlers and Threads


Using Handler and Thread for background processing.

Step-by-Step Implementation
1. Create a Handler in the Main Thread
2. Create and Start a Background Thread
3. Use the Handler to Communicate with the Main Thread

EXAMPLE
V2V EdTech LLP

Handlers and Threads


Posting and handling messages with Handler.

Using Handler to post and handle messages is a powerful way to manage background tasks and
communicate with the main thread in Android. A Handler allows you to send and process Message and
Runnable objects associated with a thread's MessageQueue

Key Components
Handler: For sending and processing Message and Runnable objects.
Message: A simple data structure for conveying information between threads.
Runnable: An interface for defining a block of code to be executed.
V2V EdTech LLP

Handlers and Threads


Posting and handling messages with Handler.

Step-by-Step Implementation
1. Create a Handler
2. Post Messages and Runnables to the Handler
3. Handle Messages and Runnables

EXAMPLE
V2V EdTech LLP

Handlers and Threads


Creating and managing threads for concurrent execution.

Creating and managing threads for concurrent execution in Android involves using the Thread
class or higher-level concurrency utilities like ExecutorService.

Key Concepts
Creating Threads: Using Thread or Runnable.
Managing Threads: Using ExecutorService for better thread management.
Communicating with the UI: Using Handler or runOnUiThread for UI updates.
V2V EdTech LLP

Handlers and Threads


Creating and managing threads for concurrent execution.

Creating and managing threads for concurrent execution in Android involves using the Thread
class or higher-level concurrency utilities like ExecutorService.

Key Concepts
Creating Threads: Using Thread or Runnable.
Managing Threads: Using ExecutorService for better thread management.
Communicating with the UI: Using Handler or runOnUiThread for UI updates.
V2V EdTech LLP

THANK YOU

You might also like