工具类
package com.framework.common.threadPool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* @author yuer629
*/
public class ThreadPools {
public static ExecutorService exec = new ThreadPoolExecutor(
1, //核心线程池大小
5, //最大线程池大小
1000L, //线程最大空闲时间
TimeUnit.MILLISECONDS, //时间单位
new LinkedBlockingQueue<Runnable>(1024), //线程等待队列
new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
return new Thread(r);
}
}, //线程创建工厂
new ThreadPoolExecutor.AbortPolicy() //拒绝策略,这里直接抛出异常
);
}
public class ThreadPools {
public static ExecutorService exec = new ThreadPoolExecutor(
20,
300,
0L,
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(1024),
new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
return new Thread(r);
}
},
new ThreadPoolExecutor.AbortPolicy());
}
参数自行根据需求修改
调用工具类
ThreadPools.exec.execute(() -> {
//调用方法
});