Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does jvm do to use multicore cpu resource?

Usually a java program is running with a single process called "javaw".And when I run a single process I can only get max resource of one core(of multicores). But when I run a multithread program in jvm, the number of cores it used is according to the number of threads which is beyond one process can do. So can anyone give me some information about how jvm deal with multithread program in multicore cpu machine ?

/**
* I run this program in my machine which has 8 core cpu
* and the jre is 1.6.0_24
* How does jvm use one process to use all the cpu resources?        
*/

public class MultiCoreUseTest implements Runnable{
@Override
public void run() {

    int i;
    while(true)
         i =1;
}

public static void main(String[] args) {

    //create 8 threads
            //8 threads the usage of cpu is 100%
            // if 4 threads the usage of cpu is 50%
    for(int i = 0; i<8; i++){
        new Thread(new MultiCoreUseTest()).start();     
    }
}

}
like image 478
christophe Avatar asked Oct 08 '12 06:10

christophe


1 Answers

The threads of one process can be distributed across different CPUs and CPU cores, just as individual processes can. Just because an application has one process does not mean that its many threads are bound to one CPU/core.

like image 146
cdhowie Avatar answered Nov 05 '22 05:11

cdhowie