简体   繁体   English

关于Java线程和进程优先级的一些问题

[英]Some questions about Java Threads and Process Priorities

I have a small question about threads. 我对线程有一个小问题。 On Unix systems, we have nice , which can be used to set priorities processes. 在Unix系统上,我们nice ,可以用来设置优先级进程。 OK, on ​​my system, I call some external processes, however, I would like to set priority for them. 好的,在我的系统上,我调用了一些外部进程,但是,我想为它们设置优先级。 In unix, I could call other ProcessBuilder and set the nice to process I want, but in Windows, it is not possible. 在unix中,我可以调用其他ProcessBuilder并设置好我想要的进程,但在Windows中,它是不可能的。

If I start a thread with some priority, and use within ProcessBuilder it, the process will have the same priority as thread? 如果我启动一个具有某些优先级的线程,并在ProcessBuilder中使用它,那么该进程将具有与线程相同的优先级? Or is there some other way to do this? 或者还有其他方法可以做到这一点吗?

Cheers 干杯

There's no way to set priority on a process ( Process ) level in Java. 没有办法在Java中设置进程( Process )级别的优先级。

If I start a thread with some priority, and use ProcessBuilder within it, the process will have the same priority as thread? 如果我启动一个具有一定优先级的线程,并在其中使用ProcessBuilder ,该进程将具有与线程相同的优先级? Or is there some other way to do this? 或者还有其他方法可以做到这一点吗?

The process will run side by side with the JVM, so it will not inherit the threads priority. 该过程将与JVM并行运行,因此它不会继承线程优先级。 It will be scheduled on it's own by the operating system. 它将由操作系统自行安排。

As stated above, there is no built in cross-platform way of tweaking the priority of a process, but there is a Thread.setPriority(int) though. 如上所述,没有内置的跨平台方式来调整进程的优先级,但是有一个Thread.setPriority(int) So perhaps you could do the work by the external program in a separate thread (instead of starting a new process) and use the setPriority method on this thread. 所以也许您可以通过外部程序在单独的线程中完成工作 (而不是启动新进程)并在此线程上使用setPriority方法。

Related questions / answers: 相关问题/答案:

You could write a C/C++ DLL and export a JNI function that calls SetPriorityClass 您可以编写C / C ++ DLL并导出一个调用SetPriorityClass的JNI函数

You could then use this in your java code 然后,您可以在Java代码中使用它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM