一、线程池的7个参数
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler) {
if (corePoolSize < 0 ||
maximumPoolSize <= 0 ||
maximumPoolSize < corePoolSize ||
keepAliveTime < 0)
throw new IllegalArgumentException();
if (workQueue == null || threadFactory == null || handler == null)
throw new NullPointerException();
this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue;
this.keepAliveTime = unit.toNanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
}
static class DefaultThreadFactory implements ThreadFactory {
private static final AtomicInteger poolNumber = new AtomicInteger(1);
private final ThreadGroup group;
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;
DefaultThreadFactory() {
SecurityManager s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() :
Thread.currentThread().getThreadGroup();
namePrefix = "pool-" +
poolNumber.getAndIncrement() +
"-thread-";
}
public Thread newThread(Runnable r) {
Thread t = new Thread(group, r,
namePrefix + threadNumber.getAndIncrement(),
0);
if (t.isDaemon())
t.setDaemon(false);
if (t.getPriority() != Thread.NORM_PRIORITY)
t.setPriority(Thread.NORM_PRIORITY);
return t;
}
}
二、简单的线程池工具类
package com.itheima.security.springboot.test;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
/**
* 自己的线程工具类
*
* @Auther: Vector
* @Date: ${DATE} * @Description: ${PACKAGE_NAME} * @version: 1.0
*/
public class ThreadPoolUtil {
/**
* 核心线程数
*/
private static final int SIZE_CORE_POOL = 5;
/**
* 最大线程数
*/
private static final int SIZE_MAX_POOL = 10;
/**
* 存活时间
*/
private static final long ALIVE_TIME = 2000;
/**
* 存放任务的队列
*/
private static BlockingQueue<Runnable> bqueue = new ArrayBlockingQueue<>(60);
/**
* The default rejected execution handler
*/
private static final RejectedExecutionHandler defaultHandler =
new ThreadPoolExecutor.AbortPolicy();
private static ThreadPoolExecutor pool =
new ThreadPoolExecutor(SIZE_CORE_POOL,
SIZE_MAX_POOL,
ALIVE_TIME,
TimeUnit.MILLISECONDS,
bqueue,
ThreadPoolUtil.myDefaultThreadFactory(),
defaultHandler);
static {
pool.prestartAllCoreThreads();
}
public static ThreadPoolExecutor getPool() {
return pool;
}
public static ThreadFactory myDefaultThreadFactory() {
return new MyDefaultThreadFactory();
}
static class MyDefaultThreadFactory implements ThreadFactory {
private static final AtomicInteger poolNumber = new AtomicInteger(1);
private final ThreadGroup group;
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;
MyDefaultThreadFactory() {
SecurityManager s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() :
Thread.currentThread().getThreadGroup();
namePrefix = "dcss-account" +
poolNumber.getAndIncrement() +
"-thread-";
}
public Thread newThread(Runnable r) {
Thread t = new Thread(group, r,
namePrefix + threadNumber.getAndIncrement(),
0);
if (t.isDaemon())
t.setDaemon(false);
if (t.getPriority() != Thread.NORM_PRIORITY)
t.setPriority(Thread.NORM_PRIORITY);
return t;
}
}
}
三、动态线程池
https://dynamictp.cn/