IO intensive thread pool size setting

  java

Type judgment (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.

IO intensive thread size

/**
 * Support class for thread pool size
 * 
 * @author Nadeem Mohammad
 *
 */
public final class ThreadPoolUtil {
    
    private ThreadPoolUtil() {
        
    }
    /**
     * Each tasks blocks 90% of the time, and works only 10% of its
     *    lifetime. That is, I/O intensive pool
     * @return io intesive Thread pool size
     */
    public static int ioIntesivePoolSize() {
        
        double blockingCoefficient = 0.9;
        return poolSize(blockingCoefficient);
    }

    /**
     * 
     * Number of threads = Number of Available Cores / (1 - Blocking
     * Coefficient) where the blocking coefficient is between 0 and 1.
     * 
     * A computation-intensive task has a blocking coefficient of 0, whereas an
     * IO-intensive task has a value close to 1,
     * so we don't have to worry about the value reaching 1.
     *  @param blockingCoefficient the coefficient
     *  @return Thread pool size
     */
    public static int poolSize(double blockingCoefficient) {
        int numberOfCores = Runtime.getRuntime().availableProcessors();
        int poolSize = (int) (numberOfCores / (1 - blockingCoefficient));
        return poolSize;
    }
}

Use

ExecutorService executorService = Executors.newFixedThreadPool(ThreadPoolUtil.ioIntesivePoolSize());

This semantic setting is more expressive.

doc