Is there a way in Java to find out how many CPUs (or cores) are installed?

前端 未结 5 1395
离开以前
离开以前 2021-02-14 00:46

I want to make some tuning for multithreaded program.

If I know how much threads can really work in parallel I can make the program much more effective.

Is there

5条回答
  •  失恋的感觉
    2021-02-14 01:47

    How about (code snippets speak a 1000 words):

     public class Main {
    
     /**
      * Displays the number of processors available in the Java Virtual Machine
      */
     public void displayAvailableProcessors() {
    
        Runtime runtime = Runtime.getRuntime();
    
        int nrOfProcessors = runtime.availableProcessors();
    
        System.out.println("Number of processors available to the Java Virtual Machine: " + nrOfProcessors);
    
     }
    
     public static void main(String[] args) {
        new Main().displayAvailableProcessors(); 
     }
    }
    

提交回复
热议问题