ThreadPoolExecutor
常用线程池:
Executor,ExecutorService, Future, Callable, Executors, FixedThreadPool, CachedThreadPool,
ScheduledThreadPool, SingleThreadExceutor, ForkJoinPool, WorkStealingPool, ThreadPoolExecutor
线程池底层实现。除 ForkJoinPool 外,其他常用线程池底层都是使用 ThreadPoolExecutor实现的。
这里重点讲解 ThreadPoolExecutor 线程池
重点理解线程池参数:
int corePoolSize: 核心容量,创建线程池的时候,默认有多少线程。也是线程池保持的最少线程数
int maximumPoolSize:最大容量,线程池最多有多少线程
long keepAliveTime:生命周期,0 为永久。当线程空闲多久后,自动回收。
TimeUnit unit: 生命周期单位,为生命周期提供单位,如:秒,毫秒、
BlockingQueue<Runnable> workQueue :任务队列,阻塞队列。注意,泛型必须是Runnable
本章主要是帮助快速使用ThreadPoolExecutor进行开发,以下以一个经典简单案例帮助理解和使用ThreadPoolExecutor
/**
* 线程池
* 固定容量线程池
*/
package concurrent.t08;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class Test_09_ThreadPoolExecutor {
public static void main(String[] args) {
// 模拟fixedThreadPool, 核心线程5个,最大容量5个,线程的生命周期无限。
ExecutorService service =
new ThreadPoolExecutor(5, 5, 0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
for(int i = 0; i < 6; i++){
service.execute(new Runnable() {
@Override
public void run() {
try {
TimeUnit.MILLISECONDS.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " - test executor");
}
});
}
System.out.println(service);
service.shutdown();
System.out.println(service.isTerminated());
System.out.println(service.isShutdown());
System.out.println(service);
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
service.shutdown();
System.out.println(service.isTerminated());
System.out.println(service.isShutdown());
System.out.println(service);
}
}
本文详细解析了ThreadPoolExecutor线程池的参数配置与工作原理,包括核心容量、最大容量、生命周期和任务队列等关键概念,并通过一个经典案例展示了如何使用ThreadPoolExecutor进行线程池的快速开发。
1017

被折叠的 条评论
为什么被折叠?



