2

On a Unix system, you can run a process at lower CPU "priority" (pedantically, it does not change the thing that is called the priority, but rather influences what share of available CPU time is used, which is "priority" in the general sense) 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 the Linux case.

Raedwald
  • 46,613
  • 43
  • 151
  • 237
  • http://stackoverflow.com/a/2866604/829571 – assylias May 04 '17 at 15:06
  • 1
    "Niceness" is not the same thing as priority. The goal of niceness is that if two processes are CPU-bound at the same time, the "nicer" one should get fewer CPU cycles on average. "Priority" usually is about which process is able to preempt the other when some asynchronous event occurs. – Solomon Slow May 04 '17 at 15:16
  • To summarise an answer [elsewhere](https://stackoverflow.com/a/7684821/545127): POSIX requires that the threads of a process share a *nice* value, but in Linux the can have different *nice* values that can cause threads to be scheduled more or less often, so implicitly the *nice* value **does** affect the scheduling of threads. – Raedwald Feb 26 '21 at 07:14

3 Answers3

2

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

F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY        TIME CMD
0 -  1000 20746 10006  0   -   - - 1739135 -    pts/2      0:00 java Main
0 S  1000     -     -  0  99  19 -     - futex_ -          0:00 -
1 S  1000     -     -  0  99  19 -     - wait_w -          0:00 -
1 S  1000     -     -  0  99  19 -     - futex_ -          0:00 -
1 S  1000     -     -  0  99  19 -     - futex_ -          0:00 -

Start process with : nice -n 15 java Main
Output of ps -m -l 21488

F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY        TIME CMD
0 -  1000 21488 10006  0   -   - - 1722494 -    pts/2      0:00 java Main
0 S  1000     -     -  0  95  15 -     - futex_ -          0:00 -
1 S  1000     -     -  0  95  15 -     - wait_w -          0:00 -
1 S  1000     -     -  0  95  15 -     - futex_ -          0:00 -
1 S  1000     -     -  0  95  15 -     - futex_ -          0:00 -

The NI column seems to reflect what I passed to nice and the priority changes accordingly too. I got the process ID (20746, 21488) using jps.

Note that running jstack 21488 for example will not give the above information.

I ran the above on Ubuntu 16.04 LTS (64bit)

Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82
1

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?

Community
  • 1
  • 1
jiveturkey
  • 2,484
  • 1
  • 23
  • 41
0

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.

slim
  • 40,215
  • 13
  • 94
  • 127