Custom thread pool, and how best to create a thread pool

There are preset java thread pool: newSingleThreadExecutor, newFixedThreadPool, newCacheedThreadPool, newScheduledThreadPool, newWorkStealingPool. If not, you can also use ThreadPoolExecutor create a custom thread pool. The main constructor:

 1 public ThreadPoolExecutor(int corePoolSize,
 2                           int maximumPoolSize,
 3                           long keepAliveTime,
 4                           TimeUnit unit,
 5                           BlockingQueue<Runnable> workQueue)
 6 
 7 public ThreadPoolExecutor(int corePoolSize,
 8                           int maximumPoolSize,
 9                           long keepAliveTime,
10                           TimeUnit unit,
11                           BlockingQueue<Runnable> workQueue,
12                           ThreadFactory threadFactory,
13                           RejectedExecutionHandler handler)

Next we introduce the parameters which the thread pool size and the first four parameters.

  • corePoolSize : a core number of threads.
    • Just create a thread pool, no, that will not be created in advance. When the mission arrived, and the current thread does not exceed corePoolSize, it will create a new thread to perform the task, even if other threads are idle.
    • Idle and will not be released, keepAliveTime NA.
  • maximumPoolSize : maximum number of threads. As described above, if the current thread is more than corePoolSize, first try to line up, if the queue is full or circumstances beyond the team, then it will not line up, but the number of threads to check whether maximumPoolSize, if not, create a thread until the number of threads reach maximumPoolSize.
  • keepAliveTime : idle thread survival time. When the number of threads in the thread pool greater than corePoolSize, additional idle threads of survival. If at the time, no new tasks will release the thread. A value of 0 indicates that the thread will not release timeout.
  • unit
  • BlockingQueue : blocking queue. You can use LinkedBlockingQueue (default unbounded), ArrayBlockingQueue, PriorityBlockingQueue (unbounded), SynchronousQueue (not actual storage space). Use unbounded queue, you need to pay attention to, up to a maximum number of threads corePoolSize, new tasks can only line up, maximumPoolSize meaningless. SynchronousQueue have only just idle threads, the team will be successful, otherwise, always create a new thread, until it reaches maximumPoolSize.
  • Handler : job rejection policy. Bounded queue, the number of threads to reach maximumPoolSize, the queue is full, the trigger task deny policy. Four processes: AbortPolicy (by default, throw an exception), DiscardPolicy (ignore the new task, do not throw an exception), DiscardOldestPolicy (throw away the longest wait, own queuing), CallerRunsPolicy (task submitter thread perform the task)

Best custom-created thread pool queue bounded, limited maximumPoolSize, using the task deny policy. If the queue is unbounded, not always line up service tasks, memory consumption, and even lead to memory exception. If the queue is bounded but maximumPoolSize wireless, might create too many threads, accounting for memory and CPU.

 

Guess you like

Origin www.cnblogs.com/ivy-xu/p/12607811.html