最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring Boot分段處理List集合多線程批量插入數(shù)據(jù)的解決方案

 更新時(shí)間:2024年04月26日 09:54:11   作者:濤哥是個(gè)大帥比  
大數(shù)據(jù)量的List集合,需要把List集合中的數(shù)據(jù)批量插入數(shù)據(jù)庫(kù)中,本文給大家介紹Spring Boot分段處理List集合多線程批量插入數(shù)據(jù)的解決方案,感興趣的朋友跟隨小編一起看看吧

項(xiàng)目場(chǎng)景:

大數(shù)據(jù)量的List集合,需要把List集合中的數(shù)據(jù)批量插入數(shù)據(jù)庫(kù)中。

解決方案:

拆分list集合后,然后使用多線程批量插入數(shù)據(jù)庫(kù)

1.實(shí)體類(lèi)

package com.test.entity;
import lombok.Data;
@Data
public class TestEntity {
	private String id;
	private String name;
}

2.Mapper

如果數(shù)據(jù)量不大,用foreach標(biāo)簽就足夠了。如果數(shù)據(jù)量很大,建議使用batch模式。

package com.test.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import com.test.entity.TestEntity;
public interface TestMapper {
	/**
	  * 1.用于使用batch模式,ExecutorType.BATCH開(kāi)啟批處理模式
	  * 數(shù)據(jù)量很大,推薦這種方式
	  */
	@Insert("insert into test(id, name) "
			   + " values"
			   + " (#{id,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR})")
	void testInsert(TestEntity testEntity);
	/**
	  * 2.使用foreach標(biāo)簽,批量保存
	  * 數(shù)據(jù)量少可以使用這種方式
	  */
	@Insert("insert into test(id, name) "
			   + " values"
			   + " <foreach collection='list' item='item' index='index' separator=','>"
			   + " (#{item.id,jdbcType=VARCHAR}, #{item.name,jdbcType=VARCHAR})"
			   + " </foreach>")
	void testBatchInsert(@Param("list") List<TestEntity> list);
}

3.spring容器注入線程池bean對(duì)象

package com.test.config;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
@Configuration
@EnableAsync
public class ExecutorConfig {
    /**
     * 異步任務(wù)自定義線程池
     */
    @Bean(name = "asyncServiceExecutor")
    public Executor asyncServiceExecutor() {
    	ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //配置核心線程數(shù)
        executor.setCorePoolSize(50);
        //配置最大線程數(shù)
        executor.setMaxPoolSize(500);
        //配置隊(duì)列大小
        executor.setQueueCapacity(300);
        //配置線程池中的線程的名稱(chēng)前綴
        executor.setThreadNamePrefix("testExecutor-");
        // rejection-policy:當(dāng)pool已經(jīng)達(dá)到max size的時(shí)候,如何處理新任務(wù)
        // CALLER_RUNS:不在新線程中執(zhí)行任務(wù),而是有調(diào)用者所在的線程來(lái)執(zhí)行
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //調(diào)用shutdown()方法時(shí)等待所有的任務(wù)完成后再關(guān)閉
        executor.setWaitForTasksToCompleteOnShutdown(true);
        //等待所有任務(wù)完成后的最大等待時(shí)間
		executor.setAwaitTerminationSeconds(60);
        return executor;
    }
}

4.創(chuàng)建異步線程業(yè)務(wù)類(lèi)

package com.test.service;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.test.entity.TestEntity;
import com.test.mapper.TestMapper;
@Service
public class AsyncService {
	@Autowired
	private SqlSessionFactory sqlSessionFactory;
	@Async("asyncServiceExecutor")
    public void executeAsync(List<String> logOutputResults, CountDownLatch countDownLatch) {
        try{
        	//獲取session,打開(kāi)批處理,因?yàn)槭嵌嗑€程,所以每個(gè)線程都要開(kāi)啟一個(gè)事務(wù)
        	SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH);
        	TestMapper mapper = session.getMapper(TestMapper.class);
            //異步線程要做的事情
        	for (int i = 0; i < logOutputResults.size(); i++) {
    			System.out.println(Thread.currentThread().getName() + "線程:" + logOutputResults.get(i));
    			TestEntity test = new TestEntity();
    			//test.set()
    			//.............
    			//批量保存
    			mapper.testInsert(test);
    			//每1000條提交一次防止內(nèi)存溢出
    			if(i%1000==0){
    				session.flushStatements();
    			}
			}
        	//提交剩下未處理的事務(wù)
    		session.flushStatements();
        }finally {
            countDownLatch.countDown();// 很關(guān)鍵, 無(wú)論上面程序是否異常必須執(zhí)行countDown,否則await無(wú)法釋放
        }
    }
}

5.拆分list調(diào)用異步的業(yè)務(wù)方法

package com.test.service;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
@Service
public class TestService {
	@Resource
	private AsyncService asyncService;
	public int testMultiThread() {
        List<String> logOutputResults = getTestData();
        //按線程數(shù)拆分后的list
        List<List<String>> lists = splitList(logOutputResults);
        CountDownLatch countDownLatch = new CountDownLatch(lists.size());
        for (List<String> listSub:lists) {
            asyncService.executeAsync(listSub, countDownLatch);
        }
        try {
            countDownLatch.await(); //保證之前的所有的線程都執(zhí)行完成,才會(huì)走下面的;
            // 這樣就可以在下面拿到所有線程執(zhí)行完的集合結(jié)果
        } catch (Exception e) {
            e.printStackTrace();
        }
        return logOutputResults.size();
    }
	public List<String> getTestData() {
		List<String> logOutputResults = new ArrayList<String>();
        for (int i = 0; i < 3000; i++) {
        	logOutputResults.add("測(cè)試數(shù)據(jù)"+i);
		}
        return logOutputResults;
    }
	public List<List<String>> splitList(List<String> logOutputResults) {
		List<List<String>> results = new ArrayList<List<String>>();
		/*動(dòng)態(tài)線程數(shù)方式*/
		// 每500條數(shù)據(jù)開(kāi)啟一條線程
		int threadSize = 500;
		// 總數(shù)據(jù)條數(shù)
		int dataSize = logOutputResults.size();
		// 線程數(shù),動(dòng)態(tài)生成
		int threadNum = dataSize / threadSize + 1;
	    /*固定線程數(shù)方式
		    // 線程數(shù)
		    int threadNum = 6;
		    // 總數(shù)據(jù)條數(shù)
		    int dataSize = logOutputResults.size();
		    // 每一條線程處理多少條數(shù)據(jù)
		    int threadSize = dataSize / (threadNum - 1);
	    */
		// 定義標(biāo)記,過(guò)濾threadNum為整數(shù)
		boolean special = dataSize % threadSize == 0;
		List<String> cutList = null;
		// 確定每條線程的數(shù)據(jù)
		for (int i = 0; i < threadNum; i++) {
			if (i == threadNum - 1) {
				if (special) {
					break;
				}
				cutList = logOutputResults.subList(threadSize * i, dataSize);
			} else {
				cutList = logOutputResults.subList(threadSize * i, threadSize * (i + 1));
			}
			results.add(cutList);
		}
        return results;
    }
}

6.Controller測(cè)試

@RestController
public class TestController {
	@Resource
	private TestService testService;
	@RequestMapping(value = "/log", method = RequestMethod.GET)
	@ApiOperation(value = "測(cè)試")
	public String test() {
		testService.testMultiThread();
		return "success";
	}
}

總結(jié):

注意這里執(zhí)行插入的數(shù)據(jù)是無(wú)序的。

擴(kuò)展:Java多線程分段處理List集合

項(xiàng)目場(chǎng)景:

大數(shù)據(jù)量的List集合,需要把List集合中的數(shù)據(jù)批量插入數(shù)據(jù)庫(kù)中。

解決方案:

拆分list集合后,然后使用多線程實(shí)現(xiàn)

public static void main(String[] args) throws Exception {
	// 開(kāi)始時(shí)間
	long start = System.currentTimeMillis();
	List<String> list = new ArrayList<String>();
	for (int i = 1; i <= 3000; i++) {
		list.add(i + "");
	}
    /*動(dòng)態(tài)線程數(shù)方式*/
	// 每500條數(shù)據(jù)開(kāi)啟一條線程
	int threadSize = 500;
	// 總數(shù)據(jù)條數(shù)
	int dataSize = list.size();
	// 線程數(shù),動(dòng)態(tài)生成
	int threadNum = dataSize / threadSize + 1;
    /*固定線程數(shù)方式
	    // 線程數(shù)
	    int threadNum = 6;
	    // 總數(shù)據(jù)條數(shù)
	    int dataSize = list.size();
	    // 每一條線程處理多少條數(shù)據(jù)
	    int threadSize = dataSize / (threadNum - 1);
    */
	// 定義標(biāo)記,過(guò)濾threadNum為整數(shù)
	boolean special = dataSize % threadSize == 0;
	// 創(chuàng)建一個(gè)線程池
	ExecutorService exec = Executors.newFixedThreadPool(threadNum);
	// 定義一個(gè)任務(wù)集合
	List<Callable<Integer>> tasks = new ArrayList<Callable<Integer>>();
	Callable<Integer> task = null;
	List<String> cutList = null;
	// 確定每條線程的數(shù)據(jù)
	for (int i = 0; i < threadNum; i++) {
		if (i == threadNum - 1) {
			if (special) {
				break;
			}
			cutList = list.subList(threadSize * i, dataSize);
		} else {
			cutList = list.subList(threadSize * i, threadSize * (i + 1));
		}
		final List<String> listStr = cutList;
		task = new Callable<Integer>() {
			@Override
			public Integer call() throws Exception {
				//業(yè)務(wù)邏輯,循環(huán)處理分段后的list
				System.out.println(Thread.currentThread().getName() + "線程:" + listStr);
				//......
				int logCount = 0;	//記錄下數(shù)量
				for (int j = 0; j < listStr.size(); j++) {
					logCount++;
				}
				return logCount;
			}
		};
		// 這里提交的任務(wù)容器列表和返回的Future列表存在順序?qū)?yīng)的關(guān)系
		tasks.add(task);
	}
	exec.invokeAll(tasks);
	//總數(shù)
    int total = 0;
    try {
        List<Future<Integer>> results = exec.invokeAll(tasks);
        for (Future<Integer> future : results) {
            //累計(jì)線程處理的總記錄數(shù)
            total+=future.get();
        }
        // 關(guān)閉線程池
        exec.shutdown();
	} catch (Exception e) {
		e.printStackTrace();
	}
	System.out.println("線程任務(wù)執(zhí)行結(jié)束");
	System.out.println("總共處理了"+total+"條數(shù)據(jù),消耗了 :" + (System.currentTimeMillis() - start) + "毫秒");
}

到此這篇關(guān)于Spring Boot分段處理List集合多線程批量插入數(shù)據(jù)的文章就介紹到這了,更多相關(guān)Spring Boot批量插入數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Mybatis-plus實(shí)現(xiàn)主鍵自增和自動(dòng)注入時(shí)間的示例代碼

    Mybatis-plus實(shí)現(xiàn)主鍵自增和自動(dòng)注入時(shí)間的示例代碼

    這篇文章主要介紹了Mybatis-plus實(shí)現(xiàn)主鍵自增和自動(dòng)注入時(shí)間的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • java+vue實(shí)現(xiàn)添加單選題、多選題到題庫(kù)功能

    java+vue實(shí)現(xiàn)添加單選題、多選題到題庫(kù)功能

    這篇文章主要為大家詳細(xì)介紹了java+vue實(shí)現(xiàn)添加單選題、多選題到題庫(kù)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • java檢查數(shù)組是否有重復(fù)元素的方法

    java檢查數(shù)組是否有重復(fù)元素的方法

    這篇文章主要介紹了java檢查數(shù)組是否有重復(fù)元素的方法,涉及java針對(duì)數(shù)組元素的操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-07-07
  • java生成xml格式文件的方法

    java生成xml格式文件的方法

    這篇文章主要介紹了java生成xml格式文件的方法,涉及java節(jié)點(diǎn)遍歷與屬性操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2016-07-07
  • Jenkins源代碼管理SVN實(shí)現(xiàn)步驟解析

    Jenkins源代碼管理SVN實(shí)現(xiàn)步驟解析

    這篇文章主要介紹了Jenkins源代碼管理SVN實(shí)現(xiàn)步驟解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • Java項(xiàng)目中防止SQL注入的四種方法推薦

    Java項(xiàng)目中防止SQL注入的四種方法推薦

    sql注入是web開(kāi)發(fā)中最常見(jiàn)的一種安全漏洞,這篇文章為大家整理了四種Java項(xiàng)目中防止SQL注入的方法,有需要的小伙伴可以參考一下
    2025-03-03
  • 基于SpringBoot打造RESTful API實(shí)戰(zhàn)指南

    基于SpringBoot打造RESTful API實(shí)戰(zhàn)指南

    本文詳細(xì)介紹了RESTful API的基本概念、設(shè)計(jì)規(guī)范以及與非RESTful風(fēng)格的對(duì)比,通過(guò)Spring Boot實(shí)戰(zhàn)部分,展示了如何實(shí)現(xiàn)一個(gè)簡(jiǎn)單的RESTful API,需要的朋友可以參考下
    2025-12-12
  • Spring?boot2.0?實(shí)現(xiàn)日志集成的方法(2)

    Spring?boot2.0?實(shí)現(xiàn)日志集成的方法(2)

    這篇文章主要介紹了Spring?boot2.0?實(shí)現(xiàn)日志集成的方法,上一章講解了spring?boot日志簡(jiǎn)單集成,這篇我們將日志進(jìn)行分類(lèi),常規(guī)日志、異常日志、監(jiān)控日志等,需要將日志輸出到不同的文件,具體內(nèi)容需要的小伙伴可以參考一下
    2022-04-04
  • SpringBoot+Mybatis+Vue 實(shí)現(xiàn)商品模塊的crud操作

    SpringBoot+Mybatis+Vue 實(shí)現(xiàn)商品模塊的crud操作

    這篇文章主要介紹了SpringBoot+Mybatis+Vue 實(shí)現(xiàn)商品模塊的crud操作,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • 詳解SpringMVC中的四種跳轉(zhuǎn)方式、視圖解析器問(wèn)題

    詳解SpringMVC中的四種跳轉(zhuǎn)方式、視圖解析器問(wèn)題

    這篇文章主要介紹了SpringMVC的四種跳轉(zhuǎn)方式、視圖解析器,springmvc核心配置文件和視圖解析器的使用,添加視圖解析器,通過(guò)案例講解四種跳轉(zhuǎn)方式,需要的朋友可以參考下
    2022-10-10

最新評(píng)論

华蓥市| 绥化市| 沛县| 闵行区| 江口县| 抚松县| 浏阳市| 兴国县| 吴江市| 岳阳市| 拜泉县| 九龙城区| 江达县| 周口市| 鄢陵县| 郁南县| 朝阳县| 水城县| 三门峡市| 阿拉善右旗| 海盐县| 保靖县| 民丰县| 肥西县| 高州市| 行唐县| 遂昌县| 沾益县| 平邑县| 南充市| 莲花县| 庆阳市| 舞阳县| 丰都县| 公安县| 甘孜| 白河县| 德惠市| 荥阳市| 名山县| 西畴县|