Search code examples
javamultithreadingmaventhread-priority

Decrease priority of maven build threads/processes


I use parallel build of my maven project.

Usually I run mvn install -T 4 (I have 4 cores on my PC).

After that I must wait almost 5-10 minutes to fully build project. I can't do anything on my Windows workstation: mouse/keyboard is not responsive. CPU load = 100%.

Is there any way to decrease priority of threads/processes(javac) produced by maven?

As I understand if I set priority below normal - OS will schedule less time for maven build and more for other programs.


Solution

  • The comment suggesting to make use of less than 4 cores so that you have free cores to do other things represents a misconception. Under normal circumstances, your cores are doing mostly nothing but waiting, so it is pointless to reserve some of them to be just sitting there doing nothing, while maven has work to do. The point is, as you have already guessed, to let the maven process utilize as much CPU power as your machine can give, and to reduce the priority of the maven process, so that when you want to interact with your computer while maven is running, your interactions get higher priority than maven, so they complete quickly, and immediately afterwards maven is free to proceed with its own thing using the full capacity of your CPU.

    So, under windows, at the command prompt, start /belownormal mvn install -T 4 will do it.

    Better yet, if your CPU is sufficiently modern, (and it probably is,) and if you don't want to have to worry about the question of precisely how many cores you have, (you probably don't,) then use start /belownormal mvn install -T1C to instruct maven to figure out how many cores you have, and spawn one thread per core. This is preferable because it allows for hyper-threading: Your CPU may only have 4 cores, but these cores may be capable of hyper-threading with 2 threads per core, so maven will spawn 8 threads.

    Type help start for more options about the built-in 'start' command of Windows which gives you control over the priority of applications that you launch via the command-line. For example, you can specify /low instead of /belownormal; it may yield even better responsiveness, and quite possibly for no perceptible slowdown of the mvn process.

    The only situation under which it may make sense to reserve cores is if you want to run other CPU-intensive tasks in parallel with maven, (you probably don't,) and you don't trust the Windows scheduler to do a good job in orchestrating this. (You should.)