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
Threads in one process share its memory: both A and B can read and write
count.Step 1 of 4Both want to add one. Each first reads the value: both read 5.
Step 2 of 4Each 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 4A 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
Sources
- Android Developers: Processes and threads overview ("the Android UI toolkit is not thread-safe. So, don't manipulate your UI from a worker thread. Do all manipulation to your user interface from the UI thread.")
- Kotlin docs: Shared mutable state and concurrency ("Coroutines can be executed parallelly using a multi-threaded dispatcher like the Dispatchers.Default. It presents all the usual parallelism problems. The main problem being synchronization of access to shared mutable state.")
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.
Answer every question above correctly to unlock the next puzzle.
Something wrong or unclear on this page? Tell me: a puzzle that didn't click is a bug in the puzzle.