views:

86

answers:

6

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

A: 

-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 http://stackoverflow.com/questions/128039/java-threads-priority-in-linux

Tedil
@Tedil - I think you are confusing the Linux thread/process priority with the Java thread priority. Java allows you to set a Java thread priority to any value from MIN_PRIORITY to MAX_PRIORITY. How these values are mapped to native thread priorities is ... not relevant here.
Stephen C
values goes from -20 (highest) to 19 (lowest)
xdevel2000
@Stephen C Well, after reading more answers and comments, he actually *does* want to know how these values are mapped (which is JVM specific). (And no, I didn't confuse it - Java *is* platform independent and it wouldn't make sense to use OS specific parameters.)
Tedil
@Tedil - yea, it turns out he was interested in the mapping.
Stephen C
A: 

This page seems to have answers about this : http://www.javamex.com/tutorials/threads/priority.shtml

CFP
+4  A: 

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.

Romain Hippeau
`MIN_PRIORITY` and `MAX_PRIORITY` [*do* have defined values](http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/constant-values.html#java.lang.Thread.MIN_PRIORITY). They are 1 and 10 respectively. The implementation-defined thing is how those levels map to actual OS prioerity levels!
Joachim Sauer
A: 

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

Stephen C
My question was concerning the value on Linux system that was mapped by the JVM when I do setPriority with MAX_PRIORITY!
xdevel2000
@xdevel2000 - the only 100% reliable answer is to print out the values of those constants **using your installed JVM** ... and don't assume that they will be the same on another JVM.
Stephen C
Not again, if I print out Thread.MAX_PRIORITY I have 10 that is the value of this constant. What I'd like to know is how 10 is mapped to the equivalent "max value" on a Linux system. Sorry, may be I explain myself bad because I'm not a native english!!!
xdevel2000
@xdevel2000: your questions doesn't really explain that you're interested in the mapping from the Java value to the Linux kernel value. Maybe you should expand on that, by updating your question.
Joachim Sauer
+1  A: 

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

jdecuyper
A: 

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.

Peter Lawrey