On a Unix system, you can run a process at lower CPU priority using the nice
command:
nice program
And you could use that to run a JVM process:
nice java -jar program.jar
The Java program run by that JVM process will start multiple threads.
Does the nice
change affect the scheduling of those Java threads? That is, will the Java threads have a lower CPU priority when run as
nice java -jar program.jar
that when run as
java -jar program.jar
In general, this will be system dependent, so I am interested in teh Linux case.
Actually...Niceness is a property of the application according to POSIX.1. Here is a more detailed post. is nice() used to change the thread priority or the process priority?
According to what
ps
reports niceness is applied to java threads. I ran this quick test with a java application that waits for user input:Start process with :
nice -n 19 java Main
Output of
ps -m -l 20746
Start process with :
nice -n 15 java Main
Output of
ps -m -l 21488
The
NI
column seems to reflect what I passed tonice
and the priority changes accordingly too. I got the process ID (20746, 21488) usingjps
.Note that running
jstack 21488
for example will not give the above information.I ran the above on Ubuntu 16.04 LTS (64bit)
Java is not special. It's just a process, and the OS sets its "niceness" the same way as with any other process.
On Linux, Java threads are implemented using native threads, so again, "niceness" is subject to OS controls in the same way as any other native thread.