线程池以及四种常见线程池
public ThreadPoolExecutor(int corePoolSize,//核心线程池大小,核心线程将会尽可能地一直活着 int maximumPoolSize,//线程池最大数量包括核心线程数量 long keepAliveTime,//非核心线程最长存活时间 TimeUnit unit,//keepAliveTime的单位 BlockingQueueworkQueue,//等待线程的队列 ThreadFactory threadFactory,//线程工程 RejectedExecutionHandler handler)//线程拒绝执行回调复制代码
四种常见的线程池:
-
Executors.newCachedThreadPool()
new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue
())复制代码 -
Executors.newFixedThreadPool(int nThreads)
new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue
())复制代码 -
Executors.newScheduledThreadPool(int nCorepoolSize)
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) { return new ScheduledThreadPoolExecutor(corePoolSize);}//ScheduledThreadPoolExecutor():public ScheduledThreadPoolExecutor(int corePoolSize) { super(corePoolSize, Integer.MAX_VALUE, DEFAULT_KEEPALIVE_MILLIS, MILLISECONDS, new DelayedWorkQueue());}复制代码
-
Executors.newSingleThreadPool()
public static ExecutorService newSingleThreadExecutor() { return new FinalizableDelegatedExecutorService (new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue
()));}复制代码