importjava.io.File;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.List;importjava.util.Map;importjava.util.concurrent.Callable;importjava.util.concurrent.ExecutionException;importjava.util.concurrent.ExecutorService;importjava.util.concurrent.Executors;importjava.util.concurrent.Future;importcom.ziyun.cms.util.CalcFoldMD5Util;importcom.ziyun.cms.util.MD5Util;public classApp {public static voidmain(String[] args) {try{long start =System.currentTimeMillis();
String folderPath= "D:\\test\\ABC";
List list = CalcFoldMD5Util.getFileList(newFile(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组
*
*@paramsource 要分组的数据源
*@paramn 平均分成n组
*@param
*@return*@throwsExecutionException
*@throwsInterruptedException*/
public static Map averageAssign(List source, final int nThreads, String path) throwsInterruptedException, ExecutionException {if (source == null ||source.isEmpty()) {return null;
}
ExecutorService executorService=Executors.newFixedThreadPool(nThreads);
List> futures = new ArrayList>(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 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 subList =value;
Callable task = new Callable() {public Map call() throwsException {
Map map = new HashMap(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);
}
}returnmap;
}
};
futures.add(executorService.submit(task));
}for (Futurefuture : futures) {
map.putAll(future.get());
}
executorService.shutdown();