O'Reilly logo

Efficient Android Threading by Anders Goransson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 is required for a great user experience, but it is accompanied with 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 file system, 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 is executed 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. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required