package org.example.thread;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.*;
/**
* Description: 线程池工厂
*/
@Slf4j
public class ThreadPoolFactory {
/**
* 线程池1
*/
private static ThreadPoolProxy demoF = new ThreadPoolProxy(10, "demo1");
/**
* 线程池2
*/
private static ThreadPoolProxy demoT = new ThreadPoolProxy(4, "demo2");
static {
//ThreadFactory threadFactory = new ThreadPoolProxy.DemoThreadFactory("scheduledPool");
//ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(1, threadFactory);
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
scheduledExecutorService.scheduleAtFixedRate(() -> {
log.info("ThreadPoolFactory 线程池队列监控开始");
log.info("线程池1队列堵塞:{}", getUploadBdaThreadPool().getBlockSize());
log.info("线程池2队列堵塞:{}", getUploadSensorThreadPool().getBlockSize());
log.info("ThreadPoolFactory 线程池队列监控结束");
}, 10L, 20L, TimeUnit.SECONDS);
}
/**
* 线程池1
*
* @return 线程池
*/
public static ThreadPoolProxy getUploadBdaThreadPool() {
if (demoF == null) {
synchronized (ThreadPoolProxy.class) {
if (demoF == null) {
demoF = new ThreadPoolProxy(10, "uploadBda");
}
}
}
return demoF;
}
/**
* 线程池2
*
* @return 线程池
*/
public static ThreadPoolProxy getUploadSensorThreadPool() {
if (demoT == null) {
synchronized (ThreadPoolProxy.class) {
if (demoT == null) {
demoT = new ThreadPoolProxy(3, "uploadSensor");
}
}
}
return demoT;
}
}
package org.example.thread;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Description: 线程池代理
*/
@Slf4j
public class ThreadPoolProxy {
private ThreadPoolExecutor threadPoolExecutor;
private int corePoolSize;
private String threadPoolNamePrefix;
/**
* 构造函数
*
* @param corePoolSize 核心线程数
*/
public ThreadPoolProxy(int corePoolSize, String threadPoolNamePrefix) {
this.corePoolSize = corePoolSize;
this.threadPoolNamePrefix = threadPoolNamePrefix;
}
/**
* 初始化线程池
* 双重检查加锁
*/
private void initThreadPoolExecutor() {
if (threadPoolExecutor == null) {
synchronized (ThreadPoolProxy.class) {
if (threadPoolExecutor == null) {
BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>();
ThreadFactory threadFactory = new DemoThreadFactory(threadPoolNamePrefix);
RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();
this.threadPoolExecutor = new ThreadPoolExecutor(corePoolSize, Integer.MAX_VALUE, 60,
TimeUnit.SECONDS, workQueue, threadFactory,
handler);
}
}
}
}
/**
* 执行任务
*/
public void execute(Runnable task) {
initThreadPoolExecutor();
threadPoolExecutor.execute(task);
}
/**
* 提交任务
*/
public Future<?> submit(Callable<?> task) {
initThreadPoolExecutor();
return threadPoolExecutor.submit(task);
}
/**
* 提交任务
*/
public Future<Integer> submitInt(Callable<Integer> task) {
initThreadPoolExecutor();
return threadPoolExecutor.submit(task);
}
long getBlockSize() {
initThreadPoolExecutor();
return threadPoolExecutor.getQueue().size();
}
/**
* 线程工厂
*/
public static class DemoThreadFactory implements ThreadFactory {
private final ThreadGroup group;
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;
public DemoThreadFactory(String threadNamePrefix) {
SecurityManager s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
namePrefix = threadNamePrefix + "-threadPool-";
}
@Override
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);
}
t.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
log.error("thread:{}===>Exception:{}", t.getName(), e.getMessage());
log.error("线程异常:", e);
throw new RuntimeException(e);
}
});
return t;
}
}
}