package com.vanke.sampling.dao; import com.gexin.rp.sdk.base.uitls.RandomUtil; import com.google.common.collect.Lists; import java.text.SimpleDateFormat; import java.util.*; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class test { public static void main(String[] args) throws InterruptedException { SimpleDateFormat fmt=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss:SSS'Z'"); List<String> stringList=new ArrayList<>(); for (int i=0;i<20;i++){ stringList.add(RandomUtil.randomUUID()); } //TODO:总线程数 10 final Integer threadSize=10;//Runtime.getRuntime().availableProcessors(); //TODO:总数据量 40w final Long total=Long.parseLong(String.valueOf(stringList.size())); if (total>0){ //TODO:每个线程将执行插入操作的数据条目 List<String> temp; List<test1> list= Lists.newLinkedList(); Long pageSize=(total%threadSize==0)?total/threadSize:total/threadSize+1; ExecutorService executorService=Executors.newFixedThreadPool(threadSize); for (int i=1;i<=threadSize;i++){ temp= page(stringList,2,i); list.add(new test1(temp)); } //TODO:多线程批量插入数据-逻辑 executorService.invokeAll(list); } } public static List<String> page(List<String> dataList, int pageSize,int currentPage) { List<String> currentPageList = new ArrayList<>(); if (dataList != null && dataList.size() > 0) { int currIdx = (currentPage > 1 ? (currentPage - 1) * pageSize : 0); for (int i = 0; i < pageSize && i < dataList.size() - currIdx; i++) { String data = dataList.get(currIdx + i); currentPageList.add(data); } } return currentPageList; } } class test1 implements Callable<Boolean>{ private List<String> list; test1(List<String> teststr){ list=teststr; } @Override public Boolean call() throws Exception{ if(!list.isEmpty()){
// dosomething 这里的业务逻辑实例只能手动管理维护,不能依赖于@Autowried for (String str:list){ System.out.println(Thread.currentThread().getName()+"-"+str); } } return true; } }