开发者

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 fo开发者_如何学编程rm 0 (lowest) to 31 (highest).


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.


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".)


Have you tried using the constants Thread.MIN_PRIORITY and Thread.MAX_PRIORITY to define the priority of your threads?


It worth noting that the thread priority is just a hint which is largely ignored on Linux, unless you are root and can only be lowered on windows.

In short, you shouldn't be writing your program to rely on the behaviour of thread priorities. Whatever you are trying to do is best done another way.


Use setPriority, the priority level range from 1 (least important) to 10 (most important), if no level is explicitly set, the priority level is 5 by default.

Also check constants MAX_PRIORITY and MIN_PRIORITY


-20 (highest) to +20 (lowest), 0 is default. You cannot increment the nice value by a negative value if you're not root, though. (e.g. incrementing a nice value of 5 by -7 would be ignored, because you usually haven't got the permissions to do that).

EDIT see https://stackoverflow.com/questions/128039/java-threads-priority-in-linux

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜