@GetMapping("/test2")
public Map<String, Object> test2() {
long start = System.nanoTime();
Map<String, Object> result = new ConcurrentHashMap<>();
CompletableFuture.allOf(
CompletableFuture.runAsync(() -> {
result.put("key1", selectData1());
}, ForkJoinPool.commonPool()),
CompletableFuture.runAsync(() -> {
result.put("key2", selectData2());
}, ForkJoinPool.commonPool()),
CompletableFuture.runAsync(() -> {
result.put("key3", selectData3());
}, ForkJoinPool.commonPool())
).join();
long end = System.nanoTime();
System.out.printf("test2耗时: %d纳秒\n", (end - start));
return result;
}
并发执行代码段
最新推荐文章于 2024-02-23 13:02:02 发布