0% found this document useful (0 votes)
4 views28 pages

MobApp 20 - Threads in Android

The document discusses threading in mobile application development, particularly in Android, highlighting the importance of using different threads to prevent UI unresponsiveness. It categorizes threads into those attached to activities/fragments (like AsyncTask and Loaders) and those that are not (like Services), explaining their roles and functionalities. Additionally, it outlines five types of threads used in Android development, including the Main Thread, UI Thread, Worker Thread, Any Thread, and Binder Thread.

Uploaded by

arslanqadri958
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)
4 views28 pages

MobApp 20 - Threads in Android

The document discusses threading in mobile application development, particularly in Android, highlighting the importance of using different threads to prevent UI unresponsiveness. It categorizes threads into those attached to activities/fragments (like AsyncTask and Loaders) and those that are not (like Services), explaining their roles and functionalities. Additionally, it outlines five types of threads used in Android development, including the Main Thread, UI Thread, Worker Thread, Any Thread, and Binder Thread.

Uploaded by

arslanqadri958
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/ 28

CSI-615 Mobile and Application Development

Arfan Shahzad
{ [email protected] }
Mobile Application Development
Threading

• 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.

• The thread which is created by the runtime system is known as the


main thread.
Threading cont…

• The main thread’s primary role is to handle the UI in terms of event


handling and interaction with views in the UI.

• If there is a task that is time-consuming and that task is run on the


main thread, then it will stop other tasks until it gets completed,
which in turn may result in displaying a warning “Application is
unresponsive” to the user by the operating system.
Threading cont…

• So we need different threads for such tasks.

• As you can see the following image, showing threads handling


different tasks over main thread and background thread also:
Threading cont…
Threading cont…

• In android, all threading components belong to one of two basic


categories.

• 1- Threads that are attached to an activity/fragment: This category


of threads are bound to the lifecycle of the activity/fragment and
these are terminated as soon as the activity/fragment is destroyed.

• Thread components: AsyncTask, Loaders.


Threading cont…

• 2- Threads that aren’t attached to any activity/fragment: These


types of threads can continue to run beyond the lifetime of the
activity or fragment from which they were spawned.

• Threading Components: Service.


Threading cont…
Attached to an activity/fragment: ASYNCTASK
• AsyncTask is the most elementary Android component for threading.

• It’s super easy and simple to use it’s also good for some basic
scenarios:
Threading
cont…
Attached to
an activity/
fragment:
ASYNCTASK
Threading cont…
Attached to an activity/fragment: ASYNCTASK
• AsyncTask, however, falls short if you would like your deferred task to
run beyond the lifetime of the activity/fragment.

• The fact that even the slightest of screen rotation can cause the
activity to be destroyed is simply awful!

• So We Come to Loaders:
Threading cont…
Attached to an activity/fragment: LOADERS
• Loaders are the answer to the awful nightmare mentioned above.

• Loaders are great at performing in that context and they


automatically stop when the activity is destroyed, even more, the
sweet fact is that they also restart themselves after the activity is
recreated.
Threading cont…
Attached to an activity/fragment: LOADERS
Threading cont…
Not attached to an activity/fragment: SERVICE
• Service could be thought of as a component that’s useful for
performing long (or potentially long) operations with no UI.

• Service’s don’t have any UI.

• Service runs within the main thread of its hosting process; the service
doesn’t create its own thread and doesn’t run during a separate
process unless you specify otherwise.
Threading cont…
Not attached to an activity/fragment: SERVICE
Threading cont…
Not attached to an activity/fragment: SERVICE
• Service could be thought of as a component that’s useful for
performing long (or potentially long) operations with no UI.

• Service’s don’t have any UI.

• Service runs within the main thread of its hosting process; the service
doesn’t create its own thread and doesn’t run during a separate
process unless you specify otherwise.
Threading cont…
Not attached to an activity/fragment: SERVICE
• Service could be thought of as a component that’s useful for
performing long (or potentially long) operations with no UI.

• Service’s don’t have any UI.

• Service runs within the main thread of its hosting process; the service
doesn’t create its own thread and doesn’t run during a separate
process unless you specify otherwise.
Threading cont…

• There are five types of thread used in Android mobile development:

1. Main Thread

2. UI Thread

3. Worker Thread

4. Any Thread

5. Binder Thread
Threading cont…
Main Thread
• When we launch our app on Android, it creates the first thread of
execution called the “Main Thread”.

• The communication between the components from the Android UI


toolkit and the dispatching of events to their appropriate UI widgets is
handled by the main thread.
Threading cont…
Main Thread
• We should avoid network operations, database calls, and the loading
of certain components in the main thread.

• Because the main thread is called synchronously when executed, that


means the user interface will remain completely unresponsive until
the performance completes.
Threading cont…
Main Thread
Threading cont…
UI Thread
• Every app in Android has its own thread which is responsible for
running the UI objects, like view objects.

• Such a thread is known as the UI thread.

• The UI thread is the main thread of execution for our app as this is
where most of the app code is run.
Threading cont…
UI Thread
• The UI thread is where all of our app components (like activities,
services, content providers, and broadcast receivers) are created.

• This thread allows our tasks to perform their background work and
then move the results to UI elements such as bitmaps.

• All objects running on our UI thread will be able to access other


objects which are also running on the same UI thread.
Threading cont…
UI Thread
• The tasks that we run on a thread from a thread pool do not run on
our UI thread, so they will not have access to UI objects.

• The data moves from a background thread to the UI thread, using a


handler that runs on the UI thread.
Threading cont…
The Worker Thread
• The worker thread is a background thread.

• The worker threads are created separately, other than threads like the
UI thread.

• As we know from the rules, we cannot block a UI thread so this is


where the worker thread comes into play since we can use them to
run the child processes and tasks.
Threading cont…
Any Thread
• In Any thread, the annotated method can be called from any thread.

• If the annotated element is a class, then all methods in the class can
be called from Any Thread.
Threading cont…
Any Thread
Threading cont…
Binder Thread
• Binder thread represents a separate thread of service.

• The binder is a mechanism that provides inter-process communication.

• The binder thread is used in service binding with interprocess


communication.

• This concept is mainly related to service calls with interfaces defined by


Android Interface Definition Language (AIDL).

You might also like