Threading and Concurrency:
-
What is the Android Main Thread (UI Thread), and why is it important to perform UI operations on it?
- The Android Main Thread is the thread responsible for handling the user interface. To maintain responsiveness and avoid ANR (Application Not Responding) errors, execute all UI operations on this thread.
-
Explain the difference between a Handler and a Thread in Android.
- You use a Handler to send and process messages in a specific order on a single thread, typically the UI thread. A Thread is a separate, independent execution unit.
-
What is the purpose of the AsyncTask class in Android, and how is it used for background tasks?
- AsyncTask is used for performing background tasks and updating the UI on the main thread. It provides a straightforward way to work with threads without managing them manually.
-
How can you perform network operations on a separate thread in Android?
- To perform network operations on a separate thread, you can use classes like AsyncTask, Thread, or Executor along with background threads to avoid blocking the UI thread.
-
Explain the concept of the Looper in Android, and why is it used?
- The Looper is used to manage message queues and message processing in a thread. It’s essential for creating threads that can handle asynchronous tasks, such as those in Handler or AsyncTask.
-
What is the purpose of the runOnUiThread() method in Android, and how is it used?
- runOnUiThread() is used to run a code block on the UI thread from a background thread. It’s often used to update UI elements safely from non-UI threads.
-
What is the difference between Handler, Looper, and MessageQueue in Android’s threading model?
- A Looper oversees a thread’s message queue, where you employ a Handler to post messages to it, and a MessageQueue stores pending messages.
-
Explain the purpose of the ThreadPoolExecutor in Android, and how does it work?
- ThreadPoolExecutor is a framework for efficiently managing a pool of worker threads. It helps optimize the use of system resources and parallelize tasks.