Cross-platform way to change java process priority

前端 未结 2 2050
遥遥无期
遥遥无期 2020-12-01 23:59

I need to invoke .jar file in separate JVM from another java application, and it very CPU-consuming, so it should run with background priority in order not to affect the res

2条回答
  •  隐瞒了意图╮
    2020-12-02 00:45

    The simple answer is that there is no portable way to change the priority of a Process in Java. (Threads - yes, Processes - no.)

    If your Java application needs to start a new JVM to run the CPU intensive application, then the chances are that it is already not entirely portable. For example, you will typically need to give the pathname of the java command (or equivalent), a -cp argument (or equivalent), system specific JVM options, and so on.

    So, assuming that the command to launch the JVM is already non-portable, it should hardly matter if you replace the command with a wrapper script that does OS-specific things to change the priority of the launched process. (For example, for UNIX or Linux you could simply use nice to launch the JVM.)

提交回复
热议问题