浅谈Java ThreadPoolExecutor的使用
什么是ThreadPoolExecutor
ThreadPoolExecutor是Java中的线程池的实现类之一,通过它可以实现线程池的管理和任务分配的功能,是多线程程序开发中常用的工具。
ThreadPoolExecutor的构造方法
ThreadPoolExecutor有多个构造方法,常用的有以下四种:
-
ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,BlockingQueue
workQueue):构造一个新的ThreadPoolExecutor,将使用给定的初始参数和阻塞队列。 -
ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,BlockingQueue
workQueue,ThreadFactory threadFactory): 构造一个新的ThreadPoolExecutor,将使用给定的初始参数,阻塞队列和线程工厂 -
ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,BlockingQueue
workQueue,RejectedExecutionHandler handler):构造一个新的ThreadPoolExecutor,将使用给定的初始参数,阻塞队列和饱和策略。 -
ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,BlockingQueue
workQueue,ThreadFactory threadFactory,RejectedExecutionHandler handler): 构造一个新的ThreadPoolExecutor,将使用给定的初始参数,阻塞队列,线程工厂和饱和策略。
常用参数
- corePoolSize:线程池核心线程数,当任务数量达到核心线程数时,线程池会创建新的线程来执行任务,但是创建的线程数不能超过maximumPoolSize
- maximumPoolSize:线程池最大线程数,当任务数量达到最大线程数时,线程池会根据饱和策略来处理任务。
- keepAliveTime:线程的空闲时间,当线程处于空闲状态超过指定时间,线程会被销毁。
- unit:时间单位。
- workQueue:阻塞队列,用于存储等待执行的任务。
- threadFactory:创建线程的工厂。
- handler:饱和策略,当任务数量大于 maximumPoolSize + workQueue 的容量时,线程池的处理策略。
示例
任务队列使用LinkedBlockingQueue
代码示例:
public class LinkedBlockingQueueDemo {
public static void main(String[] args) {
int corePoolSize = 2;
int maximumPoolSize = 4;
long keepAliveTime = 60;
TimeUnit unit = TimeUnit.SECONDS;
BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>(10);
RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();
ThreadPoolExecutor executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler);
for (int i = 1; i <= 20; i++) {
final int j = i;
executor.execute(() -> {
System.out.println("Task " + j + " is Running...");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
executor.shutdown();
}
}
执行结果:
Task 1 is Running...
Task 2 is Running...
Task 3 is Running...
Task 4 is Running...
Task 5 is Running...
Task 6 is Running...
Task 7 is Running...
Task 8 is Running...
Task 9 is Running...
Task 10 is Running...
Task 11 is Running...
Task 12 is Running...
Task 13 is Running...
Task 14 is Running...
Task 15 is Running...
Task 16 is Running...
Task 17 is Running...
Task 18 is Running...
Task 19 is Running...
Task 20 is Running...
任务队列使用ArrayBlockingQueue
代码示例:
public class ArrayBlockingQueueDemo {
public static void main(String[] args) {
int corePoolSize = 2;
int maximumPoolSize = 4;
long keepAliveTime = 60;
TimeUnit unit = TimeUnit.SECONDS;
BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(10);
RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();
ThreadPoolExecutor executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler);
for (int i = 1; i <= 20; i++) {
final int j = i;
executor.execute(() -> {
System.out.println("Task " + j + " is Running...");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
executor.shutdown();
}
}
执行结果:
Task 1 is Running...
Task 2 is Running...
Task 3 is Running...
Task 4 is Running...
Task 5 is Running...
Task 6 is Running...
Task 7 is Running...
Task 8 is Running...
Task 9 is Running...
Task 10 is Running...
由于ArrayBlockingQueue的容量为10,而创建线程池规定的最大线程数为4,故只有前10个任务可以加入到队列中执行,后面的任务因为队列已满,无法加入队列,被拒绝执行。因此,只输出了前10个任务的执行结果。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:浅谈Java ThreadPoolExecutor的使用 - Python技术站