Chapter 3. Threads on Android

Every Android application is started with numerous threads that are bundled with the Linux process and the Dalvik VM to manage its internal execution. But the application is exposed to system threads, like the UI and binder threads, and creates background threads of its own. In this chapter, we’ll get under the hood of threading on the Android platform, examining the following:

  • Differences and similarities between UI, binder, and background threads
  • Linux thread coupling
  • How thread scheduling is affected by the application process rank
  • Running Linux threads

Android Application Threads

All application threads are based on the native pthreads in Linux with a Thread representation in Java, but the platform still assigns special properties to threads that make them differ. From an application perspective, the thread types are UI, binder, and background threads.

UI Thread

The UI thread is started on application start and stays alive during the lifetime of the Linux process. The UI thread is the main thread of the application, used for executing Android components and updating the UI elements on the screen. If the platform detects that UI updates are attempted from any other thread, it will promptly notify the application by throwing a CalledFromWrongThreadException. This harsh platform behavior is required because the Android UI Toolkit is not thread safe, so the runtime allows access to the UI elements from one thread only.

Note

UI elements in Android are ...

Get Efficient Android Threading 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.