views:

1878

answers:

7

I have a multi thread application built on Java and I need to set different priorities to this threads. In windows it works fine, a bigger value (priority) gets more CPU time just like it was supposed.

On Linux, I cant find a logical answer to how it is working. Looked online, but can't seem to find a definitive answer on how its working on this platform. Some suggest that thread are given equal priorities, and the JVM ignores those settings... Some suggest values are just inverted (small value, more priority) because of the native process priority... etc.

Is there a right way to use this in Linux?

EDIT: I'm currently using Java 1.4.2, but I would appreciate any insight on how things work in recent and future versions.

A: 

As far as I can remember it depends mostly on the used JVM. Some JVMs use real processes for each thread or threadgroup, so you can use the operationg systems tools to tune the priorities.

ComSubVie
+2  A: 

Keep in mind that thread priorities are just a hint to the JVM. Also, on Unix JVMs can use native (natively scheduled) or green (scheduling emulated by JVM) threads.

EDIT: I found an interesting discussion on bugs.sun.com (http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4813310). You may want to try your code on Java 6+ with various magic command-line JVM options.

Alexander
I found that reference to Effective Java well worth...
mrlinx
A: 

Not sure what values you're using, but Java thread priority should be controlled by the constants, (MAX|MIN|NORM)_PRIORITY in Thread. I guess setting outside of the range may have unknown consequences. The current ThreadGroup also affects the max priorty any Thread in that group can have. I'd double check these.

It might be worth checking against Java 5 and 6 just to observe the behavior. There may be underlying changes that aren't obvious in Javadocs since this is likely a low-level JVM change (if any) and you'd have to wade through JVM release changelogs.

As a last resort you can play with schedulers on Linux, but only if you're REALLY desperate to sort this out.

Just curious, what are you doing that thread priority matters?

basszero
+1  A: 

First and foremost, you're going to want to upgrade to a more recent version of Java -- Sun stops supporting Java 1.4.2 in October.

Second, try playing with the JVM startup options (particularly -XX:+UseThreadPriorities).

Craig Trader
without that flag, what is the behavior we should expect?
mrlinx
I believe on Sun's 1.4.2 JVM on Linux thread priorities are completely ignored.
Alexander
A: 

I can't speak for 1.4.2, but OpenJDK 6, priorities are translated to nice values: 1 to 4, 2 to 3, ..., 10 to -5. (For nice values, the lower the better, priority-wise.) See this thread for a full translation table: http://stackoverflow.com/questions/297804/thread-api-priority-translation-to-os-thread-priority

Chris Jester-Young
+2  A: 

There's a "workaround", found by Akshaal. This "fix" gets you exactly what several people have been asking for: That you can lower the priority from Thread priority NORMAL and down (but you can't heighten it to above "nice level 0").

The reason this works is a bug in the JVM code that checks the ThreadPriorityPolicy flag, which should be set to 0 (ignore the requested java priorities) or 1 (use Linux nice levels for the requested java priorities, but this also requires root - and falls back to 0 if a normal user tries to use it). If you set it to anything else than 0 or 1, it "bugs out", and runs as if 1. Since a user cannot heighten the nice level to below 0 (0 is unix normal priority), the OS ignores the negative values, but it does work for the positive values (which is lower priorities).

Try this JVM flag as a normal user: -XX:ThreadPriorityPolicy=42

I wrote a bit about it here: http://tech.stolsvik.com/2010/01/linux-java-thread-priorities-workaround.html

stolsvik
A: 

@stolsvik You are absolutely right. Java thread priorities are completely ignored by Linux JVM. I wanted to 'vote up' for you for sharing such a good article relating to priorities but guess I do not have the reputation points yet to do that. Anyways, thanks for sharing the JVM bug to get around the thread priorities in Linux.