Common parameters of thread pool

        ​​​​A thread pool is a mechanism for managing and reusing threads, which can improve the performance and efficiency of multi-threaded applications. Commonly used thread pool parameters include:

  1. Number of core threads (corePoolSize): The number of active threads always maintained in the thread pool. Even if the thread is idle, it will not be recycled.

  2. Maximum number of threads (maximumPoolSize): The maximum number of threads allowed to exist in the thread pool. When the number of tasks exceeds the number of core threads and the work queue is full, the thread pool creates new threads to process tasks until the maximum number of threads is reached.

  3. Idle thread survival time (keepAliveTime): When the number of threads in the thread pool exceeds the number of core threads and the idle time exceeds the specified time, the excess threads will be destroyed to reduce resource consumption.

  4. Work Queue (workQueue): A queue used to store tasks waiting to be executed. Common work queue types include bounded queues (such as ArrayBlockingQueue) and unbounded queues (such as LinkedBlockingQueue). Bounded queues can limit the number of tasks in the thread pool and avoid memory overflow.

  5. Thread Factory (threadFactory): Factory class used to create new threads. By customizing the thread factory, you can configure threads in a personalized way, such as setting thread names, priorities, etc.

  6. Rejection policy (rejectedExecutionHandler): When the thread pool and work queue are full and unable to process new tasks, the rejection policy defines how to handle these rejected tasks. Common rejection strategies include throwing exceptions, discarding tasks, discarding the oldest tasks, and caller running.

おすすめ

転載: blog.csdn.net/chaojichunshen/article/details/132419510