Introduction and advantages of thread pool
1, the introduction
- What a thread pool does is basically control the number of threads running, queue tasks to be executed during processing, and then start those tasks after the thread is created. If the number of tasks to be executed exceeds the maximum number of threads in the thread pool, you need to wait for other threads to complete before you can continue allocating threads.
2, advantages
Main features: Thread reuse; Control the maximum number of concurrent requests; Management of thread
- Reduce resource consumption. Reduce the system cost of thread creation and destruction by reusing created threads
- Improve response speed. When the task arrives, the task can use an existing thread to execute the task instead of waiting for the thread to be created
- Improve thread manageability. Threads are scarce resources, and if created unrestrictively, not only consume system resources, but also reduce system stability. Using thread pools can be further allocated, tuned, and monitored
ThreadPoolExecutor parameter description
Java provides a class called ThreadPoolExecutor to define a thread pool for us. The constructor of this class takes seven parameters. Details are as follows:
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler) {
if (corePoolSize < 0 ||
maximumPoolSize <= 0 ||
maximumPoolSize < corePoolSize ||
keepAliveTime < 0)
throw new IllegalArgumentException();
if (workQueue == null || threadFactory == null || handler == null)
throw new NullPointerException();
this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue;
this.keepAliveTime = unit.toNanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
}
Copy the code
The following describes the seven parameters of the thread pool in terms of the bank counter transaction process.
The description of the bank counter is as follows: 2 fixed Windows, 3 active Windows and 10 seats in the reception area
The working process is as follows:
- 2 fixed Windows to receive guests,
- When there are many guests, sit in the reception area first.
- If the reception area is full, open an activity window to receive guests
- If the fixed window, reception area and active window are full of people, the new guests will be notified to come back next time.
- If there are no guests for a period of time, remove the active window, by the fixed window continue to receive guests.
1.CorePoolSize: indicates the number of core threads2Fixed Windows2.MaximumPoolSize: indicates the maximum number of threads2Fixed Windows +3Active Windows3.KeepAliveTime: indicates the keepAliveTime of idle threads4.TimeUnit: TimeUnit5.WorkQueue: Indicates the queue in which tasks are queued6.ThreadFactory: Factory for creating threads7.RejectedExecutionHandler: reject the policy. Yes4Kind of. <1< p style = "max-width: 100%; clear: both;2> return the submitted task to the main thread <3> Discards the longest waiting task in the queue <4> < p style = "max-width: 100%; clear: bothCopy the code
Thread pools provided by JUC
The source code for the thread pool provided by the JDK is posted below. None of the following thread pools are useful, and if you do use them, you should manually call the Constructor of ThreadPoolExecutor
1, newCachedThreadPool
public static ExecutorService newCachedThreadPool(a) {
return new ThreadPoolExecutor(0.2147483647.60L,
TimeUnit.SECONDS,
new SynchronousQueue());
}
Copy the code
2, newFixedThreadPool
public static ExecutorService newFixedThreadPool(int var0) {
return new ThreadPoolExecutor(var0,
var0,
0L,
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue());
}
Copy the code
3, newScheduledThreadPool
public ScheduledThreadPoolExecutor(int corePoolSize) {
super(corePoolSize,
Integer.MAX_VALUE,
0,
NANOSECONDS,
new DelayedWorkQueue());
}
Copy the code
4, newSingleThreadExecutor
public static ExecutorService newSingleThreadExecutor(a) {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1.1.0L,
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}
Copy the code
4. Determine the number of threads
1. CPU intensive
If most threads are used for CPU calculation, the number of threads can be set to CPU +1.
2. IO intensive
There are two strategies:
- The number of threads is 2 x number of cpus
- Number of cpus /(1- factor), the factor is generally between 0.8 and 0.9