Chapter 8. Concurrency (Multi-Threading)

Concurrency, also sometimes known as parallelism or multi-threading, is the concept of performing multiple tasks at the same time, dividing the computer’s resources into distinct entities that very quickly alternate between each other until any entity has its entire workload completed, at which time it’s removed from this procedure. The act of computation jumping between different processes (small-p) is known as “context switching,” which has a more common, and very different, definition in computer science (switching your brain’s resources from one domain to another).

Tasks

In this chapter, you’ll learn to:

  1. Perform a task in a background thread.

  2. Act on the results of work performed in the background thread on the main thread.

Android

In Java, a processing context is known as a Thread. A Thread is an object instance and is created exactly as you’d expect:

A Thread is passed in a Runnable instance. Once a thread is started (by calling the start method), the thread invokes the run method of that Runnable. As soon as that method is complete (by returning or throwing), the thread terminates.

In Android, there is a single Thread instance that all work is performed on by default, including and especially UI work. That is important—in fact, the concept is sometimes called the “UI thread,” but is more commonly known as the “main” thread. Since the first release of ...

Get Native Mobile Development 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.