一、官网的解释
/**
* Creates a new {@code ThreadPoolExecutor} with the given initial
* parameters.
*
* @param corePoolSize the number of threads to keep in the pool, even
* if they are idle, unless {@code allowCoreThreadTimeOut} is set
* @param maximumPoolSize the maximum number of threads to allow in the
* pool
* @param keepAliveTime when the number of threads is greater than
* the core, this is the maximum time that excess idle threads
* will wait for new tasks before terminating.
* @param unit the time unit for the {@code keepAliveTime} argument
* @param workQueue the queue to use for holding tasks before they are
* executed. This queue will hold only the {@code Runnable}
* tasks submitted by the {@code execute} method.
* @param threadFactory the factory to use when the executor
* creates a new thread
* @param handler the handler to use when execution is blocked
* because the thread bounds and queue capacities are reached
* @throws IllegalArgumentException if one of the following holds:<br>
* {@code corePoolSize < 0}<br>
* {@code keepAliveTime < 0}<br>
* {@code maximumPoolSize <= 0}<br>
* {@code maximumPoolSize < corePoolSize}
* @throws NullPointerException if {@code workQueue}
* or {@code threadFactory} or {@code handler} is null
*/
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler)
我的自定义注解
- corePoolSize:核心线程池大小(核心线程数量)
- maximumPoolSize:最大线程池大小
- keepAliveTime :线程最大空闲时间
- unit :keepAliveTime 时间单位
- workQueue :线程等待队列
- threadFactory :线程创建工厂
- handler :拒绝策略
ThreadPoolExecutor的执行顺序
二 、使用案例
(自定义线程工厂,自定义拒绝策略)
package com.wx;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
public class ThreadTest {
public static void main(String[] args) {
int corePoolSize = 3;
int maximumPoolSize = 3;
long keepAliveTime = 10;
TimeUnit unit = TimeUnit.SECONDS;
BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(2);
ThreadFactory threadFactory = new MyThreadFactory();
// RejectedExecutionHandler handler = new MyIgnorePolicy();
RejectedExecutionHandler handler = new ThreadPoolExecutor.CallerRunsPolicy();
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
threadPoolExecutor.prestartAllCoreThreads(); // 预启动所有核心线程
for (int i = 0; i <= 10; i++) {
MyTask task = new MyTask(String.valueOf(i));
threadPoolExecutor.execute(task);
}
}
static class MyThreadFactory implements ThreadFactory{
private AtomicInteger atomicInteger = new AtomicInteger(0);
@Override
public Thread newThread(Runnable r) {
Thread thread = new Thread(r,"wxThread-"+atomicInteger.getAndIncrement());
System.out.println(">>>>>>>>"+thread.getName()+" has been created !");
return thread;
}
}
static class MyIgnorePolicy implements RejectedExecutionHandler{
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
System.err.println(r.toString()+"rejected");
// System.out.println("completedTaskCount: " + executor.getCompletedTaskCount());
}
}
static class MyTask implements Runnable{
private String name;
public MyTask(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "MyTask{" +
"name='" + name + '\'' +
'}';
}
@Override
public void run() {
try {
System.out.println(this.toString() + " is running!");
Thread.sleep(300); //让任务执行慢点
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
AbortPolicy:默认测策略,抛出RejectedExecutionException运行时异常;
CallerRunsPolicy:这提供了一个简单的反馈控制机制,可以减慢提交新任务的速度;
DiscardPolicy:直接丢弃新提交的任务;
DiscardOldestPolicy:如果执行器没有关闭,队列头的任务将会被丢弃,然后执行器重新尝试执行任务(如果失败,则重复这一过程);