Thread priorities

In our Thread methods overview, we mentioned the setPriority() and getPriority() methods. The essential idea is that we call setPriority() on a thread, passing in an integer representing its priority. The number should lie in the range of two constants MIN_PRIORITY and MAX_PRIORITY defined on Thread, and will typically reference NORM_PRIORITY, the default priority of a thread if we don't set it to anything else. For example, to give a thread a priority that is "half way between normal and maximum", we could call:

thr.setPriority((Thread.MAX_PRIORITY - Thread.NORM_PRIORITY) / 2);

Problems with thread priorities

The notion that some threads should have a higher "priority" than others is an intuitive idea. However, there are a couple of caveats with thread priorities that we need to beware of:

Next: thread priority in more detail

Having sufficiently vilified thread priorities, on the next page we'll look in more detail at what Java thread priority means on different systems.


If you enjoy this Java programming article, please share with friends and colleagues. Follow the author on Twitter for the latest news and rants.

Editorial page content written by Neil Coffey. Copyright © Javamex UK 2021. All rights reserved.