Android from the foundation · Puzzle 3 · 4 of 23

Threads share one house

All the threads in a process see the same memory. That's what makes them useful together, and it's also how two of them can quietly break the same value. Here's the trap, and Android's way around it for the screen.

The problem

In puzzle 2, a download thread and the main thread worked in the same process. The download finishes and has a result: how does the main thread get it? And if both threads can reach the same data, what happens when both change it at once?

See it

Two threads in one process share one value; both read 5, both add one and both write 6, so one update is lost; with one owner, only the main thread writes and the other hands it a note kernel one process, one memory count = 5 thread A thread B reads 5reads 5 count = 6both wrote 6: one is lost screen textnoteone owner writes main thread
  1. Threads in one process share its memory: both A and B can read and write count.

    Step 1 of 4
  2. Both want to add one. Each first reads the value: both read 5.

    Step 2 of 4
  3. Each adds one and writes back 6. The count should be 7: one update is lost. That's a race condition. Predict: how do you stop it?

    Step 3 of 4
  4. A lock makes them take turns, or give the value one owner. Android gives the screen one owner: the main thread. Others hand it a note.

    Step 4 of 4

The building block

A process has one block of memory (puzzle 0), and every thread inside it can read and write all of it. A value one thread writes, the others can read a moment later. That's how threads cooperate: one prepares data, another uses it.

The danger is changing the same value at the same moment. Even count = count + 1 is three steps for the CPU: read the value, add one, write it back. If two threads both read 5, both add one and both write 6, one update is lost: the count should be 7. This is a race condition: the result depends on the exact order the kernel happened to run their steps in, so it works most of the time and breaks once in a while.

There are two ways out. A lock lets only one thread at a time into a piece of code: the second one waits until the first is done. Or give the value one owner: only one thread ever touches it, and the others ask it to.

Code that stays correct when several threads use it at once is thread-safe. Android's screen code isn't, so it uses the second way. Buttons, text and everything drawn belong to the main thread, and no other thread may change them. Puzzle 16 shows how other threads ask the main thread to do it for them.

The analogy

The house has one whiteboard everybody can write on. Two people each read "5 visitors", each add one, each write "6": one visitor vanished. Either they take turns with the marker (a lock), or only the clerk may write on it and the others hand her notes (one owner).

Check yourself

Two threads each add 1 to the same counter, 1,000 times, with no lock. The counter starts at 0. What can it end at?

Right. Read, add, write: two threads can read the same old value.

Not quite. Both run; the kernel switches between them, and sometimes their steps overlap.

Not quite. Adding one is three steps (read, add, write), and two threads can interleave them.

Not quite. Threads share the process's memory, so each sees the other's writes.

A download thread has a result for the screen. What's the safe way to show it?

Right. One owner: only the main thread touches the screen.

Not quite. Android's screen code isn't built for locks; it has one owner, the main thread.

Not quite. Shared memory is exactly why that's unsafe: the main thread may be drawing the same text.

What's the difference between a lock and one owner?

Not quite. That's the two swapped.

Right. Turns at the marker vs only the clerk writes.

Sources

What you now know

Threads in one process share its memory: a value one thread changes, every other thread sees. If two threads change the same value at the same moment, their steps can interleave and an update is lost. The fix is a lock (one thread at a time) or one owner (only one thread ever touches it). Android picked one owner for the screen: the main thread.