After reading this article you will understand:
What is the process flow of the thread pool thread pool Save the blocking queue for the task to be performed create your own thread pool JDK provides thread pooling and usage scenarios Newfixedthreadpool newsinglethreadexecutor newcachedthreadpool Newscheduledthreadpool two methods of submitting tasks execute submit close the thread pool how to select or configure the summary
what is a thread pool
Thread pool concept Everyone should be very clear, help us to repeat the management of threads, avoid creating a large number of threads to increase overhead.
In addition to lowering overhead, the thread pool can also increase the response speed, understand the point JVM students may know that the creation of an object might need to go through the following steps: After checking that the corresponding class has been loaded, parsed, and initialized, the memory allocated to the new object is initially 0 Set the key information for the object, such as the hash code of the object, and then execute the Init method to initialize the object
The cost of creating an object needs to go through so many steps, and it takes time to reuse the thread pool of threads that have already been created and, naturally, to make a contribution to improving response speed. Process flow of thread pool
Creating a thread pool requires the use of the Threadpoolexecutor class, whose constructor parameters are as follows:
Public threadpoolexecutor (int corepoolsize, //number of core threads
int maximumpoolsize, //MAX thread number
long KeepAliveTime///////////////////////////////////
timeunit
Workqueue,// save queue Threadfactory threadfactory for tasks to be performed
, //Create a factory for new threads to use
Rejectedexecutionhandler Handler//When the task cannot be executed (processor
) {...}
1
2
3
4
5
6 7 8 1 2 3 4 5 6-7 8
Introduction to parameters as shown in the note, to understand what these parameters are about, you need to understand the thread pool specific execution method Threadpoolexecutor.execute:
public void Execute (Runnable command) {if (command = = null) throw new NullPointerException ();
int c = Ctl.get ();
1. The current pool thread is less than the core number, a new thread executes the task if (Workercountof (c) < corepoolsize) {if (Addworker (command, True))
Return
c = Ctl.get (); //2. The core pool is full, but the task queue is not full, added to the queue if (IsRunning (c) && workqueue.offer (command)) {int recheck = Ctl.ge
T ();
if (! isrunning (Recheck) && Remove (command))//If this is turned off, reject task reject (command);
else if (workercountof (recheck) = = 0)//If the previous thread has been destroyed, create a new thread Addworker (null, FALSE); //3. The core pool is full, the queue is full, try to create a new thread else if (!addworker (command, False)) reject (command);
If creating a new thread fails, the thread pool is turned off or the thread pool is full, rejecting the Task 1 2 3 4 5 6 7
8 9 10 11 12 13 14 15 16 17
18
19 20 21 22 23 1 2 3 4
5 6 7 8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23
As you can see, the thread pool handles a task that is mainly divided into three steps, which are described in the code comment, and I explain it in plain and simple terms:
(Threads to employees, thread pool to a team, core pool compared to the core team of staff, the core pool compared to outsourced employees) there is a new demand, first look at the core staff to exceed the maximum number of core employees, there are places to recruit a core staff to do
Need to get global lock core employees have the most, HR does not give HC, then this demand has to be saved, put to the task list to be completed if the list has been piled up, the core staff basically did not have the opportunity to finish so many tasks, then find a outsourcing it
Need to get global lock if the number of core employees + outsourced staff is already the team can withstand the maximum number, there is no way, this demand cannot meet
With this picture, do you understand this process?
Because 1 and 3 new threads need to acquire global locks, this will severely affect performance. So threadpoolexecutor such a process is to execute the Execute () method as little as possible 1 and 3, more than 2.
After Threadpoolexecutor completes the warm-up (the current number of threads is not less than the core number of threads), almost all execute () is performed in step 2.
The parameters of the Threadpoolexecutor constructor mentioned earlier affect the following: Corepoolsize: Number of core thread pools
When the number of threads is less than the core quantity, a new task comes in to create a new thread, even if some threads are nothing but the core number will not create a new thread, the idle thread has to go to the task queue to take the task to execute the Maximumpoolsize: Maximum number of threads
Include number of core thread pools + outside of core if the task queue is full and the number of threads in the pool is less than the maximum number, a new thread will be created to perform the task KeepAliveTime: Thread survival time outside the core pool, that is, no task outsourcing survival time
If the thread pool is set to Allowcorethreadtimeout (true), the core thread will also have a countdown to death on its head if the task is more and easier to execute, you can increase the parameter so that the thread can be more likely to accept new tasks in the time of its existence. Workqueue: Save blocked queues for pending tasks
Different task types have different choices, and the next section describes threadfactory: where each thread is created
can give the thread a good name, set a priority of what handler: saturation strategy, we are very busy, how to do it, there are four kinds of strategies
Callerrunspolicy: As long as the thread pool is not closed, run the task directly with the caller's thread AbortPolicy: Throw rejectedexecutionexception exception directly discardpolicy: Quietly release the Mission, Do not do the Discardoldestpolicy: the queue to stay the longest task to throw away, and then call execute () try to do it. We can also implement our own Rejectedexecutionhandler interface customization policies, such as logging or something. to save a blocking queue for a pending task
When the number of core threads in the thread pool is full, the task is saved to the queue.
The queues used in the thread pool are blockingqueue interfaces, and common implementations include the following: Arrayblockingqueue: Based on array, bounded, sorted by FIFO (first-in first Out) Linkedblockingqueue: Based on the list, Sort elements by FIFO (first-in first Out)
Throughput is typically higher than arrayblockingqueue executors.newfixedthreadpool () using this queue synchronousqueue: Blocking queues that do not store elements
Each insert operation must wait until another thread calls the removal operation, otherwise the insert operation is always blocked and the throughput is usually higher than linkedblockingqueue Executors.newcachedthreadpool used this queue Priorityblockingqueue: An infinite blocking queue with priority
For more information on blocking queues look at this: create your own thread pool
Once we know what we've got, we can create our own thread pool.
① first defines the values of several key properties of the thread pool:
private static final int core_pool_size = Runtime.getruntime (). Availableprocessors () * 2; The number of core threads is the number of CPUs *2
private static final int maximum_pool_size =; Thread pool maximum number of threads
private static final int keep_alive_time = 1; Stay alive for 1 seconds
1
2
3
1
2 3
Set the number of core pools is twice times the number of CPUs, generally 4, 8, better 16 threads maximum number of threads set to 64 idle thread survival time set to 1 seconds
② then select a different blocking queue based on the task type being processed
If high throughput is required, the Synchronousqueue queue can be used, and if the order of execution is required, the priorityblockingqueue can be used, and linkedblockingqueue can be used if the maximum backlog of tasks is capped.
Private Final blockingqueue<runnable>