在这章节说说线程池,首先线程池在工作中的应用场景很多,几乎只要是线程的异步操作和并发执行任务都会涉及到线程池的使用,下面的我就说说我所认知的线程池。
首先是线程池的一些知识
1、corePoolSize(核心线程数){线程池核心线程数,默认情况下,核心线程会在线程池中一直存活,即使它们处于闲置状态。如果ThreadPoolExecutor的allowCoreThreadTimeOut属性设置为true,那么闲置的核心线程在等待新任务到来时会有超时策略,这个时间间隔由keepAliveTime所指定的时长后,核心线程就会被终止。}
2、maximumPoolSize(最大线程数){线程池所能容纳的最大线程数,当线程达到这个数值后,后续的新任务将被阻塞}
3、keepAliveTime(非核心线程闲置超时时长){一个非核心线程,如果不使用(闲置状态)的时长超过这个参数所设定的时长,就会被销毁掉,如果设置allowCoreThreadTimeOut = true,则会作用于核心线程}
4、unit(keepAliveTime的单位)
5、workQueue(维护等待执行的Runnable对象)
下面是线程池的原理:
上面的流程简单的来说:在这里举个例子,核心线程数为2,最大线程数也为2,
当用户提交线程过来,当前线程为1进行判断,当然实际线程是小于核心线程数的,所以直接创建线程并且执行,当线程为2时它也是不大于核心线程数的,所以还是直接创建线程并且执行。注意当线程3过来的时候,它是大于核心线程数,进行判断阻塞队列是否已满,没有放入队列里进行缓存起来,等待前面线程执行完,在进行执行。如果队列已满,判断是否大于最大线程数,大于直接拒绝或抛出异常,小于则创建线程并且执行。
使用线程池有什么好处呢?
1、降低了系统资源的浪费。(可以重复使用创建好的线程和线程销毁是所浪费的资源)
2、提高了响应速度。(有新的任务过来不用创建新线程或等待)
3、方便了对线程的管理。(进行统一的管理和分配)
线程池的创建方式有4种:
newCachedThreadPool(可缓存)、
newFixedThreadPool (定长,能控制线程的并发,多余的会缓存在队列里等待)、
newScheduledThreadPool (定长,支持定时和周期性任务)、
newSingleThreadExecutor (单例)
(1)newCachedThreadPool
源码
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
创建
ExecutorService newCachedThreadPool = Executors.newCachedThreadPool();
for (int i = 0; i < 10; i++) {
final int mvp = i;
newCachedThreadPool.execute(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(66);
} catch (Exception e) {
// TODO: handle exception
}
System.out.println(Thread.currentThread().getName() + ",i:" + mvp );
}
});
}
(2)newFixedThreadPool
源码
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
创建
ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(5);
for (int i = 0; i < 10; i++) {
final int mvp = i;
newFixedThreadPool.execute(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + ",i:" + mvp);
}
});
}
(3)newScheduledThreadPool
源码
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());
}
创建
ScheduledExecutorService newScheduledThreadPool = Executors.newScheduledThreadPool(5);
for (int i = 0; i < 10; i++) {
final int mvp= i;
newScheduledThreadPool.schedule(new Runnable() {
public void run() {
System.out.println("i:" + mvp);
}
}, 6, TimeUnit.SECONDS);
}
(4)newSingleThreadExecutor
源码
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}
创建
ExecutorService newSingleThreadExecutor = Executors.newSingleThreadExecutor();
for (int i = 0; i < 10; i++) {
final int mvp = i;
newSingleThreadExecutor.execute(new Runnable() {
@Override
public void run() {
System.out.println("mvp :" + mvp );
try {
Thread.sleep(66);
} catch (Exception e) {
// TODO: handle exception
}
}
});
}