windows 如何更改正在运行的java进程的优先级?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6239142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:59:44  来源:igfitidea点击:

How to change the priority of a running java process?

javawindowsprocesswindows-task-scheduler

提问by Andrew

In a related questionwe explored using ProcessBuilder to start external processes in low priority using OS-dependant commands. I also discovered that if a parent process is low priority, then all of its spawned processes start in low priority. So my new question is about starting a java file (run via double-clicking an executable jar in windows) in low priority or changing its priority programmatically during the run. I have tried altering the thread priority, but this has no effect on the windows process priority.

在一个相关的问题中,我们探讨了使用 ProcessBuilder 使用依赖于操作系统的命令以低优先级启动外部进程。我还发现,如果一个父进程是低优先级,那么它所有衍生的进程都会以低优先级启动。所以我的新问题是关于以低优先级启动 java 文件(通过双击 Windows 中的可执行 jar 运行)或在运行期间以编程方式更改其优先级。我曾尝试更改线程优先级,但这对 Windows 进程优先级没有影响。

I have tried the following, but it does not change the process priority in the task manager

我尝试了以下操作,但它不会更改任务管理器中的进程优先级

public class hello{
    public hello(){
        try{
            Thread.currentThread().setPriority(1);
            Thread.sleep(10000);    
        }catch(Exception e){e.printStackTrace();}
    }
}

The only other thing I can think of is to run the program using a batch file, but I would rather keep this in the family so to speak. So does anyone know of a java-based way to change the current process priority? Ideally, it would be nice to be able to change the priority of the process in response to user input while the program is running.

我唯一能想到的另一件事是使用批处理文件运行程序,但可以这么说,我宁愿将其保留在家庭中。那么有谁知道一种基于 Java 的方法来更改当前进程优先级?理想情况下,能够在程序运行时响应用户输入更改进程的优先级会很好。

采纳答案by Duncan McGregor

https://stackoverflow.com/questions/257859discusses how to change the priority of a thread in Windows. I don't know of any Java API to do this, so you're going to have to fall back on JNI to call into the Windows API. In your shoes I think I'd start with JNAwhich will let you map the functions easily, or find a ready-written Java wrapper to the API if there is one.

https://stackoverflow.com/questions/257859讨论了如何在 Windows 中更改线程的优先级。我不知道有任何 Java API 可以执行此操作,因此您将不得不依靠 JNI 来调用 Windows API。在你看来,我想我会从JNA开始,它可以让你轻松地映射函数,或者找到一个现成的 Java 包装器到 API,如果有的话。

回答by Peter Lawrey

Perhaps you are trying to do something the OS does for you.

也许您正在尝试做一些操作系统为您做的事情。

In Unix, under load, each process is given a short time slice to do its work. If it uses all its time slice it is assume the process is CPU bound it priority is lowers. If it blocks on IO, it is assumed to be IO bound and its priority is raised (because it didn't use all its time slice)

在 Unix 中,在负载下,每个进程都有一个很短的时间片来完成它的工作。如果它使用所有时间片,则假定该进程受 CPU 限制,它的优先级较低。如果它在 IO 上阻塞,则假定它是 IO 绑定的并且它的优先级被提高(因为它没有使用它的所有时间片)

All this only matters if there isn't enough CPU. If you keep you CPU load below 100% most of the time, every process will get as much CPU as it needs and the priority doesn't make much difference.

所有这些只有在没有足够的 CPU 时才重要。如果您大部分时间都将 CPU 负载保持在 100% 以下,则每个进程都将获得所需的 CPU 数量,并且优先级没有太大区别。

回答by Fabian

(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.

一般来说,进程的线程调度是一个内核相关的特性,几乎没有一种可移植的方法来做到这一点。事实上,优先级的含义差别很大。例如在 NT 上,高值 24 表示实时,值 1 表示空闲。在 unix 上,这是相反的:1 是最快的,而较大的值则较慢。

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

当然,Java 使用.setPriority1(最低)到 10(最高)的范围来抽象这些信息。

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.

还没有指出,但许多 unix 上的一个相当大的问题是:默认情况下,用户不能提高进程的优先级(即降低 nice 值),即使用户自己之前降低了优先级。

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

相比之下,在 NT 上,我认为您可以将优先级重新提高为默认优先级。

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

简单地说:.setPriority可以在 windows 上工作,但很可能在 unix 上不工作。