利用多线程操作数据存表,保证事务正常

/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 ([email protected])
 */
package org.springblade.etl.ods.fd.service.impl;

import com.google.common.collect.Lists;
import org.springblade.etl.ods.fd.entity.OdsFdResultEntity;
import org.springblade.etl.ods.fd.vo.OdsFdResultVO;
import org.springblade.etl.ods.fd.excel.OdsFdResultExcel;
import org.springblade.etl.ods.fd.mapper.OdsFdResultMapper;
import org.springblade.etl.ods.fd.service.IOdsFdResultService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseServiceImpl;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.stream.Collectors;

/**
 * 新人日记-测评结果表 服务实现类
 *
 * @author BladeX
 * @since 2024-02-26
 */
@Service
public class OdsFdResultServiceImpl extends BaseServiceImpl implements IOdsFdResultService {

	@Override
	public IPage selectOdsFdResultPage(IPage page, OdsFdResultVO odsFdResult) {
		return page.setRecords(baseMapper.selectOdsFdResultPage(page, odsFdResult));
	}


	@Override
	public List exportOdsFdResult(Wrapper queryWrapper) {
		List odsFdResultList = baseMapper.exportOdsFdResult(queryWrapper);
		//odsFdResultList.forEach(odsFdResult -> {
		//	odsFdResult.setTypeName(DictCache.getValue(DictEnum.YES_NO, OdsFdResult.getType()));
		//});
		return odsFdResultList;
	}

	@Override
	public void removeAll() {
		baseMapper.removeAll();
	}


	@Resource
	@Qualifier("normalThreadPool")  // 使用您配置的线程池
	private Executor executorNormal;

	// 使用CompletableFuture确保多线程保存操作的正常回滚
	// 使用CompletableFuture确保多线程保存操作的正常回滚,并输出不同线程号执行任务的日志
	public void saveBatchInMultipleThreads(List odsFdResultEntities) {
		// 将odsFdResultEntities分成30份
		List> partitions = Lists.partition(odsFdResultEntities, odsFdResultEntities.size() / 30);

		// 使用CompletableFuture.allOf来等待所有任务完成
		List> futures = new ArrayList<>();
		for (int i = 0; i < partitions.size(); i++) {
			int partitionIndex = i;
			CompletableFuture future = CompletableFuture.runAsync(() -> {
				long threadId = Thread.currentThread().getId();
				System.out.println("Thread " + threadId + " is saving partition " + partitionIndex);
				saveBatch(partitions.get(partitionIndex));
			}, executorNormal);
			futures.add(future);
		}

		// 等待所有任务完成
		CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]))
			.join();
	}

}

你可能感兴趣的:(java,spring,boot)