by BehindJava

How can an application use multiple cores or CPUs in Java

Home » java » How can an application use multiple cores or CPUs in Java

In this blog, we are going to learn about how application uses multiple cores or CPUs in Java.

The Task Manager is where you can specify which CPUs your programs can use and limit their usage on that CPU. Typically, this is only useful for debugging legacy programs which have problems with multi-threading.

To do this,

  • Run Task Manager. Click Processes in the left pane. Right click and choose Set Affinity…
  • Tick the checkboxes to ensure that the applications you are running will not affect the performance. Windows will only schedule threads from that process onto those particular CPUs.

It doesn’t make sense to quote me on that as this might be something that Windows itself does. Please do try it out yourself, though!

You can also do this programmatically in.NET after your program has launched using the System.Diagnostics.Process.ProcessorAffinity property, but I don’t think it will’remember’ the settings, so there will always be a short period in which your app is run on whichever CPU windows sees fit.

This is primarily useful for troubleshooting broken legacy software. However, if you’re a tech-savvy individual with an understanding of programming, you might find this book useful. This may not be a bad thing, so don’t change anything here. Your Windows settings are more important.

Note: This applies at the entire process level. If you set affinity for CPU0 only, and then launch 50 threads, all 50 of those threads will run on CPU0, and CPU1, 2, 3, etc will sit around doing nothing.

it’s often desirable to set threads as “critical”, so they’re prioritized over everything else. Use an example API: