Cross-platform way to change java process priority

前端 未结 2 2065
遥遥无期
遥遥无期 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:58

    I don't know the way to set the priority for an external process. Thread however has a setPriority method, so if you control the target application, you could perhaps add a switch, telling the application to set its own priority to minimum:

    theThread.setPriority(Thread.MIN_PRIORITY);

    If it still affects the system, I suggest you interleave some short sleeping to offload the cpu.


    Another option:

    If you put the target .jar in the classpath of the "initiating" application, you can simply invoke the main-method of the jar-file in a newly created thread, and then set the priority using the above method. (This should work even if you don't control the source-code of the target jar file.)

提交回复
热议问题