Using multiple threads of learning thread pool TheadPoolExecutor

Create a thread pool

ThreadPoolExecutor(
  int corePoolSize,
  int maximumPoolSize,
  long keepAliveTime,
  TimeUnit unit,
  BlockingQueue<Runnable> workQueue,
  ThreadFactory threadFactory,
  RejectedExecutionHandler handler) 

corePoolSize: Minimum number of threads

maximumPoolSize: The maximum number of threads first

keepAliveTime, unit: idle timeout policy

workQueue: Queuing queue

threadFactory: custom-created thread policy

handler: Custom deny policy, the policy specified by the handler refused custom solutions currently support

CallerRunsPolicy

AbortPolicy

DiscardPolicy

DiscardOldestPolicy

Precautions

It is strongly recommended to use a bounded queue: unbounded queue easily lead to oom, oom and causes all the requests is not available. Use executor create a thread pool, oom can cause problems, because many are using the queue

Even with a default deny policy: default deny throws abnormal operation, abnormal operation does not enforce catch at compile time. This will cause the thread to stop, but the most important thing is there is no notice, this will lead developers to perform that task properly. Deal with this problem, the safest or try catch catch the exception.

Published 23 original articles · won praise 19 · views 1415

Guess you like

Origin blog.csdn.net/u012335601/article/details/89646897