Threads configuration based on no. of CPU-cores

前端 未结 8 1255
孤城傲影
孤城傲影 2020-11-27 13:10

Scenario : I have a sample application and I have 3 different system configuration -

- 2 core processor, 2 GB RAM, 60 GB HHD,
- 4 core processor, 4 GB RAM, 8         


        
8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-27 13:35

    You can get the number of processors available to the JVM like this:

    Runtime.getRuntime().availableProcessors()
    

    Calculating the optimal number of threads from the number of available processors is unfortunately not trivial however. This depends a lot on the characteristics of the application, for instance with a CPU-bound application having more threads than the number of processors make little sense, while if the application is mostly IO-bound you might want to use more threads. You also need to take into account if other resource intensive processes are running on the system.

    I think the best strategy would be to decide the optimal number of threads empirically for each of the hardware configuration, and then use these numbers in your application.

提交回复
热议问题