如何更改正在运行的java进程的优先级? [英] How to change the priority of a running java process?

查看:146
本文介绍了如何更改正在运行的java进程的优先级?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我尝试了以下方法,但它没有改变进程优先级任务管理器

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();}
    }
}

我能想到的唯一另一件事是使用批处理文件运行程序,但我宁愿把它留在家里也可以这么说。那么有谁知道基于java的方式来改变当前的进程优先级?理想情况下,能够在程序运行时更改进程的优先级以响应用户输入。

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.

推荐答案

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

https://stackoverflow.com/questions/257859 discusses 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 JNA which will let you map the functions easily, or find a ready-written Java wrapper to the API if there is one.

这篇关于如何更改正在运行的java进程的优先级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆