Thread pool Executor in java

Advantages of thread pool:

Reduce resource consumption: reduce the consumption caused by thread creation and destruction by reusing the created threads

Improve response speed: When the task arrives, the task can be executed immediately without waiting for the thread to pass through

Improve the manageability of threads: threads are scarce resources. If they are created unlimitedly, they will not only consume system resources but also reduce the stability of the system. The thread pool can be used for uniform allocation, tuning and monitoring

Implementation principle:

After submitting a task to the thread pool:

1) The thread pool determines whether the threads in the core thread pool are all performing tasks. If it is not, create a new worker thread to perform the task. If the threads in the core thread pool are performing tasks, enter the next process

2) The thread pool judges whether the work queue is full. If it is not full, store the newly submitted task in this work queue. If the work queue is full, go to the next process

3) The thread pool determines whether the threads of the thread pool (the maximum number of threads) are all in a working state. If not, create a new worker thread to perform the task. If it is full, hand it over to the saturation strategy

Parameter Description:

corePoolSize: The size of the thread pool. The thread will be created when a task is submitted to the thread pool, even if other threads are idle, the new operation will stop when the task to be executed is greater than this value.

maximumPoolSize: the maximum number of threads allowed to be created in the thread pool;

runnableTaskQueue: Task queue, used to store tasks waiting to be executed, respectively: ArrayBlockingQueue, LinkedBlockingQueue, SynchronousQueue, PriorityBlockingQueue (use bounded queues as much as possible, unbounded queues will have the risk of memory overflow)

threadFactory: The thread factory is used to set the factory for creating threads. Just implement the ThreadFactory interface

rejectedExecutionHandler: Saturation strategy. When the thread pool and queue are full, the new task adopts the saturation strategy, which has the following values:
          AbortPolicy: throw an exception. 
          DiscardPolicy: Discard the task without throwing an exception. 
          DiscardOldestPolicy: Discard the first task in the queue and execute the current task
          CallerRunsPolicy: The task is processed by the calling thread

keepAliveTime: idle thread survival time. By default, when the number of threads is greater than corePoolSize, this value will work on those threads that exceed corePoolSize.

TimeUnit: The unit of idle thread survival time, optional units are days (DAYS), hours (HOURS), minutes (MINUTES), and seconds (SECONDS)

Submit task:

To submit a task to the thread pool, call the execute() or submit() method

execute(): no return value, the input parameter must be an instance that implements the Runnable interface

submit(): There is a return value, it returns a future type object. Call the get() method to get the return value. The input parameter must be an instance that implements the Callable interface

ScheduledThreadPoolExecutor:

ScheduledThreadPoolExecutor can be used to execute asynchronous tasks or periodically execute tasks after a given delay, using an unbounded queue (DelayQueue), so setting the maximumPoolSize has no effect. When calling scheduleAtFixedRate()和scheduleWithFixedDelay()方法, adding tasks to the queue, threads in the thread pool from the queue Get the task in and execute it

Timed tasks in Spring are implemented using ScheduledThreadPoolExecutor, and there is only one thread in the thread pool, only one, only one!

 

Guess you like

Origin blog.csdn.net/qq_42407917/article/details/111603316