Official account: Java Xiaokaxiu, website: Javaxks.com

The author: xiao zhi early wood, link: blog.csdn.net/o9109003234/article/details/109523691

1. Java thread pool

① Benefits of using thread pools properly
  • Java’s thread pool is the most widely used concurrency framework, and can be used by almost any program that needs to perform tasks asynchronously or concurrently.
  • The benefits of using thread pools properly:
  1. Reduce resource consumption.throughReuse the threads already createdTo reduceThread creation and destructionThe resulting consumption. For example, the worker thread Woker wirelessly loops over tasks in a blocking queue to execute.
  2. Improve response speed.When the mission arrived,Tasks can be executed immediately without waiting for a thread to be created.
  3. Improve thread manageability.Threads are scarce resources, and Java’s thread pool can be used for thread resourcesUniform distribution,tuningandmonitoring.
② Workflow of thread pool
  • When a new task is added to the thread pool, the thread pool process is as follows: 1.Thread pool Determines whether all threads in the core thread pool are executing tasks.If not,Create a new worker thread to execute the task. If all the threads in the core thread pool are executing tasks, the next process goes on.
  1. The thread pool determines whether the blocking queue is full. If the blocking queue is not full, the newly submitted task is stored in the blocking queue. If the blocking queue is full, the next process is entered.

  2. Thread pool Determines whether all threads in the thread pool are working.Copy the code

    If not, a new worker thread is created to execute the task. If it is full, the saturation policy is handed over to handle the task.

  • The core implementation class for thread pools is

    ThreadPoolExecutor classCopy the code

    To perform the submitted task. Therefore, when a task is submitted to the thread pool, the specific processing flow is determined by

    ThreadPoolExecutor classCopy the code

    the

    The execute () method

    To get it done.

  1. If fewer threads are currently runningcorePoolSize, a new worker thread is created to execute the task (To perform this step, you need to acquire the global lock).
  2. If the currently running thread is greater than or equal tocorePoolSizeAnd,BlockingQueueIf not, add the task toBlockingQueueIn the.
  3. ifBlockingQueueIs full, and the currently running thread is less thanmaximumPoolSize, a new worker thread is created to execute the task (To perform this step, you need to acquire the global lock).
  4. If the currently running thread is greater than or equal tomaximumPoolSize.The mission will be rejectedAnd callRejectExecutionHandler. RejectExecution () method. namelyA saturation policy is invoked to process the task.
  • Worker thread:A thread pool encapsulates a thread as a thread when it is createdWorker thread Woker. Instead of destroying the Woker immediately after the missionLoop to get tasks in the blocking queue to execute.
③ Create a thread pool (7 parameters)
  • Can be achieved byThreadPoolExecutorTo create a thread pool:
new ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, 
	TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler)
Copy the code
  • CorePoolSize (base size of thread pool) :
  1. When a task is submitted to the thread pool, the thread pool creates a new thread to execute the task.Note:Even if there isThe idle base threadThe task can be executed, and a new thread is created.
  2. If the number of threads in the thread pool is greater than or equal tocorePoolSize, no new thread is created.
  3. If the thread pool’sPrestartAllCoreThreads () method, thread pools doCreate and start all base threads ahead of time.
  • MaximumPoolSize (maximum number of thread pools) : The maximum number of threads allowed to be created in a thread pool.
  1. The blocking queue is full and the number of threads is less thanmaximumPoolSizeA new thread can be created to execute the task.
  2. ifUse unbounded blocking queues.Does this parameter have any effect.
  • WorkQueue: A blocking queue that holds tasks waiting to be executed.
  1. ArrayBlockingQueue:Array structure-basedBounded blocking queuesAccording to theFIFO (first in first out) principlePrioritize tasks. Using this queue, the maximum number of threads that can be created in the thread pool ismaximumPoolSize.
  2. LinkedBlockingQueue:Based on the linked list structureUnbounded blocking queueAccording to theFIFO (first in first out) principleSort tasks, throughput is higher thanArrayBlockingQueue. Using this queue, the maximum number of threads that can be created in the thread pool iscorePoolSize.Static factory method Executor.newFixedThreadPool()This queue is used.
  3. SynchronousQueue will:aNot storing elementsBlocking queue of.Adding a task must wait until another thread removes it.Otherwise, the add operation remains blocked.Static factory method Executor.newCachedThreadPool()This queue is used.
  4. PriorityBlokingQueue:aSupported prioritytheUnbounded blocking queue. Using this queue, the maximum number of threads that can be created in the thread pool iscorePoolSize.
  • KeepAliveTime (hold time for thread activity) :The thread poolAfter the worker thread is idle.The amount of time that you stay alive. ifThere are many tasks and the execution time of the tasks is relatively short, you canThe bigkeepAliveTimeTo improve thread utilization.
  • Unit (thread activity hold time unit) :Optional units are availableDAYS,HOURS,MINUTES,ms,microseconds,seconds.
  • Handler (saturation policy, or rejection policy) : When both the queue and the thread pool are full, i.e., the thread pool is saturated, a policy must be adopted to handle submitted new tasks.
  1. AbortPolicy:When you can’t handle a new task,Throw an exception directly, this isThe default policy.
  2. CallerRunsPolicy:Execute the task with the caller’s thread.
  3. DiscardOldestPolicy:Discards the blocking queueThe topA task and executes the current task.
  4. DiscardPolicy:The task is discarded.
  • ThreadFactory: The factory class for building threads
  • Conclusion:
  1. Core pool, maximum pool, idle time, unit of time, blocking queue; The other two: reject policy, thread factory class
  2. Common thread pool creation parameters are as follows.PS: CachedThreadPoolThe core pool is 0 and the maximum pool isInteger.MAX_VALUE, which is equivalent toOnly the largest pool is used; Other thread pools, the core pool is as large as the largest pool, and therefore equivalent toOnly the core pool is used.
FixedThredPool: new ThreadExcutor(n, n, 0L, ms, new LinkedBlockingQueue<Runable>()
SingleThreadExecutor: new ThreadExcutor(1, 1, 0L, ms, new LinkedBlockingQueue<Runable>())
CachedTheadPool: new ThreadExcutor(0, max_valuem, 60L, s, new SynchronousQueue<Runnable>());
ScheduledThreadPoolExcutor: ScheduledThreadPool, SingleThreadScheduledExecutor.
Copy the code
  1. If the blocking queue used is an unbounded queue, the rejection policy is never invoked because no amount of work can be placed in the queue.
  2. SynchronousQueueisDo not store tasksThe new task is either immediately executed by an existing thread or a new thread is created to execute it.
④ Submit tasks to the thread pool
  • useThreadPoolEXecutor.executor()Methods to submit tasks:
Public void execute(Runnable command) {// command is null, Throw NullPointerException if (command == null) throw new NullPointerException(); int c = ctl.get(); If (workerCountOf(c) < corePoolSize) {if (addWorker(command, true))) return; c = ctl.get(); } // Add the task to the blocking queue if (isRunning(c) &&workqueue.offer (command)) {int recheck = ctl.get(); if (! isRunning(recheck) && remove(command)) reject(command); else if (workerCountOf(recheck) == 0) addWorker(null, false); }// The blocking queue is full, try to create a new thread, if maximumPoolSize is exceeded, execute handler.rejectexecution () else if (! addWorker(command, false)) reject(command); }Copy the code
⑤ Five running states of the thread pool
  • RUNNING: The thread pool in this state can accept newly submitted tasks and process tasks in the blocking queue.
  • SHUTDOWN:The thread pool in this state ** cannot receive newly submitted tasks **, but can process tasks in the blocking queue **.(The government service hall no longer allows people to get numbers, so they can leave work after dealing with the affairs at hand and in line.) `
  1. In aRUNNING state, the callShutdown () methodCauses the thread pool to enter this state.
  2. Note: The finalize () methodIt also happens during executionImplicit callShutdown () method.
  • STOP:Thread pool for this stateNew submitted tasks are not acceptedAlso,Do not process tasks in blocking queues.It also interrupts ongoing tasks.(There was no service at the government service hall, no numbers, no lines, no work at hand.)
  1. In the thread poolRUNNING or SHUTDOWN status, the callShutdownNow () methodCauses the thread pool to enter this state.
  • TIDYING:ifAll missions have been terminated.WorkerCount =0
  1. Is called when the thread pool enters this stateTerminated () hook methodEnter theTERMINATED status.
  • TERMINATED:

    in

    Terminated () hook methodCopy the code

    After the execution, enter the state,

    Does the default terminated() hook method do nothing

    .

⑥ Thread pool closing (The shutdown or shutdownNow method)
  • You can call the thread poolThe shutdown or shutdownNow methodTo close the thread pool: Traverse the worker threads in the thread pool, calling them one by oneInterrupt methodtoInterrupt threads.
  • Features of Shutdown method and shutdownNow:
  1. Shutdown methodSets the state of the thread pool toThe SHUTDOWN state.Only idle worker threads are interrupted.
  2. ShutdownNow methodSets the state of the thread pool toThe STOP state.All worker threads are interrupted, regardless of whether the worker thread is idle.
  3. Calling either of these methods makesIsShutdown methodReturns true;After all tasks in the thread pool are closed.IsTerminated methodReturns true.
  4. Usually useShutdown methodTo close the thread pool, if the task is not required to completeShutdownNow method.

2. Java thread pool tuning and monitoring

① Thread pool tuning (proper configuration of thread pool)
  • First, analyze the characteristics of tasks from the following perspectives:
  1. Nature of the task: CPU intensive task,IO intensive taskandMixed task.
  2. Task priority: high, medium, low.
  3. Task execution time: long, medium, short.
  4. Task dependencies: Whether to rely on other system resources, such asDatabase connection.
  • Tasks of different nature can be handled separately by thread pools of different sizes.Can be achieved byRuntime.getRuntime().availableProcessors()Method to obtain the number of cpus on the current device.
  1. CPU intensive taskconfigurationThe smallest possible threadFor example, configure a thread pool of Ncpu+1 N_{CPU}+1 Ncpu+1 thread.
  2. IO intensive taskBecause the thread is not executing tasks all the time, thenConfigure as many threads as possibleFor example, 2∗Ncpu 2*N_{CPU} 2∗Ncpu.
  3. Mixed task, if can be split, thenSplit it into one CPU intensive task and one IO intensive task. As long as theThe time difference between the two tasks is not too great, thenThe throughput of decomposed execution is higher than that of serial execution; If the execution time of the two tasks is too different, there is no need to break them down.
  • Tasks with different prioritiesPriority queues can be usedPriorityBlockingQueueIt allows high-priority tasks to be executed first. However, ifHigh priority tasks are always added to the blocking queue, thenLow-priority tasks may never be executed.
  • Perform tasks at different timesCan be handed over toThread pools of different sizesTo deal with, orPriority queues can also be usedAnd letPerform tasks that require a short execution time first.
  • Tasks that depend on database connection poolsSince the thread submits the SQL and waits for the database to return the result,The number of threads should be set to be largeIn order to better use the CPU.
  • A bounded queue is recommended, the bounded queue energyIncreased system stability and early warning capabilities. You can make it as big as you want, say thousands.Use unbounded queues, the queue of the thread pool gets bigger and bigger,It may fill up memory and cause the entire system to become unusable.
② Thread pool monitoring
  • The thread pool can be monitored by reading the thread pool parameters provided by the thread pool. The following properties can be used:
  1. TaskCount:The number of tasks that need to be executed by the thread pool, including completed, unexecuted, and executing tasks.
  2. CompletedTaskCount:The thread pool is runningNumber of completed tasks.completedTaskCount <= taskCount.
  3. LargestPoolSize:The thread poolMaximum number of threads ever created.This data lets you know if the thread pool is full.Is equal to the maximum size of the thread pool, it meansThe thread pool was full.
  4. getPoolSize:Number of threads in the thread pool. ifThread pools are not destroyedWords,Threads in the pool are not automatically destroyed, soThe number of threads in the thread pool only increases.
  5. GetActiveCount:To obtainActivities of theThe number of threads.
  • throughInherited thread poolandrewriteThe thread poolbeforeExecute.afterExecuteterminatedMethods we can use inBefore task execution.After performingandBefore the thread pool closesDo something.
  • For example, monitoring tasksAverage execution time.Maximum execution timeandMinimum execution timeAnd so on.These methods are empty methods in the thread pool, such as:
protected void beforeExecute(Thread t, Runnable r) { }
Copy the code

Common problems with Java thread pools

1. Talk about Java thread pools

  • Basic explanation:
  1. In order toThreadPoolExecutorFor the entry point, explainExcute () methodIs reflected inJava thread pools run processes.
  2. Worker thread Worker, its cycle work characteristics
  3. How to create a new thread pool: 7 parameters (focus on blocking queues and saturation policies)
  • Advanced:
  1. The thread poolFive state characteristicsAs well asHow do I switch between states:running,shutdown,stop,tidying,terminated.
  2. How to close a thread:Shutdown methodandShutdownNow methodThe characteristics of
  3. Thread pool tuning (For different characteristics of the task + A bounded queue is recommended)
  4. The thread poolMonitoring parametersAs well asMethods that can be overridden.
  • There are two main types of thread pools: normal thread poolsThreadPoolExecutor, a thread pool that supports deferred or periodically executed tasksScheduledThreadPoolExcutor.
  • Interpretation of theThreadPoolExcutorFive common parameters + two uncommon parameters, including three kinds of thread pools: parameters at creation time, running process, each suitable scenario.
  • Interpretation of theScheduledThreadPoolExecutorHow to change the time of a task.
  • Five defined thread pools are provided, all of which can be passedExecutorsUtility class to call, for exampleExecutors.newFixedThreadPool(12)

2. If corePoolSize is X, maximumPoolSize is Y, and blocking queue is Z, how will the WTH task be allocated?

3. How can thread pools be tuned?

  • Thread pool tuning (For different characteristics of the task + A bounded queue is recommended)

4. How to deal with core parameters in the thread pool if they exceed core size, how to deal with queue full, and what are the rejection policies? (More specific)