O'Reilly logo

Efficient Android Threading by Anders Goransson

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

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 the internal execution of the application. But the application is exposed to system threads like the UI and Binder threads, and creates background threads of its own. It is now time to get in under the hood of threading on the Android platform:

  • Differences and similarities between UI, Binder and background threads.
  • The Linux thread coupling.
  • How thread scheduling is affected by the application process rank.
  • Observe 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 when the application is started 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 ...

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