@RestController
public class ThreadController {
@Resource(name = "taskExecutor")
private ThreadPoolTaskExecutor executor;
@GetMapping("/check")
public Map<String, Object> check() throws Exception{
Map<String, Object> map = new HashMap<>();
Future<Integer> pa = executor.submit(new CheckPrice(a));
Future<Integer> pb = executor.submit(new CheckPrice(b));
Future<Integer> pc = executor.submit(new CheckPrice(c));
Integer[] priceList = { pa.get(), pb.get(), pc.get() };
map.put("最小价格", Collections.min(Arrays.asList(priceList)));
return map;
}
}
假设每个 CheckPrice 需要 2 秒耗时, 有多人同时访问"/check"的时候, 怎样才能让每个人都只是等 2 秒, 而不用排队一个个处理?
貌似只要是需要等待 Future.get 结果的, controller 都是堵住的, CompletableFuture 也看了一个早上, 好像也会堵 controller, 或者返回空值的...