java in four common thread pool --- personal summary

First, the classification of the thread pool, use the difference between the scene and their (personal notes)

  ① newCachedThreadPool : create a cache thread pool, thread pool longer than if treatment needs, the flexibility to reclaim idle thread, if not recyclable, the new thread

  Analysis of the bottom : corePoolSize is 0; maximumPoolSize is Integer.MAX_VALUE; keepAliveTime to 60L; unit is TimeUnit.SECONDS; workQueue is SynchronousQueue (synchronous queue)

  Usage scenarios : a lot of short-term asynchronous execution of applets or lightly loaded server

  It is a popular explanation : When a new task comes, inserted into SynchronousQueue, because SynchronousQueue synchronous queue, so will look to execute threads available in the pool, if you can thread is executed, if there is no available threads create a thread to perform this task; if the thread pool idle for more than the specified size, the thread will be destroyed.


  ② newFixedThreadPool : create a fixed-size thread pool, you can control the maximum number of concurrent threads, excess threads will wait in the queue

  Analysis of the bottom : return ThreadPoolExecutor instance, the number of the received parameters nThread set of threads, as corePoolSize nThread, maximumPoolSize is nThread; keepAliveTime is 0L (when open); Unit as: TimeUnit.MILLISECONDS; WorkQueue as: new LinkedBlockingQueue <Runnable> () no solution blocking queue

  Usage scenarios : the implementation of long-term task, much better performance

  Popular speaking : Creating a pond can accommodate a fixed number of threads, every thread of survival time is infinite, when the pond is full not add a thread; if all the threads in the pool are busy, the new mission will enter the blocking queue (unbounded blocking queue)


  ③ newSingleThreadExecutor : to create a single-threaded thread pool, it will only work with a single thread to perform the task to ensure that all tasks are performed in a specified order (FIFO, LIFO, priorities)

  Analysis of the bottom: the ThreadPoolExecutor example FinalizableDelegatedExecutorService packaging, corePoolSize is 1; maximumPoolSize is 1; keepAliveTime is 0L; unit is: TimeUnit.MILLISECONDS; workQueue as: new LinkedBlockingQueue <Runnable> () without unblocking queue

  Use Scene: Scene of a task to perform a task

  Popular speaking : thread pool that creates only one thread, and the thread of survival time is infinite; when the thread is busy, the new task will enter the blocking queue (unbounded blocking queue)


  ④ newScheduledThreadPool : create a fixed-length thread pool support of regular and periodic task execution

  Analysis of the bottom : Create ScheduledThreadPoolExecutor instance, corePoolSize of the passed parameters, maximumPoolSize is Integer.MAX_VALUE; keepAliveTime is 0; unit is: TimeUnit.NANOSECONDS; workQueue as: new DelayedWorkQueue () a sorted queue timeout ascending

  Use Scene : Scene of the periodic tasks

  Popular speaking: to create a fixed-size thread pool, thread pool thread survival time unlimited, the thread pool to support regular and periodic task execution, if all the threads are busy status for a new task will enter DelayedWorkQueue queue, which according to a time-out time ordered queue structure

Second, the task execution thread pool process analysis:

  1) When the thread pool is less than corePoolSize, submit new task will create a new thread to perform the task, even though at this time there is a free thread pool.

  2) When the thread pool reaches corePoolSize, submit new task will be placed workQueue, the waiting thread pool task scheduled for execution

  3) When workQueue full and maximumPoolSize> corePoolSize, the newly submitted task creates a new thread to perform tasks

  4) When submitting a number of tasks exceeds maximumPoolSize, submit new tasks handled by RejectedExecutionHandler

  5) When the thread pool threads exceeds corePoolSize, idle time to reach keepAliveTime, shut down idle threads

  6) When the setting allowCoreThreadTimeOut (true), the thread pool threads idle for corePoolSize also closed keepAliveTime

Guess you like

Origin www.cnblogs.com/rmxd/p/11230530.html