并发请求案列


public static void main(String[] args) throws ExecutionException, InterruptedException {
    long start = System.currentTimeMillis();
    List address = Arrays.asList("", "2", "3");
    HashMap> result = new HashMap<>();
    for (String addr : address) {
        Future future = CacheThreadPool.getExecutor().submit(() -> getAmount(addr));
        result.put(addr, future);
    }

    for (Future value : result.values()) {
        System.out.println(value.get());
    }
    System.out.println(System.currentTimeMillis() - start);

}

public static double getAmount(String addr) throws InterruptedException {
    //此处处理http请求
    Thread.sleep(2000);

    return 100;
}

 

你可能感兴趣的:(java)