//多线程批量新增数据
private void batchDeal(List<User> data, int batchNum, String time) throws InterruptedException {
int totalNum = data.size();
int pageNum = totalNum % batchNum == 0 ? totalNum / batchNum : totalNum / batchNum + 1;
ExecutorService executor = Executors.newFixedThreadPool(pageNum);
try {
CountDownLatch countDownLatch = new CountDownLatch(pageNum);
List<User> subData = null;
int fromIndex, toIndex;
for (int i = 0; i < pageNum; i++) {
fromIndex = i * batchNum;
toIndex = Math.min(totalNum, fromIndex + batchNum);
subData = data.subList(fromIndex, toIndex);
ImportTask task = new ImportTask(subData, countDownLatch, time);
executor.execute(task);
}
// 主线程必须在启动其它线程后立即调用CountDownLatch.await()方法,
// 这样主线程的操作就会在这个方法上阻塞,直到其它线程完成各自的任务。
// 计数器的值等于0时,主线程就能通过await()方法恢复执行自己的任务。
countDownLatch.await();
} finally {
// 关闭线程池,释放资源
executor.shutdown();
}
}
class ImportTask implements Runnable {
private List<User> list;
private String time;
private CountDownLatch countDownLatch;
public ImportTask(List<User> data, CountDownLatch countDownLatch, String time) {
this.list = data;
this.time = time;
this.countDownLatch = countDownLatch;
}
@Override
public void run() {
if (null != list) {
shujuDao.insertYsxmJhBatch(list, time);
}
// 发出线程任务完成的信号
countDownLatch.countDown();
}
}
//分割list数组
public static List<List<User>> splitList(List<User> sourceList, int groupSize) {
int length = sourceList.size();
// 计算可以分成多少组
int num = (length + groupSize - 1) / groupSize;
List<List<RCSJ_YSXMJH>> newList = new ArrayList<>(num);
for (int i = 0; i < num; i++) {
// 开始位置
int fromIndex = i * groupSize;
// 结束位置
int toIndex = (i + 1) * groupSize < length ? (i + 1) * groupSize : length;
newList.add(sourceList.subList(fromIndex, toIndex));
}
return newList;
}
多线程批量新增数据
最新推荐文章于 2024-05-23 10:54:59 发布