views:

21

answers:

2

How to set my process PriorityClass to "realtime" on windows (xp and later is in my main intrest) and its analogs on Lin? What is crossplatform Java analog for Windows SetProcessClass?

+1  A: 

Using Runtime.exec() or ProcessBuilder, there is no support for setting process priority for started process.

Each OS manages priorities in different way, and some platforms where java runs don't have priorities at all (think embedded systems). Process priorities are a feature of the operating system, and each operating system has it's own unique methods of going about deciding who to schedule on the CPU.

Unix based systems use the renice utility, while windows based systems use the SetProcessClass method (as you're obviously aware). Other systems act differently, if process priority is supported at all. This means you would have to:

  1. Write a system detection routine to determine which operating system you reside on.
  2. Write an operating system specific routine to determine how to obtain your own process's identifier (required for changing the priority).
  3. Write an operating system specific routine to change your program's priority, probably by launching a script through the existing Runtime.exec() or ProcessBuilder interfaces.

Then at runtime, you could change your process's priority by having the Java program probe for the operating system, check to see if it is one of the "supported" changeable-priority systems, find the process id (if supported), and change the priority of that id (if supported).

The underlying reason that prevents a more portable solution is that process priorities are not a concept applied uniformly across all operating systems. You're going to have to find or build a solution which is not fully portable, but works on the platforms you care about.

Edwin Buck
So... I see your point. How to set my process PriorityClass to "realtime" on windows? (Java 6)
Kabumbus
A: 

There isn't a a platform neutral way to do that. What you can do is change the priority of specific Threads.

For example using:

Thread.currentThread().setPriority(Thread.MAX_PRIORITY);

You can change the current Thread's priority to the highest one in your priority class.

juancn
That sets the priority for the Thread inside of Java, not for the process. These two are not the same thing.
Edwin Buck