Image for post
Image for post

Exploring Android Thread Priority

Anubhav Gupta
Aug 31, 2018 · 4 min read

Lets start with basic understanding of how scheduling works in Android or Java runtime.

Android Scheduling Basics

The Java runtime supports a very simple, deterministic scheduling algorithm known as fixed priority scheduling.

This algorithm schedules threads based on their priority relative to other runnable threads.

Android’s thread scheduler uses two main factors to determine how threads are scheduled across the entire system: nice values and cgroups.

nice values in Android are used as a measure of a thread’s priority. Threads with a higher nice value (i.e., lower priority, as in they are being “nice” to other threads in the system) will run less often than those with lower nice values

The thread control groups(cgroups) are Linux container that are used to manage the allocation of processor time for all threads in one container

Android defines multiple control groups, but the most important ones for applications are Foreground and Background Group. The threads in different control groups gets different amounts of execution on processor. Threads in FG group gets more CPU time than threads in BG group. Threads in BG groups gets roughly 5% of CPU time.

Image for post
Image for post

Why we need to set thread priority?

The threads created by application by default have the same priority and cgroup membership as UI thread. As a result they compete on equal terms for processor allocation. Hence an application that creates a lot of background threads may reduce performance of UI thread.

Image for post
Image for post
Worker thread competing for resources with UI thread

To decouple background threads from control group of UI thread, we need reduce the priority of background threads.

Setting Thread Priorities

Now we know what are thread priorities and that they should be set correctly to ensure app responsiveness. Let’s look at how to set thread priority in Android.

In Android we have two ways to set priority to threads : Thread.setPriority() and Process.setThreadPriority().

Thread.setPriority() contains a value from MIN_PRIORITY(1) to MAX_PRIORITY(10)

The second way to set priorities is to call Process.setThreadPriority(). It supports value from -20 to 19.

Here is the mapping from Thread class priority to Process class Priority

Image for post
Image for post

Now we know how to set priority lets look at examples in Android.

Normal Thread

Normal thread by default inherits thread priority of caller thread. So if you create a new thread from main thread it will get same priority as main thread.

To reduce the priority of newly created thread you can use Process.setThreadPriority(priority) at starting of run() method of runnable passed in this thread.

Example :

Handler Thread

Handler thread by default has priority Process.THREAD_PRIORITY_DEFAULT

Image for post
Image for post

You can set priority using the other constructor

Image for post
Image for post

Example :

Intent Service

Intent service use handler thread to run the background tasks. As we saw above, the handler thread has default priority if used with single param constructor. Intent service use single param constructor for creating handler thread.

Image for post
Image for post

Since we don’t have access to handler thread inside intent service, so to set priority you need to call Process.setThreadPriority(priority) at start of onHandleIntent() method.

Example :

Executer Service

ExecuterService implementation likeThreadPoolExecutor accepts thread factory as parameter. Using thread factory we can customise threads it uses. As customisation we can set their priority. Now as explained above we should use Process.setThreadPriority() instead Thread.setPriority() to have more refined access to thread priorities.

So to achieve this in thread factory we can create a customised priority thread factory

Example:

Rx

Schedulers in Rx such as io(), computation(), single() use system property and default values to decide thread priority of their threads. Logic is as follows:

Image for post
Image for post
Image for post
Image for post

So to set priority for Rx threads we need to set system properties used by these schedulers.

Note: They use Thread class priorities so we need to set Thread class priorities in system property and not Process class priorities

I have also created a sample project containing all the sample examples. https://github.com/anubhav7nov/ThreadPrioritySample

Thank You!!

MindOrks

Our community publishes stories worth reading on Android…

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store