Do Linux JVMs actually implement Thread priorities? Do Linux JVMs actually implement Thread priorities? multithreading multithreading

Do Linux JVMs actually implement Thread priorities?


Well, let's look at the source:

line 2947:

////////////////////////////////////////////////////////////////////////////////// thread priority support// Note: Normal Linux applications are run with SCHED_OTHER policy. SCHED_OTHER// only supports dynamic priority, static priority must be zero. For real-time// applications, Linux supports SCHED_RR which allows static priority (1-99).// However, for large multi-threaded applications, SCHED_RR is not only slower// than SCHED_OTHER, but also very unstable (my volano tests hang hard 4 out// of 5 runs - Sep 2005).//// The following code actually changes the niceness of kernel-thread/LWP. It// has an assumption that setpriority() only modifies one kernel-thread/LWP,// not the entire user process, and user level threads are 1:1 mapped to kernel// threads. It has always been the case, but could change in the future. For// this reason, the code should not be used as default (ThreadPriorityPolicy=0).// It is only used when ThreadPriorityPolicy=1 and requires root privilege.

...

line 2982:

 static int prio_init() {   if (ThreadPriorityPolicy == 1) {     // Only root can raise thread priority. Don't allow ThreadPriorityPolicy=1     // if effective uid is not root. Perhaps, a more elegant way of doing     // this is to test CAP_SYS_NICE capability, but that will require libcap.so     if (geteuid() != 0) {       if (!FLAG_IS_DEFAULT(ThreadPriorityPolicy)) {         warning("-XX:ThreadPriorityPolicy requires root privilege on Linux");       }       ThreadPriorityPolicy = 0;     }   }   return 0; }

...

line 2997:

OSReturn os::set_native_priority(Thread* thread, int newpri) {  if ( !UseThreadPriorities || ThreadPriorityPolicy == 0 ) return OS_OK;  int ret = setpriority(PRIO_PROCESS, thread->osthread()->thread_id(), newpri);  return (ret == 0) ? OS_OK : OS_ERR;}

So! At least on Sun Java, on Linux, you won't see thread priorities unless you have done -XX:ThreadPriorityPolicy and that seems to require root.


Just a shot in the dark here, but wouldn't having prioritized threads in the JVM require having the ability to adjust the priority of the operating system threads?

Linux (and any Unix-like OS) limits the ability to give processes higher priority to root. I would think there would be a similar limitation on threads.


The default Linux thread scheduler policy SCHED_OTHER does not support priorities. Or to be more exact, it support a priority setting with one value: 0. The other so called 'real time' policies SCHED_FIFO and SCHED_RR support higher priorities, but are only available to processes with super user privileges.