Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread priority level

To make an application in Java under Linux with threads if I wanted to set a priority level which are min and max values?

In Windows the ranges goes form 0 (lowest) to 31 (highest).

like image 945
xdevel2000 Avatar asked Dec 22 '22 01:12

xdevel2000


2 Answers

In Java it is Thread.MIN_PRIORITY to Thread.MAX_PRIORITY there are no defined ranges, because it depends on the underlying OS and/or JVM.

For performance beware, if you are sharing resources between Threads of different priorities you may run into Priority Inversion issues. That is when a low priority Thread holds a resource with a high priority thread waiting on it. Then the high priority thread may wait for a long time.

like image 106
Romain Hippeau Avatar answered Jan 10 '23 12:01

Romain Hippeau


The Thread class has two static int fields called MIN_PRIORITY and MAX_PRIORITY whose actual values are potentially (if not actually) platform specific 1 and 10. It is theoretically possible that these values could change in some future Java release. However, it is highly unlikely, not least because such a change would break binary compatibility. (The value of a primitive constant may be bound to the code that uses it at compile time.) Either way, it is implicit in the specification of (for example) the setPriority method that the value of MIN_PRIORITY will be less than or equal to the value of MAX_PRIORITY.

EDIT

Ah ... so you really want to know how the Java priority values (e.g. Thread.MIN_PRIORITY and Thread.MAX_PRIORITY) map to Linux native thread priorities:

  • Once again, the priority mappings could be platform / JVM / version specific. Sun don't specify what they are.

  • It is highly unlikely you can determine the priority mappings in a pure Java application.

  • Experimentally you may be able to figure the mappings out by setting various Java thread priorities and looking at the corresponding Linux native thread priorities. (There's probably some way to get the ps command to print out thread priorities.)

  • Alternatively, you could download and read the OpenJDK source code (for your version / platform) to see what the Java runtime actually does.

EDIT 2

In fact, according to this page, the mapping of Java priorities to native thread priorities depends on (and can be explicitly set using) HotSpot -XX java options. (Search the page for "priority".)

like image 31
Stephen C Avatar answered Jan 10 '23 12:01

Stephen C