public void saveList(String s) throws ExecutionException, InterruptedException, TimeoutException {
String orgId = "1111";
Map<String, Object> params = new HashMap<>();
params.put("orgId", orgId);
//如果没有传参 取昨天的0点到24点的时间戳
if (StringUtils.isEmpty(s)) {
params.put("startTime", LocalDateTime.of(LocalDate.now(), LocalTime.MIN).toInstant(ZoneOffset.ofHours(8)).toEpochMilli() - 86400000L);
params.put("endTime", LocalDateTime.of(LocalDate.now(), LocalTime.MAX).toInstant(ZoneOffset.ofHours(8)).toEpochMilli() - 86400000L);
} else {
//有值取指定的时间戳
String[] split = s.split(",");
params.put("startTime", Long.valueOf(split[0]));
params.put("endTime", Long.valueOf(split[1]));
}
CompletableFuture future2 = CompletableFuture.runAsync(() -> {
try {
synchronized (params){
params.put("type",2);
List<RfTxjobImplement> list = rfTxjobImplementDao.getListByOrgId(params);
if (list != null && list.size() > 0) {
rfTxjobImplementDao.saveByList(list, 2);
}
}
// Map<String, Object> params2 = params;
// params2.put("type", 2);
//
// List<RfTxjobImplement> list = rfTxjobImplementDao.getListByOrgId(params2);
// if (list != null && list.size() > 0) {
// rfTxjobImplementDao.saveByList(list, 2);
//
// }
} catch (Exception e) {
log.error("错误" + e.toString());
}
});
CompletableFuture future3 = CompletableFuture.runAsync(() -> {
try {
synchronized (params){
params.put("type",3);
List<RfTxjobImplement> list = rfTxjobImplementDao.getListByOrgId(params);
if (list != null && list.size() > 0) {
rfTxjobImplementDao.saveByList(list, 3);
}
}
// Map<String, Object> params3 = params;
// params3.put("type", 3);
//
// List<RfTxjobImplement> list = rfTxjobImplementDao.getListByOrgId(params3);
// if (list!=null &&list.size()>0){
// rfTxjobImplementDao.saveByList(list, 3);
//
// }
} catch (Exception e) {
log.error("错误" + e.toString());
}
});
CompletableFuture future4 = CompletableFuture.runAsync(() -> {
try {
synchronized (params){
params.put("type",4);
List<RfTxjobImplement> list = rfTxjobImplementDao.getListByOrgId(params);
if (list != null && list.size() > 0) {
rfTxjobImplementDao.saveByList(list, 4);
}
}
// Map<String, Object> params4 = params;
// params4.put("type", 4);
// List<RfTxjobImplement> list = rfTxjobImplementDao.getListByOrgId(params4);
// if (list!=null &&list.size()>0){
// rfTxjobImplementDao.saveByList(list, 4);
//
// }
} catch (Exception e) {
log.error("错误" + e.toString());
}
});
future2.get(5, TimeUnit.SECONDS);
future3.get(5, TimeUnit.SECONDS);
future4.get(5, TimeUnit.SECONDS);
}
三个异步线程都操作了map,然后整个线程里的逻辑都在同步代码块内,三个线程的效率是不是和写成同步的一样了
如果是现在写法的话,确实是差不多的