public void asynLogin() {
ExecutorService executor = new ThreadPoolExecutor(5, 5, 100, TimeUnit.SECONDS, new LinkedBlockingDeque<>());
try {
Future<Map<String, Object>> mapFuture = executor.submit(new Callable<Map<String, Object>>() {
@Override
public Map<String, Object> call() throws Exception {
Map<String, Object> roleMap = new HashMap<String, Object>();
roleMap.put("role", "管理员");
long endTime = System.currentTimeMillis();
log.info("查询用户角色信息耗时:" + (endTime - startTime) + "毫秒");
return roleMap;
}
});
mapFuture.get();
} catch (Exception e) {
e.printStackTrace();
}
CompletableFuture<Map<String, Object>> rote = CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Map<String, Object> roleMap = new HashMap<String, Object>();
roleMap.put("role", "管理员");
long endTime = System.currentTimeMillis();
log.info("查询用户角色信息耗时:" + (endTime - startTime) + "毫秒");
return roleMap;
});
rote.join();
CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Map<String, Object> roleMap = new HashMap<String, Object>();
roleMap.put("role", "管理员");
long endTime = System.currentTimeMillis();
log.info("查询用户角色信息耗时:" + (endTime - startTime) + "毫秒");
return roleMap;
}, executor);
CompletableFuture.runAsync(() -> {
long startTime = System.currentTimeMillis();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info("执行金额增删改操作用时:" + (System.currentTimeMillis() - startTime) / 1000 + "秒");
}).join();
CompletableFuture<Void> roleCompletableFuture = CompletableFuture.runAsync(() -> {
long startTime = System.currentTimeMillis();
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info("执行角色增删改操作用时:" + (System.currentTimeMillis() - startTime) / 1000 + "秒");
}, executor);
roleCompletableFuture.join();
}
public void thenRun(){
CompletableFuture completableFuture = CompletableFuture.runAsync(()->{
long startTime = System.currentTimeMillis();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info("执行金额增删改操作用时:" + (System.currentTimeMillis() - startTime) / 1000 + "秒");
});
CompletableFuture completableFuture1 = completableFuture.thenRunAsync(()->{
long startTime = System.currentTimeMillis();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info("执行金额增删改操作用时:" + (System.currentTimeMillis() - startTime) / 1000 + "秒");
});
completableFuture1.join();
}
public void thenAccept(){
CompletableFuture<Map> completableFuture = CompletableFuture.supplyAsync(()->{
long startTime = System.currentTimeMillis();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Map<String, Object> amountMap = new HashMap<String, Object>();
amountMap.put("amount", 90);
log.info("执行金额查询操作用时:" + (System.currentTimeMillis() - startTime) / 1000 + "秒");
return amountMap;
});
CompletableFuture completableFuture1 = completableFuture.thenAcceptAsync((map)->{
long startTime = System.currentTimeMillis();
if (Integer.parseInt(map.get("amount").toString()) > 90) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info("金额充足,可以购买!:" + (System.currentTimeMillis() - startTime) / 1000 + "秒");
} else {
log.info("金额不足,无法购买!:" + (System.currentTimeMillis() - startTime) / 1000 + "秒");
}
});
completableFuture1.join();
}
public void thenApply(){
CompletableFuture<Map> completableFuture = CompletableFuture.supplyAsync(()->{
long startTime = System.currentTimeMillis();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Map<String, Object> amountMap = new HashMap<String, Object>();
amountMap.put("amount", 90);
log.info("执行金额查询操作用时:" + (System.currentTimeMillis() - startTime) / 1000 + "秒");
return amountMap;
});
CompletableFuture<Map> completableFuture1 = completableFuture.thenApply(map -> {
long startTime = System.currentTimeMillis();
Map<String, Object> numberMap = null;
if (Integer.parseInt(map.get("amount").toString()) > 90) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
numberMap = new HashMap<String, Object>();
numberMap.put("number", 90);
log.info("金额充足,可以购买!:" + (System.currentTimeMillis() - startTime) / 1000 + "秒");
} else {
log.info("金额不足,无法购买!:" + (System.currentTimeMillis() - startTime) / 1000 + "秒");
}
return numberMap;
});
completableFuture1.join();
}