Will a multi-threaded Java application use the multi-core machine really well?

If I write a multi-threaded java application, will the JVM take care of using all available cores? Do I have to do some work?

+2
performance multithreading jvm multicore
source share
4 answers

If you do not use the JVM with the so-called "green" threads (these days it is very small), Java threads are executed by OS threads, so several threads on different kernels start differently.

+10
source share

For tracking, I see 100% usage on both cores when I run this code on my dual core. If I give the number of threads from two to one, one core goes to 100%, and the other about 4%.

package test; import java.util.ArrayList; public class ThreadTest { public void startCPUHungryThread() { Runnable runnable = new Runnable(){ public void run() { while(true) { } } }; Thread thread = new Thread(runnable); thread.start(); } public static void main(String[] args) { ThreadTest thread = new ThreadTest(); for (int i=0; i<2; i++) { thread.startCPUHungryThread(); } } } 
+4
source share

All modern JVMs will use as many cores as possible than your hardware. An easy way to illustrate this is to download and run the DaCapo test on your computer. The lusearch test uses 32 threads. If you run this on your desktop or server, you should see that all your processors have suffered 100% utilization during the test.

+2
source share

On the contrary, it is sometimes useful to "bind" / set an affinity for the Java process in order to use only a set of cores / sockets, although this is done through the semantics of the OS. As previously reported, most real-time sessions use all processors and high-speed applications can consume more resources than might be expected.

0
source share

All Articles