9

I have a small question about threads. On Unix systems, we have nice, which can be used to set priorities processes. 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.

If I start a thread with some priority, and use within ProcessBuilder it, the process will have the same priority as thread? Or is there some other way to do this?

Cheers

aioobe
  • 413,195
  • 112
  • 811
  • 826
caarlos0
  • 20,020
  • 27
  • 85
  • 160
  • Hi caarlos0, don't have a clear answer on this as I'm not familiar with the particular function you're trying to replicate but in the windows task manager you can right click a process and give it priority or processor affinity. – shaunhusain Jun 20 '11 at 19:02
  • 2
    Also found a few more potentially helpful links: http://www.javamex.com/tutorials/threads/priority_what.shtml http://slashdot.org/story/06/09/03/2231244/Permanently-Set-Process-Priority-in-Windows – shaunhusain Jun 20 '11 at 19:06

2 Answers2

5

There's no way to set priority on a process (Process) level in Java.

If I start a thread with some priority, and use ProcessBuilder within it, the process will have the same priority as thread? 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. 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. 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.

Related questions / answers:

Community
  • 1
  • 1
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • 2
    Just to add to this, this will only set priority on the thread in the Java Virtual Machine. You can't affect the threads priorities from the OS level. – Marcelo Jun 20 '11 at 19:07
4

You could write a C/C++ DLL and export a JNI function that calls SetPriorityClass

You could then use this in your java code

Adrian Regan
  • 2,240
  • 13
  • 11