Thread priority is a hint to the OS scheduler about how important a thread is relative to others. Higher priority threads are more likely to get CPU time.
| Constant | Value | Use Case |
|---|---|---|
| Thread.MIN_PRIORITY | 1 | Background, non-urgent |
| Thread.NORM_PRIORITY | 5 | Default |
| Thread.MAX_PRIORITY | 10 | Critical, time-sensitive |
Java/Kotlin Thread
val thread = Thread {
// Low priority background work
}
thread.priority = Thread.MIN_PRIORITY
thread.start()
// Or inside the thread
Thread {
Thread.currentThread().priority = Thread.MAX_PRIORITY
// High priority work
}.start()
Android provides finer control with Process.setThreadPriority():
Thread {
// Set Android thread priority (different scale!)
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND)
// -20 (highest) to 19 (lowest), 0 is default
}.start()
| Constant | Value | Use Case |
|---|---|---|
| THREAD_PRIORITY_URGENT_AUDIO | -19 | Audio playback |
| THREAD_PRIORITY_AUDIO | -16 | Audio processing |
| THREAD_PRIORITY_URGENT_DISPLAY | -8 | Display updates |
| THREAD_PRIORITY_DISPLAY | -4 | UI work |
| THREAD_PRIORITY_DEFAULT | 0 | Normal |
| THREAD_PRIORITY_BACKGROUND | 10 | Background work |
| THREAD_PRIORITY_LOWEST | 19 | Least important |
class BackgroundTask : Thread() {
override fun run() {
// Mark as background priority
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND)
// Now this thread won't compete with UI thread
performHeavyWork()
}
}
In modern Android, let the framework handle priorities:
Mark this when you can explain the answer in your own words.
Help fellow developers prepare for interviews
Sharing helps the Android community grow ๐