Android from the foundation · Puzzle 18 · 19 of 23

Slow work off the main thread, and back

Puzzle 17 said slow work must leave the main thread. But only the main thread may touch the screen. Here's the trip every Android app makes: hand the work to another thread, then hand the result back.

The problem

A tap should load a 2-second list. On the main thread, the screen freezes for 2 seconds and might even get an ANR (puzzle 17). On another thread, the list loads, but that thread may not touch the screen (puzzle 3). How does the list get from one to the other?

See it

A tap reaches the main thread, which hands the slow load to a worker thread and stays free; the worker posts the result into the main thread's queue; the main thread shows it on screen kernel the screen main thread tap loading… 2 sdone worker thread main thread: still free main thread's queuepost list shown
  1. A tap should load a 2-second list. The main thread must stay free for frames and touches.

    Step 1 of 4
  2. So it hands the load to a worker thread, and goes straight back to its queue.

    Step 2 of 4
  3. Done, the worker doesn't touch the screen: it posts the result into the main thread's queue. Predict: who shows the list?

    Step 3 of 4
  4. The main thread, when it reaches that message, like any other.

    Step 4 of 4

The building block

A worker thread is any thread that isn't the main thread, made to do slow work: download, read a file, sort a big list. While it works, the main thread keeps drawing frames and handling touches.

When the worker has the result, it doesn't touch the screen itself. It posts a small piece of code to the main thread's message queue (puzzle 16), and the main thread runs it when it gets there, like any other message:

val main = Handler(Looper.getMainLooper())   // a Handler for the main thread's queue

button.setOnClickListener {
    thread {                                  // a new worker thread
        val items = loadList()                // 2 seconds, off the main thread
        main.post { list.show(items) }        // back on the main thread
    }
}

thread { } (from Kotlin's standard library) starts a new thread that runs the code in the braces. Inside an Activity, runOnUiThread { } is a shortcut for the same post.

That's the whole pattern: out to a worker, back with a post. In real apps you'll write it with Kotlin coroutines, which make exactly this trip for you; underneath, the result still comes back as a message on the main thread.

The analogy

The clerk at the front desk sends a runner to the archive. The clerk keeps serving visitors. When the runner comes back, he doesn't barge in: he drops the file in the clerk's in-tray, and the clerk hands it to the visitor.

Check yourself

A worker thread finished loading the list and sets the screen's text itself. What's wrong?

Not quite. The screen has one owner, the main thread; another thread changing it can break it (puzzle 3).

Right. Out to a worker, back with a post.

Not quite. Speed isn't the issue: the rule is who may touch the screen.

While the worker loads for 2 seconds, the user taps another button. What happens?

Not quite. Touches are the main thread's job; the worker only loads.

Not quite. The main thread isn't blocked: the slow work is on the worker.

Right. The main thread stays free while the worker works.

What's the difference between thread { } and main.post { }?

Not quite. That's the two swapped. Re-read that part ↑

Right. Out vs back.

Sources

What you now know

Slow work runs on a worker thread, so the main thread stays free for frames and touches. The worker never touches the screen: it posts the result back to the main thread's queue (with a Handler for the main thread, or runOnUiThread), and the main thread shows it. Coroutines make the same trip for you.