Use the thread pool scene what

 

Various thread pool usage scenarios

https://blog.csdn.net/qq_17045385/article/details/79820847

 

https://www.jianshu.com/p/71b5e40f94e0

 

  1. Use the thread pool scene what
    the thread pool for a large number of asynchronous single-tasking system, such as sending text messages, save the log.

  2. Talk about important parameter to create a thread pool

    • corePoolSize: the size of the thread pool. It does not immediately go to create a thread after thread pool is created, but waits thread. When the number of currently executing thread is greater than the value, the thread is added to the buffer queue.
    • maximumPoolSize: maximum number of threads in the thread pool created.
    • keepAliveTime: How long after the thread idle destroyed. By default, this value is greater than the number of threads corePoolSize, act on the threads exceeds corePoolSize value.
    • unit: TimeUnit enumeration value types, representing keepAliveTime unit of time.
    • handler: thread deny policy.
  3. How to set these parameters, the thread pool tuning how do

    • The basic idea:

      1. High concurrency, short business task execution time, the number of threads in the thread pool can be set to the number of CPU cores + 1, reducing thread context switching.
      2. Concurrency is not high, a separate task to perform for a long time business area:
        1. IO-intensive task, because the IO operation does not take up CPU, you can increase the number of threads in the pool, allow the CPU to handle more business
        2. CPU-intensive tasks, the number of threads in the pool is set to be less, reducing the thread context switching.
      3. High concurrency, business executive for a long time, that the overall architecture design, the ability to use middleware to split tasks and decoupling.
    • The setting:

      1. corePoolSize = number of threads per processing needs
        of THREADCOUNT = Tasks / (. 1 / taskcost) = Tasks (500 ~ 1000) taskcout = 0.1 100 = 50 ~ threads. corePoolSize provided should be greater than 50. 8020 According to the principle, if 80% of the number of jobs is less than 800 per second, then corePoolSize set to 80.
      2. queueCapacity = (coreSizePool / taskcost) * responsetime
        calculated available queueCapacity = 80 / 0.1 * 1 = 80, mean queue thread can wait 1s, than the need to open a new thread to execute. Remember not to Integer.MAX_VALUE, this queue will be great, just to keep the number of threads in corePoolSize size, increased sharply when the task can not open new thread to execute, the response time will be increased sharply.
      3. maxPoolSize = (max (tasks) - queueCapacity) / (1 / taskcost) (the maximum number of tasks - queue capacity) per processing capability = maximum number of threads / each thread. Calculation available maxPoolSize = (1000-80) / 10 = 92.
      4. rejectedExecutionHandler: determined depending on the circumstances, may be discarded important task, the task will have an important use of some buffering mechanism to handle.
      5. keepAliveTime and allowCoreThreadTimeout: default can meet.

Guess you like

Origin www.cnblogs.com/weigy/p/12667425.html