面试官因为线程池,让我出门左拐!
Posted 小黑格子屋
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了面试官因为线程池,让我出门左拐!相关的知识,希望对你有一定的参考价值。
前序
public static ExecutorService newFixedThreadPool(int nThreads) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); } br
单个线程的线程池,即线程池中每次只有一个线程工作,单线程串行执行任务public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) { this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, defaultHandler); }
public static ExecutorService newFixedThreadPool(int nThreads) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); } public static ExecutorService newFixedThreadPool(int nThreads) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); }
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) { return new ScheduledThreadPoolExecutor(corePoolSize); } public static ScheduledExecutorService newScheduledThreadPool( int corePoolSize, ThreadFactory threadFactory) { return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory); }
ExecutorService cachedThreadPool=Executors.newFixedThreadPool();
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) { this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, Executors.defaultThreadFactory(), defaultHandler); }
-
corePoolSize 要保留在池中的线程数,也就是线程池核心池的大小 -
maximumPoolSize 最大线程数 -
keepAliveTime 当线程数大于核心时,此为终止前多余的空闲线程等待新任务的最长时间。 -
unit keepAliveTime 参数的时间单位 -
workQueue 用来储存等待执行任务的队列。 -
threadFactory 线程工厂 -
handler 默认的拒绝执行处理程序
可乐记得加冰,爱我就要置顶
客官!在看一下呗
以上是关于面试官因为线程池,让我出门左拐!的主要内容,如果未能解决你的问题,请参考以下文章
面试官:线程池如何按照 coremaxqueue 的执行循序去执行?
面试官:线程池如何按照coremaxqueue的执行循序去执行?(内附详细解析)