import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
/**
* 多线程操作工具类
*/
@Slf4j
public class ExecutorUtils {
// private static final int CORE_POOL_SIZE = 5; //核心线程数为 5
// private static final int MAX_POOL_SIZE = 10; //最大线程数 10
// private static final int QUEUE_CAPACITY = 100; //
// private static final Long KEEP_ALIVE_TIME = 1L; //当线程数大于核心线程数时,多余的空闲线程存活的最长时间
// 线程数,以2000条数据为一个线程,为数据大小除以2000,再加1
private static final int THREAD_COUNT = 2000;
public static void executeThreadPool(List<Object> pmsProductList) {
long start = System.currentTimeMillis();
//计数器
int size = 0;
int round = pmsProductList.size() / THREAD_COUNT + 1;
final CountDownLatch count = new CountDownLatch(round);
//使用阿里巴巴推荐的创建线程池的方式
//通过ThreadPoolExecutor构造函数自定义参数创建
// ThreadPoolExecutor executor = new ThreadPoolExecutor(
// CORE_POOL_SIZE,
// MAX_POOL_SIZE,
// KEEP_ALIVE_TIME, //当线程数大于核心线程数时,多余的空闲线程存活的最长时间
// TimeUnit.SECONDS, //时间单位
// new ArrayBlockingQueue<>(QUEUE_CAPACITY), //任务队列,用来储存等待执行任务的队列
// new ThreadPoolExecutor.CallerRunsPolicy()); //饱和策略,简单点说就是后面排队的线程就在那儿等着。
// //被拒绝的任务在主线程中运行,所以主线程就被阻塞了,别的任务只能在被拒绝的任务执行完之后才会继续被提交到线程池执行
ExecutorService executor = Executors.newFixedThreadPool(round);
for (int i = 0; i < round; i++) {
int startLen = i * THREAD_COUNT;
int endLen = ((i + 1) * THREAD_COUNT > pmsProductList.size() ? pmsProductList.size() : (i + 1) * THREAD_COUNT);
final List<Object> threadList = pmsProductList.subList(startLen, endLen);
size = size + threadList.size();
//调用业务逻辑代码
final int dd = i;
executor.execute(new Runnable() {
@Override
public void run() {
//log.info("第" + (dd + 1) + "批次插入成功");
for (int j = 0; j < threadList.size(); j++) {
//调用插入数据方法
Object o = threadList.get(j);
//System.out.println(o);
}
count.countDown();
}
});
}
//System.err.println("插入数据总条数:" + size);
//boolean isTerminated()
//若关闭后所有任务都已完成,则返回true。注意除非首先调用shutdown或shutdownNow,否则isTerminated永不为true。
// while (!executor.isTerminated()) {
//System.out.println("线程池还没有完全关闭!!!");
// }
try {
count.await();
long end = System.currentTimeMillis();
log.info("10万数据插入查询耗时:" + (end - start) + "ms");
} catch (Exception e) {
e.printStackTrace();
} finally {
// 终止线程池
// 启动一次顺序关闭,执行以前提交的任务,但不接受新任务。若已经关闭,则调用没有其他作用。
executor.shutdown();
}
}
public static void main(String[] args) {
List<Object> list = new ArrayList<>();
for (int i = 1; i <= 100000; i++) {
list.add(i + "");
}
executeThreadPool(list);
}
参考:
https://blog.csdn.net/javaloveiphone/article/details/54729821
https://www.it610.com/article/1288646108557418496.htm
https://www.cnblogs.com/bulrush/p/12547343.html
https://www.cnblogs.com/liuyi-clover/p/13332594.html