JUC包 java util concurrent
Executors,可以创建线程池
1.ThreadPoolExecutor
newCachedThreadPool,newFixedThreadPool,newSingleThreadExecutor
查看Executors源码可以得知:这3个方法内部调用的都是 ThreadPoolExecutor这个类,只是参数不同的区别。
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Executors.defaultThreadFactory(), defaultHandler);
}
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler) {
if (corePoolSize < 0 ||
maximumPoolSize <= 0 ||
maximumPoolSize < corePoolSize ||
keepAliveTime < 0)
throw new IllegalArgumentException();
if (workQueue == null || threadFactory == null || handler == null)
throw new NullPointerException();
this.acc = System.getSecurityManager() == null ?
null :
AccessController.getContext();
this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue;
this.keepAliveTime = unit.toNanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
}
参数解释:
corePoolSize:当前线程池中核心线程的个数
maximumPoolSzie:线程池能创建的最大线程数
keepAliveTime:空闲线程存活时间
unit:时间单位,为keepAliveTime指定时间
workQueue:阻塞队列,用来保存任务的阻塞队列
threadFactory:创建线程的工厂类
handler: 饱和策略(拒绝策略)
1.1 newCachedThreadPool
创建一个可根据需要创建新线程的线程池,但是在以前构造的线程可用时将重用它们,并在需要时提供的ThreadFactory创建新线程,特征:
1.线程池中数量没有固定,可达到最大值 Integer.MAX_VALUE
2.线程池中的线程可进行缓存重复利用和回收,回收默认时间1分钟
3当线程池中,没有可用线程,会重新创建一个线程
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
1.2 newFixedThreadPool
创建一个可重用固定线程数的线程池,以共享的无界队列方式来运行这些线程,在任意点,在大多数nThreads线程会处于处理任务的活动状态,如果在所有线程处于活动状态时提交附加任务,则在有可用线程之前,附加任务将在列队中等待。如果在关闭前的执行期间由于失败而导致任何线程终止,那么一个新线程将代替它执行后续的任务(如果需要),在某个线程被显式的关闭之前,池中的线程将一直存在,特征:
1.线程池中的线程处于一定的量,可以很好的控制线程的并发量
2.线程可以重复被使用,在显式关闭之前,都将一直存在
3.超出一定量的线程被提交时候需在队列中等待
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(),
threadFactory);
}
1.3 newSingleThreadExecutor
创建一个使用单个worker线程的Executor,以无界队列方式来运行该线程(注意:如果因为在关闭前的执行期间出现失败而终止了此单个线程,那么如果需要,一个新线程将代替执行它执行后续的任务)。可保证顺序的执行各个任务,并且在任意给定的时间不会有多个线程是活动的,与其他等效的newFixedThreadPool(1)不同,可保证无需重新配置此方法所返回的执行程序即可使用其他的线程。特征:
1.线程池中最多执行一个线程,之后提交的线程活动将会排在队列中依次执行
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}
public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(),
threadFactory));
}
2.ScheduledThreadPoolExecutor
newSingleThreadScheduleExecutor,newScheduleThreadPool
这两个是调用 ScheduledThreadPoolExecutor 类里面的构造方法来创建
public ScheduledThreadPoolExecutor(int corePoolSize) {
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
new DelayedWorkQueue());
}
这里面的super调用的是 ThreadPoolExecutor这个父类的方法也就是
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Executors.defaultThreadFactory(), defaultHandler);
}
2.1 newSingleThreadScheduleExecutor
创建一个单线程执行程序,它可安排在给定延迟后运行命令或者定期的执行,特征:
1.线程池中最多执行一个线程,之后提交的线程活动将会排在队列中依次执行
2.可定时或者延迟执行线程活动
public static ScheduledExecutorService newSingleThreadScheduledExecutor() {
return new DelegatedScheduledExecutorService
(new ScheduledThreadPoolExecutor(1));
}
public ScheduledThreadPoolExecutor(int corePoolSize) {
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
new DelayedWorkQueue());
}
2.2 newScheduleThreadPool
创建一个线程池,它可安排在给定延迟后运行命令或者定期的执行。特征:
1.线程池中具有指定数量的线程,即便是空线程也将保留
2.可定时或者延迟执行线程活动
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
return new ScheduledThreadPoolExecutor(corePoolSize);
}
public ScheduledThreadPoolExecutor(int corePoolSize) {
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
new DelayedWorkQueue());
}
3.ForkJoinPool
3.1 newWorkStealingPool
创建一个带并行级别的线程池,并行级别决定了同一时刻最多有多少个线程在执行,如不传并行级别参数,将默认为当前系统的CPU个数