How to change the priority of a running java process?
Asked Answered
B

4

3

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

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.

Baler answered 4/6, 2011 at 20:17 Comment(4)
on Un*x you could call yet another external process to re-nice or set CPU affinity etc. seen that this can be done from the command line. No idea how things work on the Windows side of the Java pond that said :)Distrust
@Distrust in windows you need SetPriorityClass msdn.microsoft.com/en-us/library/ms686219%28v=vs.85%29.aspx you can invoke from JNI/JNA or some cmd-line utility like gilchrist.ca/jeff/SetPriority/index.htmlPeonage
@bestsss: thanks, interesting. Makes sense in a way: I take it that most API calls can be "wrapped" inside a command-line utility when/if it doesn't exist by default. :)Distrust
@SyntaxT3rr0r, dunno about most, some might require to be executed by the same process. I have not been doing WinAPI stuff for a decade (I think).Peonage
E
3

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.

Ebonyeboracum answered 4/6, 2011 at 20:34 Comment(0)
E
6

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)

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.

Excessive answered 5/6, 2011 at 7:33 Comment(6)
Good point, and thank you for the response. However the program I am writing runs and maintains many power-hungry processes. Running at 100% for days at a time is the ideal, but these are on my home PCs so it's nice to be able to use my comp from time to time. Thus the need for a dynamic priority change. The program may also be doing time sensitive computations at times and so the ability to change to higher priority if need be is also very practical.Baler
If you have time sensitive programs, the most important thing you can do is run the application on a machine by itself. Even if you have a real-time OS, you will get enormous jitter under load.Excessive
"All this only matters if there isn't enough CPU" is an inaccurate statement in regards to the question; process priority has to do with order of operation, not amount of CPU utilisation. Higher priority processes are able to consume events before they are passed to those of lower priority.Creech
@Creech only is those events happen at the same time. If there is a free cpu a thread can run regardless of priority. If there is not enough cpu you OS can make a choice.Excessive
"The system assigns time slices in a round-robin fashion to all threads with the highest priority. If none of these threads are ready to run, the system assigns time slices in a round-robin fashion to all threads with the next highest priority." Available here: msdn.microsoft.com/en-us/library/windows/desktop/…Creech
@Creech can you clarify what you are trying to say? That doesn't change what I said.Excessive
E
3

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.

Ebonyeboracum answered 4/6, 2011 at 20:34 Comment(0)
B
1

For Windows 10, you can still set low priority to the runninng process by deprecated WMIC command:

static void setSelfLowPrio(){
    try {
        Runtime.getRuntime()
                .exec(String.format("wmic process where processid=%d CALL setpriority \"idle\"", ProcessHandle.current().pid()));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

You can also set it to "low" or "below normal" if "idle" is not enough for your process.

Birth answered 22/11, 2021 at 12:54 Comment(0)
G
0

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

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

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.

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

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

Globate answered 2/7, 2014 at 11:59 Comment(2)
Thread.setPriority has absolutely nothing to do with Windows or Unix process priorities. This is just about the internal Java sheduler and changes when / how often a Thread will run when Java is active - but does not change how often Java is run incomparison to other processes, as the OP asks.Choriamb
Java (hotspot) doesn't have "internal Java scheduler" for thread management. It relies on OS for thread priorities. For example on Windows it uses SetThreadPriority WinAPI function - hg.openjdk.java.net/jdk/jdk/file/151b990e3764/src/hotspot/os/…Vasos

© 2022 - 2024 — McMap. All rights reserved.