Hi Guys!!!
Hope you are doing well !. After long time I am back. Sorry for late to add new blogs.
My previous post on Thread
Java Thread Sleep, wait, yield and join method
Today I am sharing details on threading support in Android using java.
Threading aspect is a important part of android and Its efficeint uses in app improve performance significantly.
Main thread
When an application, launched by user, Android creates a Linux process for that app and a thread of execution for it . This thread is called the main thread or UI thread of that application.
What is main thread of an Application in Android
The main thread is nothing but a handler thread.
Responsibility
• handling events from all over the app like callbacks associated with the lifecycle information or
• callbacks from input events or
• handling events from other apps etc
Why we need another thread ?
• The main thread does so much works (example Any block of code that needs to be run is pushed into a work queue and then serviced by the main thread).
• it’s better to offer longer work to other threads, so as not to disturb the UI thread from its rendering duties.
• If we do any operation that may end up keeping main thread blocked then main thread halt halt rendring job and that impact as a screen freeze .
• If screen get freeze more than 5 sec. Android system shows ANR dialog to user
To avoid this situation, We need a separate threads to do long operation without blocking the UI/main thread . That means these threads are executed asynchronously from the UI/main thread.
How many ways are in Android ?
Android provides many ways of creating and managing threads, and there are many third-party libraries that make thread management a lot easier.
However, picking the right one that suits our needs is very important.
Here We explore :-
• Thread
• handler
• Looper and
• message queue
Thread
A java.lang.Thread object represents the most fundamental abstraction in Android’s underlying thread execution environment.
The Java virtual machine allows an application to have multiple threads of execution running concurrently.
We can create threads in two ways
1. By extending the Thread class.
class TestThread extends Thread { @Override public void run() { Log.d("Threading", "Test class thread "+Thread.currentThread().getName()); } } |
2. By implementing a Runnable interface.
We need to call the start method on the thread to start the execution
Thread Life cycle
The thread has a lifecycle with different states like
- new,
- runnable,
- running,
- non-runnable (blocked),
- terminated.
We can perform any kind of operation inside threads except updating the UI elements.
To update a UI element from a thread, we need to use either the handler or the runOnUIThread method.
Message, MessageQueue, Looper, Handler and HandlerThread
HandlerThread
HandlerThread derived from the Thread class(java) and also implement the run method of it. We start it same as thread class using Thread.start() method.
So What is the difference ?
- Keep a thread alive i.e. do not let it come out of run method
- Maintain a queue and post your task in it
- Process the queue in your run method i.e. execute task one by one or when it comes
- Finish/Terminate the thread once done (once you call quit method)
- creating, inserting, or removing Messages from the Message Queue
- processing Messages on the consumer thread
Message
The Message acts as a container for arbitrary data. The producer thread sends Messages to the Handler, which enqueues to the Message Queue.
The Message provides three pieces of extra information, required by the Handler and Message Queue to process the message:
- what — an identifier the Handler can use to distinguish messages and process them differently
- time — informs the Message Queue when to process a Message
- target — indicates which Handler should process the Message
- MessageQueue also maintains a dispatch barrier that represents the current time according to SystemClock.uptimeMillis
- When a Message timestamp is less than this value, the message is dispatched and processed by the Handler
- Create a class that extend HandlerThread
- Instantiate your handlerThread class
- call start method (your thread is running now)
- Create/Instantiate your Handler by using looper from handler thread created above
it is a better idea to extend handler and add handling for different messages.
Note: Call handlerThread.quit() when you are done with the background thread or on your activities onDestroy() method.
Runnable myRunnable = new Runnable() {
@Override
public void run() {
MainActivity.this.onResult();
} // This is your code
};
mainHandler.post(myRunnable);