Interviewer: What are the construction methods and rejection strategies of the thread pool?

In Java, thread pool is a powerful tool for managing threads to improve performance and reduce resource consumption. This article will introduce the construction method of thread pool, the type of thread pool and the rejection strategy of thread pool.

How to build a thread pool

Java The thread pool in is mainly constructed through java.util.concurrent.ExecutorServiceinterfaces and their implementation classes . ThreadPoolExecutorThe following are several common construction methods:

  1. Use Executorsclass factory methods: ExecutorsThe class provides a series of static factory methods for creating thread pools, such as newFixedThreadPool, , newCachedThreadPooland newSingleThreadExecutor.

  2. Create ThreadPoolExecutoran instance directly: You can directly instantiate ThreadPoolExecutora class and pass in a series of parameters to create a thread pool, such as the number of core threads, the maximum number of threads, the survival time of idle threads, time units, task queues, etc.

Types of thread pools

Java The thread pool in can be divided into the following types:

  1. Fixed thread pool ( FixedThreadPool): The fixed thread pool has a fixed number of core threads, and the maximum number of threads is the same as the number of core threads. This thread pool is suitable for handling a fixed number of concurrent tasks.

  2. Cacheable thread pool ( CachedThreadPool): The number of core threads in the cacheable thread pool is 0, and the maximum number of threads is Integer.MAX_VALUE. When a new task arrives, if there is an idle thread, it will be reused, if not, a new thread will be created. Idle threads are recycled after a certain amount of time. This thread pool is suitable for scenarios where a large number of short-term tasks are performed.

  3. Single-threaded executor ( SingleThreadExecutor): A single-threaded executor has only one core thread, and the maximum number of threads is also 1. Tasks will be executed sequentially, which is suitable for scenarios that require serial execution of tasks.

  4. Timing thread pool ( ScheduledThreadPool): The timing thread pool allows timing or periodic execution of tasks. It inherits from ThreadPoolExecutorand implements ScheduledExecutorServicethe interface.

The rejection policy of the thread pool

线程池的拒绝策略当线程池中的线程达到最大线程数且任务队列已满时,线程池会采用拒绝策略来处理新到来的任务。Java 中的有以下几种:

  1. AbortPolicy(默认拒绝策略):直接抛出RejectedExecutionException异常,表示任务无法处理。

  2. CallerRunsPolicy:调用任务的提交者所在的线程来执行任务。这种策略可以降低新任务提交的速度,从而为线程池腾出时间处理已提交的任务。

  3. DiscardPolicy:该策略直接丢弃新到来的任务,不抛出异常。如果你可以容忍某些任务不被执行,这种策略可能会很有用。

  4. DiscardOldestPolicy:该策略会丢弃任务队列中等待时间最长的任务,然后尝试提交新任务。这种策略在任务队列可能存在低优先级任务时较为实用。

  5. 自定义拒绝策略:你还可以实现RejectedExecutionHandler接口来自定义拒绝策略。例如,可以记录被拒绝的任务,以便稍后分析和处理。

总结

线程池在 Java 中是一个强大的工具,可以帮助我们管理和控制线程的执行。了解不同类型的线程池及其适用场景以及拒绝策略可以帮助我们更有效地使用线程池。在实际应用中,我们应根据需求选择合适的线程池类型和拒绝策略,以实现高效、稳定的并发执行。

Guess you like

Origin juejin.im/post/7215424831209635901