[Concurrent programming - foundation] (viii) thread pool

Article Directory

A, new Thread drawbacks

  • Each time new Thread new objects, poor performance.
  • Thread the lack of unified management, unlimited possible new thread, compete with each other, it is possible to take up too many system resources could result in death or OOM (not just because a separate new Thread, there may be a defect in the design, continue to trigger new Thread).
  • The lack of more features, such as the implementation of more regular execution thread interrupts.

Second, the benefits of the thread pool

  • Reuse existing threads, do not always create a thread, reducing object creation, the demise of the cost, good performance.
  • It can effectively control the maximum number of concurrent threads, improving system resource utilization, and avoid excessive competition for resources, avoid blocking
  • Provides timing executed to perform regular, single-threaded, concurrent control functions

Third, the core thread pool class -ThreadPoolExecutor

3.1 three parameters, the thread pool

       3.1.1, corePoolSize (core number of threads)
       3.1.2, maximumPoolSize (thread maximum number of threads)
       3.1.3, workQueue (blocking queue)
  • For storing task waiting to be executed, very important, is the process of running a major impact on the thread pool
       3.1.4, keepAliveTime (thread does not hold up to the task when performing long time termination)
       3.1.5, unit (keepAliveTime unit of time)
       3.1.6, threadFactory (thread factory)
  • To create a thread (default will have a default factory to create a thread, a thread is created using the default factory, the newly created thread will have the same priority and non-daemon threads, but also set the name of the thread)
       3.1.7, rejectHandler (strategy of denial when processing tasks)

Four kinds of denial policy processing

1, direct throw an exception (default) AbrotPolicy;

2, with the caller's thread to perform the task CallerRunsPolicy;

3, dropped from the queue and execute tasks most forward current task DiscardOldstPolicy;

4, discards the task DisccardPolicy

3.2 thread pool work, job submission

       3.2.1, the parameters of the relationship between the thread pool to handle the task of job submission
  • If the number of threads running less than corePoolSize, will directly create a new thread to handle the task, even if other threads in the pool are idle.
  • If the number of threads in the thread pool greater than and less than equal to the value corePoolSize value maximunPoolSize only when workQueue full when it is creating a new thread to handle the task.
  • If corePoolSize and maximumPoolSize same, then create a thread pool size is fixed, this time if there are new user submission, and workQueue not put the request into full time to workQueue inside, waiting for a free thread, and then from the inside workQueue remove the task.
  • If the number of threads running greater than maximumpoolSize, if workQueue was full, it will specify the parameters for the policy of a policy to refuse to deal with this task by the introduction of the back.
  • So when the task is submitted, there are three main order, the first is corePoolSize, it is less than directly create threads and call. The next one is workQueue, the last is a maximumuPoolSize.
       3.2.2, when submitting a new job three treatments mode thread pool
  • workQueue is to hold a blocking queue waiting for task execution when we submit a new task to the thread pool after thread pool will be determined according to the number of the task processing thread pool threads are running the current mode, there are three treatment
(1) Direct switching (SynchronusQueue)
(2) the use of an unbounded queue (a LinkedBlockingQueue)
  • The maximum number of threads that can be created for the corePoolSize, then maximumPoolSize will not play a role.
  • When all of the core thread pool threads are running the state, the new mission will be submitted into the waiting queue.
(3) bounded queue (ArrayBlockingQueue with)
  • The maximum maximumPoolSize, can reduce the consumption of resources, but such a way that the thread pool thread scheduling becomes more difficult.
  • Because the thread pool and queue capacity is limited. So let throughput and processing tasks of the thread pool to a reasonable range, we want to make the thread scheduling is relatively simple, and also to minimize the consumption of resources, we need a reasonable limit both the number.

3.3 thread pool allocation techniques

  • To reduce the consumption of resources comprising reduce cpu utilization, the operating system consumes resources, the context switching overhead, and the like, may be provided a larger and a smaller thread queue capacity pool capacity, this will reduce the throughput of the thread pool.
  • If the task we have submitted often clogged, we can adjust maximumPoolSize. If our queue capacity is small, we need to set the size of the thread pool larger, so cpu usage will be relatively higher.
  • However, if the capacity of the thread pool is set too large, too many tasks to improve when the amount of concurrency increases, then the scheduling problem between threads is a need to consider. But this may reduce the throughput of processing tasks.

Fourth, the thread instance of state

  • These states do not deal with, it is an internal thread pool to do these treatments.

4.1、RUNNING

       You can receive a new task submission and processing tasks can be blocked in the queue.

4.2、SHUTDOWN

       It belongs to the closed state, when a thread pool instance in the SHUTDOWN state, not to mention longer accept new tasks, can only continue to handle the blocking queue stored job. When the thread pool is in the RUNNING state, calls the shutdown () method when, the thread pool will enter into this state.

4.3、STOP

       Do not accept the new task, does not handle the task queue, interrupted thread is processing tasks, when the thread pool is in the RUNNING or SHUTDOWN state if you call shutdownNow () method, the thread pool will enter into the state.

4.4、TIDYING

       If all the tasks have been terminated, and this time the number of active threads is 0, the thread pool will enter into the state, after the call terminated () method, will enter the state TERMINATED, the default terminated () method does nothing, just enter the last state.

4.5、TERMINATED

       Final state.

Fifth, the method provided ThreadPoolExecutor

5.1 The basic method:

  • execute (): submit tasks to execute thread pool
  • submit (): submit the task. To return the results of execute + Future
  • shutdown (): close the thread pool, waiting tasks are executed. Close the thread pool to save system resources
  • shutdownNow (): close the thread pool, without waiting for completion of the task execution will suspend executing thread. Applies to had to suspend all tasks, this is rare.

5.2 Monitoring methods:

  • getTaskCount (): The total number of thread pool task has been executed and unexecuted
  • getCompletedTaskCount (): the number of tasks completed
  • getPoolSize (): The current number of threads in the thread
  • getActiveCount (): The current number of threads in the thread pool is performing tasks

5.3, the method of FIG.

       

Six, Executor framework interface and common methods

6.1, Executor thread pool class diagram

Here Insert Picture Description

6.2, Exector four interfaces resolve

       6.2.1、Executor
  • Executor is a simple interface to run a new task.
       6.2.2、ExecutorService
  • ExecutorService extends the Executor interface, adding a number of methods to manage the life cycle of the actuator and the mission life cycle.
       6.2.3、ScheduledExecutorService
  • ScheduledExecutorService extended ExecutorService method, which supports the Future and recurring tasks
       6.2.4、ThreadPoolExecutor
  • ThreadPoolExecutor is the strongest diagram inside the function, because according to our own needs and we need to pass parameters to specify any policy, so he placed first and only then speak

6.3, Executor create four thread pool

       6.3.1, newCachedThreadPool (create cached thread pool)
  • Executors.newCachedThreadPool:创建的是一个可缓存的线程池,如果线程池长度,超过了处理的需要,可以灵活回收空闲线程,如果没有可以回收的,就新建线程。
       6.3.2、newFixedThreadPool(创建定长的线程池)
  • Executors.newFixedThreadPool:创建的是一个定长的线程池,可以控制线程的最大并发数,超出的线程会在队列中等待。
       6.3.3、newScheduledThreadPool(定长的线程池,支持定时以及周期性的任务)
  • Executors.newScheduledThreadPool:创建的也是一个定长的线程池,支持定时以及周期性的任务。
       6.3.4、newSingleThreadExecutor(创建单线程化的线程池)
  • Executors.newSingleThreadExecutor:创建的是一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序去执行,可以按照先入先出,优先级等等去执行。

七、合理配置线程池

7.1、配置线程池原则

  • CPU密集型任务,就需要尽量压榨CPU,参考值可以设为NCPU+1

  • IO密集型任务,参考值可以设置为2*NCPU

  • 可以先将线程池大小设置为参考值,再观察任务运行情况,系统负载,资源利用率等等来进行适当调整。

7.2、配置线程池需注意

  • 使用线程池主要是为了同用存在的线程,减少对象创建消亡,能有效的控制并发线程数,可以避免过多的资源竞争,避免阻塞,也可以定时执行,单线程等控制性能的执行,性能比较好。当然不代表要随时随地的拿出线程池来用,一定要根据自己的实际场景来分析使用,以及参数配置。
  • 有时候用线程池没有直接进行来的快:当线程池里面的任务数很小时,小到任务进行的时间与任务调度的时间很接近的时候,这时候用线程池,反而会格外慢,因为花在任务调度和任务管理的时间就会更多。
Published 20 original articles · won praise 1 · views 559

Guess you like

Origin blog.csdn.net/weixin_42295814/article/details/103792458