Briefly Java thread pool

1 Java thread pool Profile

Java1.5 master Doug Lea's work. Thread pool thread can be unified distribution, responsible for processing tasks through a fixed number of threads, to avoid the frequent create and destroy objects , so threads can be repeated use , perform multiple tasks.

2 Java types of thread pool

Executors to achieve the kind of thread factory

kind achieve Blocking queue based Explanation
Fixed size thread pool FixedThreadPool LinkedBlockingQueue Fixed number of threads, task queue unbounded
Delivery model thread pool CachedThreadPool SynchronousQueue Without limiting the number of threads, task queue does not store data
Parallel thread pool WorkStealingPool FIFO_QUEUE或LIFO_QUEUE Without limiting the number of threads, dividing the task execution
Timing task thread pool ScheduledThreadPool DelayedWorkQueue Without limiting the number of threads, task execution time priority
Singleton fixed thread pool SingleThreadExecutor LinkedBlockingQueue A number of threads, task execution order of addition

Achieve 3 Java thread pool

3.1 blocking queue

queue achieve Explanation
LinkedBlockingQueue And a display lock queue link list based on the realization Similar support concurrent LinkedList
SynchronousQueue Queue-based dual-stack delivery / double lock achieved and a display queue Dual-stack / queue algorithm based on double
DelayedWorkQueue Based on the priority queue and the queue delay locks for display Similarly with the priority queue, the algorithm implemented by a binary heap

3.2 thread factory Executors

The main process thread pool

3.2.1 CachedThreadPool

  • Prior to reuse threads (threads in the thread pool is not destroyed)
  • If no thread, a new thread is created and added to the pool
  • The default thread pool is not used for the 60s is terminated and removed
  • Long-term idle pool will not consume any resources

When the execute method, will first offer the first implementation of the method SynchronousQueue submit tasks and queries whether idle threads in the thread pool to execute SynchronousQueue poll method to remove tasks. If so, then the pairing is successful, the task to the idle threads. Otherwise, the pairing fails, create a new thread to handle the task; When a thread pool idle threads will wait for the implementation of the poll method SynchronousQueue SynchronousQueue perform tasks in the new submission. If more than 60s is still not committed to the task SynchronousQueue, the idle thread will terminate; because maximumPoolSize is unbounded, so the speed of the submitted jobs> thread pool thread speed processing tasks will continue to create new threads; each submission task will there are threads to deal with immediately, so CachedThreadPool applicable to handle large, less time-consuming task.

Delivery strengths and shortcomings (cache) thread pool

Advantages: immediate task execution, the thread automatic recovery

Disadvantages: will appear instantly at the moment is too high concurrency create thousands of threads, resulting in paralysis of the system

scenes to be used

OKHTTP framework

3.2.2 FixedThreadPool

  • Reuse a fixed number of threads to create a thread pool
  • When all threads are active, if submitted other tasks, they will wait for a thread in the queue available
  • When the maximum number of tasks, plus membership be rejected
  • Thread will always exist until you call shutdown

FixedThreadPool only the core thread, and a fixed number, no non-core thread. keepAliveTime set to 0L, on behalf of the excess threads will be terminated immediately. Because no extra thread, keepAliveTime is invalid parameter; using the task queue unbounded blocking queue LinkedBlockingQueue (capacity default Integer.MAX_VALUE).

Advantages and shortcomings FixedThreadPool thread pool

Advantages: Controlled relatively fixed number of threads

Cons: tasks performed under high concurrency will become slow, will be waiting in the queue.

scenes to be used

Tomcat worker threads

3.2.3 ScheduledThreadPool

  • Set the delay time to perform regular
  • Idle thread will be reserved.

When scheduleAtFixedRate or scheduleWithFixedDelay ScheduledThreadPoolExecutor method of execution, the task will be added to achieve a wrapper class ScheduledFutureTask RunnableScheduledFuture interface to DelayedWorkQueue, thread and check whether the number of cores running threads corePoolSize. If you do not create a new thread and start. But the task is not executed immediately, but to take the task in DelayedWorkQueue packaging ScheduledFutureTask, and then to perform the task; if the thread runs reached corePoolSize, add tasks to put in the task queue DelayedWorkQueue; DelayedWorkQueue task will be sorted, first performed the task in front of the queue. After task execution, ScheduledFutureTask variables in time changed the next time you want to perform, and put back in DelayedWorkQueue.

3.3 rejection policy

Tactics Explanation
AbortPolicy When the task is added to the thread pool is rejected, it will throw an exception RejectedExecutionException.
CallerRunsPolicy When the task is added to the thread pool is rejected, processing tasks to be rejected in Thread thread pool thread pool that are currently running.
DiscardOldestPolicy When the task is added to the thread pool is rejected, the thread pool will give up waiting in the queue oldest unprocessed tasks, and then add the rejected task to the waiting queue.
DiscardPolicy When the task is added to the thread pool is rejected, discarded thread pool rejected task.

4 Description

Reference material

ScheduledThreadPool thread pool of learning blog.csdn.net/qq_36299025...

Compared with using five thread pool

www.jianshu.com/p/135c89001…

other information

Java priority queue DelayedWorkQueue principle analysis www.jianshu.com/p/587901245...

Detailed SynchronousQueue principle - fair mode www.cnblogs.com/dwlsxj/p/Th...

SynchronousQueue Detailed principles - non-equity modes www.cnblogs.com/dwlsxj/p/sy...

Guess you like

Origin juejin.im/post/5d846b376fb9a06aea61cd60