Use of thread pool
Thread resources should not be used directly or explicitly in the program, but should be provided by the thread pool
1. Advantages of using thread pool
(1) Using new Thread to create a thread has great disadvantages
1 . new Thread() is required to create objects every time a thread is used, and the performance is poor
2 . Lack of thread management may create threads indefinitely, resulting in system crash and oom
3. Single thread function and lack of other settings
(2) Advantages of thread pool
(1) Greatly reduce resource consumption, reuse threads in the thread pool, avoid thread creation and destruction, and save time and interval;
(2) Improve the system response speed. When a task arrives, it can be executed immediately by reusing the existing thread without waiting for the creation of a new thread;
(3) It is convenient to control the concurrent number of threads. If threads are created without restriction, it may lead to excessive memory occupation and OOM, and excessive cpu switching (cpu switching threads has time cost (it is necessary to maintain the current thread execution site and restore the site to be executed).
(4) Provide more powerful functions, delay timing thread pool
Thread pool class diagram
2. Main parameters of thread pool
/** * Creates a new {@code ThreadPoolExecutor} with the given initial * parameters and default thread factory and rejected execution handler. * It may be more convenient to use one of the {@link Executors} factory * methods instead of this general purpose constructor. * * @param corePoolSize the number of threads to keep in the pool, even * if they are idle, unless {@code allowCoreThreadTimeOut} is set * @param maximumPoolSize the maximum number of threads to allow in the * pool * @param keepAliveTime when the number of threads is greater than * the core, this is the maximum time that excess idle threads * will wait for new tasks before terminating. * @param unit the time unit for the {@code keepAliveTime} argument * @param workQueue the queue to use for holding tasks before they are * executed. This queue will hold only the {@code Runnable} * tasks submitted by the {@code execute} method. * @throws IllegalArgumentException if one of the following holds:<br> * {@code corePoolSize < 0}<br> * {@code keepAliveTime < 0}<br> * {@code maximumPoolSize <= 0}<br> * {@code maximumPoolSize < corePoolSize} * @throws NullPointerException if {@code workQueue} is null */ public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) { this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, Executors.defaultThreadFactory(), defaultHandler); }
1, corePoolSize (basic size of thread pool): when submitting a task to the thread pool, if the number of threads created by the thread pool is less than corePoolSize, even if there are idle threads at this time, a new thread will be created to execute the task until the number of threads created is greater than or equal to corePoolSize (in addition to submitting a new task to create and start threads) (constructed on demand), you can also start the basic threads in the thread pool in advance through the prestartCoreThread() or prestartAllCoreThreads() methods.)
2. maximumPoolSize: the maximum number of threads allowed by the thread pool. When the queue is full and the number of threads created is less than maximumPoolSize, the thread pool will create new threads to execute tasks. In addition, for unbounded queues, this parameter can be ignored.
3. keepAliveTime: when the number of threads in the thread pool is greater than the number of core threads, if the idle time of the thread exceeds the thread survival time, the thread will be destroyed until the number of threads in the thread pool is less than or equal to the number of core threads.
4. workQueue: a blocking queue used to transmit and save tasks waiting to be executed.
5. threadFactory (thread factory): used to create new threads. Threads created by threadFactory also adopt the method of new Thread(). Thread names created by threadFactory have a unified style: pool-m-thread-n (M is the number of thread pool and N is the number of threads in thread pool).
5. handler (thread saturation Policy): when the thread pool and queue are full, adding a thread will execute this policy.
There are several ways to create a thread pool
1 Executors.newFixedThreadPool(); Create a fixed size thread pool;
2 Executors.newCachedThreadPool() creates a new thread as needed. If the previous thread can still be used, use the existing thread. If there are no available threads in the thread pool, create a new thread and add it to the thread pool. If the thread is not used after 60s, terminate and remove the thread from the cache. Maintaining the thread pool for a long time will not consume resources
3 Executors.newScheduledThreadPool() creates a thread pool for delayed execution, which can specify how long to execute the task;
4 Executors.newSingleThreadExecutor() creates a thread pool for a single thread