Java thread pool configuration principles

  java, java-ee, thread

Cpu-intensive orIOio-intensive or hybrid

It depends on whether the application is CPU-intensive, IO-intensive or hybrid.

  • CPU intensive
    If the CPU is intensive, the number of CPU processors is generally configured to +/-1 thread. The so-called CPU intensive refers to the system doing normal calculation tasks of the program most of the time, such as numerical calculation, assignment, memory allocation, memory copy, loop, lookup, sorting, etc. All these processes need CPU to complete.

  • IO intensive
    IO-intensive means that the system interacts with I/O most of the time, and this time thread does not occupy CPU to process, that is, other threads can use CPU within this time range, so more threads can be configured.

  • mixed type
    If mixed, it means both occupy a certain amount of time.

Empirical formula for estimation

  • Ncpu = number of cpus

  • UCPU= target cpu usage

  • W/C= ratio of waiting time to calculation time

In order to keep the processor at the desired usage rate, the optimal thread pool size is equal to


Observe thread pool statistics

In Java, the amount of cpu available can be obtained through runtime.getruntime (). availableprocessors ().

For specific thread pool size observation, thread pool can be monitored and wait queue size can be checked by covering the hook method of executor, mainly observing the size of wait queue and the number of rejected tasks.