java 多线程处理一个list的集合
原文:http://blog.csdn.net/jenny8080/article/details/52100312
将一组数据平均分成n组 /** * 将一组数据平均分成n组 * * @param source 要分组的数据源 * @param n 平均分成n组 * @param <T> * @return */ public static <T> List<List<T>> averageAssign(List<T> source, int n) { List<List<T>> result = new ArrayList<List<T>>(); int remainder = source.size() % n; //(先计算出余数) int number = source.size() / n; //然后是商 int offset = 0;//偏移量 for (int i = 0; i < n; i++) { List<T> value = null; if (remainder > 0) { value = source.subList(i * number + offset, (i + 1) * number + offset + 1); remainder--; offset++; } else { value = source.subList(i * number + offset, (i + 1) * number + offset); } result.add(value); } return result; }
import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import com.ziyun.cms.util.CalcFoldMD5Util; import com.ziyun.cms.util.MD5Util; public class App { public static void main(String[] args) { try { long start = System.currentTimeMillis(); String folderPath= "D:\\test\\ABC"; List<File> list = CalcFoldMD5Util.getFileList(new File(folderPath)); Map map = averageAssign(list, 0, folderPath); long end = System.currentTimeMillis(); System.out.println((end - start)/1000 + "秒"); map.forEach((key, value) -> { System.out.println(key + ":" + value); }); } catch (Exception e) { e.printStackTrace(); } } /** * 将一组数据平均分成n组 * * @param source 要分组的数据源 * @param n 平均分成n组 * @param <T> * @return * @throws ExecutionException * @throws InterruptedException */ public static Map averageAssign(List<File> source, final int nThreads, String path) throws InterruptedException, ExecutionException { if (source == null || source.isEmpty()) { return null; } ExecutorService executorService = Executors.newFixedThreadPool(nThreads); List<Future<Map>> futures = new ArrayList<Future<Map>>(nThreads); String path1 = path.replaceAll("\\\\", "/"); Map map = new HashMap(1000); int remainder = source.size() % nThreads; //(先计算出余数) int number = source.size() / nThreads; //然后是商 int offset = 0;//偏移量 for (int i = 0; i < nThreads; i++) { List<File> value = null; if (remainder > 0) { value = source.subList(i * number + offset, (i + 1) * number + offset + 1); remainder--; offset++; } else { value = source.subList(i * number + offset, (i + 1) * number + offset); } final List<File> subList = value; Callable<Map> task = new Callable<Map>() { public Map call() throws Exception { Map<String, String> map = new HashMap<String, String>(300); for (File file : subList) { //文件夹不比较md5值 if (file.isFile()) { String key = file.getAbsolutePath().replaceAll("\\\\", "/").replace(path1, ""); String md5 = MD5Util.calcMD5(file); map.put(key, md5); } } return map; } }; futures.add(executorService.submit(task)); } for (Future<Map> future : futures) { map.putAll(future.get()); } executorService.shutdown();
// 等待子线程结束,再继续执行下面的代码
executorService.awaitTermination(180, TimeUnit.SECONDS);
return map; } }
看完打开支付宝扫一扫领个红包吧!