1.带返回值任务使用
countDownLatch用来计数 可以让程序顺序执行可以用在待返回值 任务也可以用在不带返回值任务
带返回值任务 Future .get()方法会阻塞代码 效果和countDownLatch 类似 但是如果不调用改方法 程序会异步执行
@RequestMapping("/test")
public String test() throws IOException {
FileOutputStream outputStream = null;
try {
File file = new File("F:\\需求\\济南税务需求\\济南测试数据.csv");
FileInputStream fileInputStream = new FileInputStream(file);
List<Map<String, String>> result = getAllColData(fileInputStream, "GBK");
List<Map<String, Object>> dataList = new ArrayList<>();
// result = result.subList(0, 10);
/* ExecutorService executor = Executors.newFixedThreadPool(50);
List<Future<List<Map<String, Object>>>> results = new ArrayList<Future<List<Map<String, Object>>>>();
result = result.subList(0, 10000);
List<List<Map<String, String>>> subList = Lists.partition(result, 100);
CountDownLatch countDownLatch = new CountDownLatch(subList.size());
for (List<Map<String, String>> li : subList) {
Future<List<Map<String, Object>>> threadResult =
executor.submit(new CompanyController.CompanyTask(li, countDownLatch));
results.add(threadResult);
}
countDownLatch.await();
for (Future<List<Map<String, Object>>> fut : results) {
System.out.println(JSON.toJSONString(fut.get()));
}
*/
int i = 1;
for (Map<String, String> map : result) {
Map<String, Object> rmap = new HashMap<>();
String id = map.get("id");
RespResult<List<ResultCompanyVo>> res = companyService.judgeCompanyAddr("370100", map.get("name"));
rmap.put("id", id);
rmap.put("name", map.get("name"));
String resStr = "";
if (res != null && res.getData() != null) {
resStr = res.getData().toString();
}
rmap.put("res", resStr);
dataList.add(rmap);
// System.out.println("第" + i + "条记录");
logger.info("第" + i + "条记录");
i++;
}
String[] headers = {"id", "name", "res"};
File resFile = new File("F:\\需求\\济南税务需求\\地址分离测试返回.csv");
outputStream = new FileOutputStream(resFile);
SupeCsvUtil.exportCsvByMap(headers, headers, dataList, "UTF-8", outputStream);
} catch (Exception e) {
e.printStackTrace();
} finally {
outputStream.close();
}
return "test";
}
// 修改业务块公司类型数据任务
private class CompanyTask implements Callable<List<Map<String, Object>>> {
private List<Map<String, String>> list;
private CountDownLatch countDownLatch;
public CompanyTask(List<Map<String, String>> list, CountDownLatch countDownLatch) {
this.list = list;
this.countDownLatch = countDownLatch;
}
@Override
public List<Map<String, Object>> call() throws Exception {
List<Map<String, Object>> dataList = new ArrayList<>();
int i = 1;
for (Map<String, String> map : list) {
System.out.println("第" + i + "条数据");
Map<String, Object> resMap = new HashMap();
ResultVO<List<ResultCompanyVo>> res = companyService.judgeCompanyAddr("370100", map.get("name"));
String resStr = "";
if (res != null && res.getData() != null) {
resStr = res.getData().toString();
}
resMap.put("id", map.get("id"));
resMap.put("name", map.get("name"));
resMap.put("res", resStr);
dataList.add(resMap);
i++;
}
countDownLatch.countDown();
return dataList;
}
}
2.如果执行不带返回值任务 任务实体类 实现runable 接口即可 使用和上面代码类似