Android Topics Threads and the MessageQueue Review of Basic Threads MessageQueue, Looper, and Handler
Review of Basic Threads What is the main thread of execution for a given Android application? Does every application have its own UI Thread? The UI Thread controls UI objects. What are these objects? Name two app components that are created and performed on the UI Thread? Why do we need Threads in an application? Is it possible to update UI elements from a background thread?
What is the main thread of execution for a given Android application? Question: What is the main thread of execution for a given Android application? Answer: UI Thread
Does every application have its own UI Thread? Question: Does every application have its own UI Thread? Answer: Yes.
The UI Thread controls UI objects. What are these objects? Question: The UI Thread controls UI objects. What are these objects? Answer: View objects
Question: Name two app components that are created and performed on the UI Thread? Answer: Activities Intents NOTE: It is a RULE that system calls to these components are performed on the UI thread.
Why do we need Threads in an application? Question: Why do we need Threads in an application? Answer: Heavy Calculations Long initialization of objects Networking Database Operations
Is it possible to update UI elements from a background thread? Question: Is it possible to update UI elements from a background thread? Answer: No.
Structure of Runnable //TASK 1: CREATE A RUNNABLE OBJECT Runnable r = new Runnable() { @Override public void run() { while (Processing ) Log.i("THREAD DEMO", "Working on it"); } }; //TASK 2: CREATE A THREAD TO PROCESS A RUNNABLE OBJECT Thread myBackgroundThread = new Thread(r); //TASK 3: START THE THREAD myBackgroundThread.start();
Threads and the MessageQueue Android maintains a queue called a MessageQueue. This queue is always populated with tasks that will update the UI. These tasks are Runnable objects and will be executed in sequence and can be as simple as rendering a button or setting the text of a TextView.
The Message Queue and the Looper Tasks within the Message Queue are in a constant loop, called a Looper. A Looper loops over a MessageQueue and dispatches messages one at a time to be executed. Looper
The Message Queue and the Looper A background thread cannot add a task to the MessageQueue. Looper Background Thread
The Message Queue and the Looper A background Thread needs the assistance of a Handler to add tasks to a MessageQueue. Tasks are not added directly to a MessageQueue, but rather through Handler objects associated with the Looper. Background Thread
Tasks on the MessageQueue are Runnable objects. It is important to remember that adding and removing messages to and from the MessageQueue is done by the Handler.
In the image, Handler A adds and removing messages to and from the MessageQueue. A Looper dispatches messages one at a time to be executed.
Create a handler and a looper. Create an instance of a Handler. Note: The Handler object will post Runnable tasks to the main MessageQueue. The posted Runnable tasks will be simple – set the TextView with the count value. Instantiate the Handler and provide a reference to the MessageQueue for the UI Thread (the main default looper). Create a backbround Thread containing a Runnable Object. Specify the work that is performed by the background thread. This code will be placed in the abstract method run(). Use a Handler to constrict a Runnable object that updates the MessageQueue. Post this Runnable object to the MessageQueue. Start the background Thread.