java多线程批量处理list集合中的数据

public class ThreadList {

	public static void main(String[] args) throws InterruptedException, ExecutionException {
		List list = new ArrayList<>();
		
		for(int i=0;i<5300;i++){
			list.add(""+i);
		}
		int threadSize = 500;//每500条数据开启一个线程
		int remainder = list.size()%threadSize;
		int threadNum  = 0;//线程数
		if(remainder == 0){
			threadNum  = list.size()/threadSize;
		} else {
			threadNum  = list.size()/threadSize + 1;
		}
		
		ExecutorService eService = Executors.newFixedThreadPool(threadNum );//创建一个线程池
		
		List> cList = new ArrayList<>(); 
		Callable task = null;
		List sList = null;
		
		for(int i=0;i nowList = sList;
			task = new Callable() {
				@Override
				public String call() throws Exception {
					StringBuffer sb = new StringBuffer();
					for(int j=0;j> results = eService.invokeAll(cList);
		for(Future str:results){
			System.out.println(str.get());
		}
		eService.shutdown();
	}
}

 

你可能感兴趣的:(java多线程批量处理list集合中的数据)