本文已参与[新人创作礼]活动,一起开启掘金创作之路。
一、newFixedThreadPool
- 有两种创建方式,代码如下
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);
}
- 意思就是创建核心线程数与最大线程数相等的线程,如果有多的任务会进入队列排队,使用的无界队列,常用的线程池,如果需要执行的任务小于线程数,则多的不会开启
二、newSingleThreadExecutor
- 也有两种创建方式,代码如下
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));
}
- 意思就是创建核心线程与最大线程都为1的线程池,只开启一个线程,使用的无界队列
三、newCachedThreadPool
- 有两种创建方式,代码如下
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>(),
threadFactory);
}
- 意思就是创建核心线程为0,最大线程都为Integer.MAX_VALUE的线程池,使用的同步移交队列, 队列不作为任务的缓冲方式,可以简单理解为队列长度为零
- 保持60分钟的意思是如果任务执行完毕,不再有新的任务执行,则60分钟之后会回收线程资源,即关闭线程
四、newSingleThreadScheduledExecutor
- 也有两种创建方式,代码如下
public static ScheduledExecutorService newSingleThreadScheduledExecutor() {
return new DelegatedScheduledExecutorService
(new ScheduledThreadPoolExecutor(1));
}
public static ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory) {
return new DelegatedScheduledExecutorService
(new ScheduledThreadPoolExecutor(1, threadFactory));
}
- ScheduledThreadPoolExecutor的源码如下
public ScheduledThreadPoolExecutor(int corePoolSize,
ThreadFactory threadFactory) {
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
new DelayedWorkQueue(), threadFactory);
}
- 意思就是核心线程数可传入,最大线程数为Integer的最大值,使用的是优先级队列,有一种用法是循环使用
ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5,new MyThreadFactory());
MyThread[] myThread2 = new MyThread[10];
for (int i = 0; i < myThread2.length; i++) {
myThread2[i] = new MyThread();
scheduledThreadPool.schedule(myThread2[i],3, TimeUnit.MINUTES);
System.out.println("已启动数量" + "第" + (i + 1) + "启动了");
}
- 一定要用ScheduledExecutorService来接收,意思是延迟3秒开启线程,如果需要执行的任务小于线程数,则多的不会开启
ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5,new MyThreadFactory());
MyThread[] myThread2 = new MyThread[2];
for (int i = 0; i < myThread2.length; i++) {
myThread2[i] = new MyThread();
scheduledThreadPool.scheduleAtFixedRate(myThread2[i],1, 3,TimeUnit.MINUTES);
System.out.println("已启动数量" + "第" + (i + 1) + "启动了");
}
- 意思是延迟1秒开启5个线程,每三分钟执行一次。重复启动时还是会创建5个线程