public static void doThreadBusiness(List<String> totalList) {
Long startTime = System.currentTimeMillis();
System.out.println("本次更新任务开始");
System.out.println("本机CPU核心数:"+Runtime.getRuntime().availableProcessors());
List<String> updateList = new ArrayList();
// 初始化线程池, 参数一定要一定要一定要调好!!!!
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(10, 50,
4, TimeUnit.SECONDS, new ArrayBlockingQueue(10), new ThreadPoolExecutor.DiscardPolicy());
// 大集合拆分成N个小集合,然后用多线程去处理数据,确保不会因为数据量过大导致执行过慢
List<List<String>> splitNList = SplitListUtils.splitList(totalList, 5000);
// 记录单个任务的执行次数
CountDownLatch countDownLatch = new CountDownLatch(splitNList.size());
// 对拆分的集合进行批量处理, 先拆分的集合, 再多线程执行
for (List<String> singleList : splitNList) {
// 线程池执行
threadPool.execute(new Thread(new Runnable(){
@Override
public void run() {
//模拟执行时间
System.out.println("当前线程:"+Thread.currentThread().getName());
List<String> batchUpdateVipList = new ArrayList<>();
for (String str : singleList) {
//组装要执行的批量更新数据
batchUpdateVipList.add(str);
}
// 这里模拟执行数据库批量更新操作
System.out.println("本次批量更新数据量:"+ batchUpdateVipList.size());
// 任务个数 - 1, 直至为0时唤醒await()
countDownLatch.countDown();
}
}));
}
try {
// 让当前线程处于阻塞状态,直到锁存器计数为零
countDownLatch.await();
} catch (Exception e) {
System.out.println("系统出现异常");
}
Long endTime = System.currentTimeMillis();
Long useTime = endTime - startTime;
System.out.println("本次更新任务结束,共计用时"+useTime+"毫秒");
}