The last time we had a quick look at what is the thread pool and several types of thread pools in Java, let's delve into the principles of the thread pool today. 1. Construction
1. Thread pool Manager (Threadpoolmanager): for creating and managing thread pools
2. Worker threads (workthread): Thread pool threads
3. Task interfaces (tasks): the interfaces that each task must implement for the work thread to schedule tasks to execute.
4. Task queue: For storing tasks that are not processed. Provides a buffer mechanism.
Note: The thread pool manager has at least the following features: Create thread pool, destroy thread pool, add new task 2, thread state
New State (new): a newly created thread object.
ready State (Runnable): After the thread object is created, other threads call the object's start () method. The thread of this state is in the running thread pool, becomes operational, and waits for access to the CPU.
running State (Running): A ready-state thread acquires the CPU and executes the program code.
blocking State (Blocked): blocking state is the thread for some reason to give up the use of CPU, temporarily stop running. Until the thread enters the ready state, there is an opportunity to go to the running state. The blocking situation is divided into three types:
(i), wait for blocking: The running thread executes the wait () method, and the JVM puts the thread into the waiting pool.
(ii), synchronous blocking: When a running thread acquires a synchronized lock on an object, the JVM puts the thread into the lock pool if the synchronization lock is occupied by another thread.
(iii), other blocking: The running thread executes the sleep () or join () method, or when an I/O request is made, the JVM puts the thread into a blocking state. When the sleep () state times out, join () waits for the thread to terminate or timeout, or the I/O process completes, the thread is back in ready state.
death Status (Dead): The thread finishes or exits the run () method because of an exception, which ends the lifecycle. 3, the implementation process
a. A task commits, if the thread pool size does not reach corepoolsize, start a worker each time a thread executes immediately
b. If it is too late to execute, put the extra thread in the Workqueue and wait for the started worker to loop through the
c. If the queue Workqueue is full and has not been executed, start a new worker under Maximumpoolsize to loop through Workqueue
d. If you boot to maximumpoolsize and the task comes in, the thread pool is full load, and the task rejects the Rejectedexecutionhandler
Process is: did not reach corepoolsize, create worker execution, reach corepoolsize join Workqueue//Workqueue full and under Maximumpoolsize, create new worker, reach Maximu Mpoolsize, execute reject public void execute (Runnable command) {if (command = = null) throw new NullPointerException
(); 1:poolsize reach Corepoolsize, perform 3 to join the task Workqueue//2:poolsize not reached, execute addifundercorepoolsize () Create new worker within Corepoolsize immediately execute task//If Corepoolsize is reached, IBID. executes 3 if (poolsize >= corepoolsize | |!addifundercorepool
Size (command) {//3:workqueue full, execute 5 if (runstate = = RUNNING && workqueue.offer (command)) { if (runstate!= RUNNING | | poolsize = = 0) {//4: If the thread pool is closed, execute the Deny policy//if poolsize==0,
New start a thread executes the task ensurequeuedtaskhandled (command) in the queue; }//5: Create a new worker within Maximumpoolsize immediately execute task//If Maximumpoolsize is reached, execute 6 deny policy} else if (!addif Undermaximumpoolsize (command)//6: Reject Policy reject (command); is SHutdown or Saturated}} 4. Thread Destroy
KeepAliveTime: Represents how long the thread is idle and then destroyed, and the thread is destroyed by the worker's Gettask ().
In general, the worker loops to get Gettask (), and if Gettask () returns NULL, worker thread worker ends, then we'll see when gettask () returns null
Runnable Gettask () {for (;;)
{try {int state = Runstate;
if (State > SHUTDOWN) return null;
Runnable R;
if (state = = SHUTDOWN)//help drain queue r = Workqueue.poll ();
else if (poolsize > Corepoolsize | | allowcorethreadtimeout)///at Poolsize greater than corepoolsize or allow core line blocks until those
Blocking timeout fetch is possible to get null, at which point the worker thread destroys r = Workqueue.poll (KeepAliveTime, timeunit.nanoseconds);
else R = Workqueue.take ();
if (r!= null) return R;
Here is the decision whether to run worker thread destruction if (Workercanexit ()) {if (runstate >= SHUTDOWN)
Stop or terminated state, terminate idle worker interruptidleworkers (); return null;
Here returns null for worker thread worker Destroy}//Others: Retry, resume loop} catch (Interruptedexception IE) { on INterruption, Re-check Runstate}}} 5. Thread pool shutdown
gently close shutdown: This method will place the runstate as shutdown, terminating all idle threads while not accepting new tasks, while still working threads are not affected, so the task person in the queue is executed
Close Shutdownnow Now: This method makes Runstate a stop, and the shutdown method is that this method terminates all threads (canceling all executing and not executing tasks), so the tasks in the queue are not executed. Summary:
Through this study, we not only know how the thread pool manages threads, but we also understand the transitions between threads in many states, which makes it easier for us to understand the thread pool. After that, we will continue to learn about multithreading, let us grow every day.