Chapter 2. Multithreading in Java

Every Android application should adhere to the multithreaded programming model built in to the Java language. With multithreading comes improvements to performance and responsiveness that are required for a great user experience, but it is accompanied by increased complexities:

  • Handling the concurrent programming model in Java
  • Keeping data consistency in a multithreaded environment
  • Setting up task execution strategies

Thread Basics

Software programming is all about instructing the hardware to perform an action (e.g., show images on a monitor, store data on the filesystem, etc.). The instructions are defined by the application code that the CPU processes in an ordered sequence, which is the high-level definition of a thread. From an application perspective, a thread is execution along a code path of Java statements that are performed sequentially. A code path that is sequentially executed on a thread is referred to as a task, a unit of work that coherently executes on one thread. A thread can either execute one or multiple tasks in sequence.

Execution

A thread in an Android application is represented by java.lang.Thread. It is the most basic execution environment in Android that executes tasks when it starts and terminates when the task is finished or there are no more tasks to execute; the alive time of the thread is determined by the length of the task. Thread supports execution of tasks that are implementions of the java.lang.Runnable interface. ...

Get Efficient Android Threading now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.