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

前端 未结 5 1420
离开以前
离开以前 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:29

    You can use

    Runtime.getRuntime().availableProcessors()
    

    But its more of a best guess and even mentioned by the API

    This value may change during a particular invocation of the virtual machine. Applications that are sensitive to the number of available processors should therefore occasionally poll this property and adjust their resource usage appropriately.

提交回复
热议问题