Cover by Marko Gargenta

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

O'Reilly logo

Threading in Android

A thread is a sequence of instructions executed in order. Although each CPU can process only one instruction at a time, most operating systems are capable of handling multiple threads on multiple CPUs, or interleaving them on a single CPU. Different threads need different priorities, so the operating system determines how much time to give each one if they have to share a CPU.

The Android operating system is based on Linux and as such is fully capable of running multiple threads at the same time. However, you need to be aware of how applications use threads in order to design your application properly.

Single Thread

By default, an Android application runs on a single thread. Single-threaded applications run all commands serially, meaning the next command is not completed until the previous one is done. Another way of saying this is that each call is blocking.

This single thread is also known as the UI thread because it’s the thread that processes all the user interface commands as well. The UI thread is responsible for drawing all the elements on the screen as well as processing all the user events, such as touches on the screen, clicks of the button, and so on. Figure 6-8 shows the execution of our code on a single UI thread.

Single-threaded execution

Figure 6-8. Single-threaded execution

The problem with running StatusActivity on the single thread is our network call to update the status. ...

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