废话不多说,先上代码 觉得有用给点热心值吧
[Java] 纯文本查看 复制代码 import cn.hutool.core.thread.ThreadFactoryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.*;
public enum ThreadPoolSingleton {
/**
* 线程池实例
*/
INSTANCE;
private final ThreadPoolExecutor executor;
private final Logger logger = LoggerFactory.getLogger(ThreadPoolSingleton.class);
public ThreadPoolExecutor getInstance() {
return executor;
}
ThreadPoolSingleton() {
//记录异常
ThreadFactory namedThreadFactory = ThreadFactoryBuilder.create()
.setNamePrefix("admin-thread-")
.setUncaughtExceptionHandler((thread, throwable) -> logger.error("ThreadPool {} got exception", thread, throwable))
.build();
//核心线程数大小
final int corePoolSize = 4;
//最大线程数大小,阻塞队列占满后会在线程池中继续开启新的线程,总的线程数量不会大于最大线程数
final int maximumPoolSize = 4;
//最大线程数空闲后的存活时间
final long keepAliveTime = 1L;
//最大线程数空闲后的存活时间的单位
final TimeUnit timeUnit = TimeUnit.MINUTES;
//阻塞队列,核心线程数占满后会把新入线程放入阻塞队列
final ArrayBlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(10);
//拒绝策略,最大线程数占满之后会执行此策略
//由调用线程(提交任务的线程)处理该任务
final ThreadPoolExecutor.CallerRunsPolicy callerRunsPolicy = new ThreadPoolExecutor.CallerRunsPolicy();
//丢弃任务并抛出RejectedExecutionException异常
//final ThreadPoolExecutor.CallerRunsPolicy CALLER_RUNS_POLICY = new ThreadPoolExecutor.AbortPolicy();
//丢弃任务,不抛异常
//final ThreadPoolExecutor.CallerRunsPolicy CALLER_RUNS_POLICY = new ThreadPoolExecutor.DiscardPolicy();
//丢弃队列中最老的一个请求,也就是即将被执行的一个任务,并尝试再次提交当前任务
//final ThreadPoolExecutor.CallerRunsPolicy CALLER_RUNS_POLICY = new ThreadPoolExecutor.DiscardOldestPolicy();
executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, timeUnit, workQueue, namedThreadFactory, callerRunsPolicy);
}
/**
* 需要获取返回值调用此方法
*
* @Param callable
* @param <T>
* @return
*/
public static <T> Future<T> submit(Callable<T> callable) {
return INSTANCE.getInstance().submit(callable);
}
/**
* 不需要获取返回值调用此方法
*
* @param runnable
*/
public static void execute(Runnable runnable) {
INSTANCE.getInstance().execute(runnable);
}
}
使用示例
获取返回值
[Java] 纯文本查看 复制代码
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
public class Main {
public static void main(String[] args) {
// 创建一个 Callable 任务
Callable<Integer> callable = new Callable<Integer>() {
@Override
public Integer call() throws Exception {
// 执行耗时操作,并返回结果
return longRunningTask();
}
};
// 提交任务并获取 Future 对象
Future<Integer> future = ThreadPoolSingleton.submit(callable);
try {
// 阻塞等待任务执行完成并获取结果
int result = future.get();
System.out.println("任务执行结果: " + result);
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
private static int longRunningTask() {
// 执行耗时操作,这里假设返回一个数值
return 42;
}
}
不需要返回值
[Asm] 纯文本查看 复制代码
public class Main {
public static void main(String[] args) {
// 创建一个实现了 Runnable 接口的任务
Runnable runnable = new Runnable() {
@Override
public void run() {
// 执行耗时操作
longRunningTask();
}
};
// 提交任务
ThreadPoolSingleton.execute(runnable);
}
private static void longRunningTask() {
// 执行耗时操作
System.out.println("任务执行中...");
}
}
|