java thread pool summary

Thread pool
1, classification
1), a fixed thread pool
ExecutorService executorService = Executors.newFixedThreadPool(10);

2), a thread pool
ExecutorService executorService = Executors.newSingleThreadExecutor();

3), a thread pool that creates threads as needed, without core threads pool. Each time
ExecutorService executorService = Executors.newCachedThreadPool();

these three are implemented with ThreadPoolExecutor. The main parameters of creating ThreadPoolExecutor are:
int corePoolSize core thread pool size, this thread is always alive, will not destroy
int maximumPoolSize maximum thread pool size, if core threads are used up, a thread will be created when there is another task, the total number of threads cannot be greater than this number.
long keepAliveTime is greater than the number of core threads threads, idle survival time
TimeUnit unit keepAliveTime time unit
BlockingQueue<Runnable> workQueue The queue for storing threads, when there are no extra threads to execute threads, threads are stored
ThreadFactory threadFactory Thread factory, used to create a worker thread
RejectedExecutionHandler handler rejection strategy, when the thread queue is full and then submit the task thread processing method.
For specific implementation, see the source code. Mainly focus on corePoolSize, maximumPoolSize, and BlockingQueue parameters.

4), CachedThreadPool is a thread pool that creates new threads as needed.
ExecutorService executor = Executors.newCachedThreadPool();

CachedThreadPool uses SynchronousQueue with no capacity as the work queue of the thread pool, but the maximumPool of CachedThreadPool is unbounded. This means that if the main thread submits tasks faster than the threads in the maximumPool can process tasks, the CachedThreadPool will keep creating new threads. In extreme cases, CachedThreadPool can exhaust CPU and memory resources by creating too many threads.

4) Scheduled thread pool ScheduledThreadPoolExecutor is a subclass of ThreadPoolExecutor and is

mainly used to run tasks after a given delay, or to execute tasks regularly. The function of ScheduledThreadPoolExecutor is similar to Timer, but ScheduledThreadPoolExecutor is more powerful and flexible. Timer corresponds to a single background thread, and ScheduledThreadPoolExecutor can specify multiple corresponding background threads in the constructor.

Definition:
class ScheduledThreadPoolExecutor 
        extends ThreadPoolExecutor
        implements ScheduledExecutorService

Create: ScheduledThreadPoolExecutor ScheduledExecutorService pool = Executors.newScheduledThreadPool(2); The queue used is: DelayedWorkQueue, which uses DelayQueue to encapsulate a PriorityQueue, which sorts ScheduledFutureTask in the queue. When sorting, the smaller time comes first (tasks with earlier time will be executed first). If the time of two ScheduledFutureTasks is the same, the sequenceNumber is compared, and the smaller sequenceNumber is ranked first (that is, if the execution time of the two tasks is the same, the task submitted first will be executed first). 2. Bounded blocking queue composed of ArrayBlockingQueue array Bounded blocking queue composed of LinkedBlockingQueue linked list unbounded blocking queue











Two-way blocking queue composed of LinkedBlockingDeque linked list 3.

Rejection policy
AbortPolicy throws an exception directly, the default policy.
CallerRunsPolicy uses the calling thread to run the task thread
DiscardOldestPolicy Discards the oldest unexecuted task in the queue, and then executes the executor to execute the submitted task
DiscardPolicy Directly discards the submitted task

These rejection strategies are all internal classes of ThreadPoolExecutor, and the source code is very simple.





Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326817202&siteId=291194637