How to change the priority of a running java process?

后端 未结 3 1566
挽巷
挽巷 2020-12-11 23:40

In a related question we explored using ProcessBuilder to start external processes in low priority using OS-dependant commands. I also discovered that if a parent process is

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-12 00:04

    (The title does not address windows specifically, but the tags do. However I think it might be relevant to know the differences.)

    In general scheduling of threads an processes is a kernel dependent feature, there is hardly a portable way to do this. In fact what priority means varies greatkly. For example on NT a high value of 24 means realtime and a value of 1 means idle. On unix this is the opposite: 1 is fastest and larger values are slower.

    Of course Java abstracts this information away using .setPriority with a range of 1 (lowest) to 10 (highest).

    Something not pointed out yet, but a pretty big problem on many unixes is: By default a user can not increase the priority of a process (that is reduce the nice value), even if the user itself decreased the priority right before.

    In contrast on NT I think you can reraise your priority back to default priority.

    Simply put: .setPriority may work on windows, but will most likely not work on unix.

提交回复
热议问题