Thread Priority Level

To make a Java application for Linux with threads, if I want to set the priority level, which is the minimum and maximum values?

On Windows, ranges go from 0 (lowest) to 31 (highest).

+4
source share
6 answers

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

For security reasons, if you use resources between threads of different priorities, you may run into problems of priority inversion. That is, when a low priority stream contains a resource with a high priority stream waiting for it. Then a high priority thread can wait a long time.

+5
source

The Thread class has two static int fields called MIN_PRIORITY and MAX_PRIORITY , the actual values โ€‹โ€‹of which are potentially (if not realistic) for platforms 1 and 10 . It is theoretically possible that these values โ€‹โ€‹may change in some future releases of Java. However, this is unlikely, not least because such a change would violate binary compatibility. (The value of a primitive constant can be bound to the code that uses it at compile time.) In any case, the specification (for example) of the setPriority method implies that the MIN_PRIORITY value will be less than or equal to the value of MAX_PRIORITY.

EDIT

And ... so you really want to know how Java priority values โ€‹โ€‹(e.g. Thread.MIN_PRIORITY and Thread.MAX_PRIORITY ) are mapped to the priorities of the native Linux thread:

  • Again, priority mappings may be platform / JVM / version specific. The sun does not indicate what they are.

  • It is unlikely that you can define priority mappings in a pure Java application.

  • Experimentally, you can map mappings by setting different priorities for Java threads and looking at the corresponding priorities for your own Linux threads. (There is probably a way to get the ps command to print thread priorities.)

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

EDIT 2

In fact, according to this page , comparing Java priorities with priorities of their own threads depends on (and can be explicitly set using) java parameters for HotSpot -XX. (Find the priority page.)

+3
source

Have you tried using the Thread.MIN_PRIORITY and Thread.MAX_PRIORITY constants to prioritize your threads?

+1
source

It is worth noting that the priority of the stream is just a hint that is mostly ignored on Linux if you are not root and can only be omitted from windows.

In short, you should not write your program to rely on thread priority behavior. Everything you try to do is best done in a different way.

+1
source

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

Also check the constants MAX_PRIORITY and MIN_PRIORITY

+1
source

-20 (highest) to +20 (lowest), 0 by default. However, you cannot increment nice with a negative value unless you are root. (for example, incrementing a good value of 5 by -7 will be ignored because you usually do not have permission to do this).

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

0
source

Source: https://habr.com/ru/post/1316655/


All Articles