public static void main(String[] args) {
String req = "xzh";
// 创建线程池
ExecutorService executor = newCachedThreadPool(Executors.defaultThreadFactory());
// 执行异步任务
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> doSomething(req), executor);
// 非阻塞处理执行结果
future.whenComplete((res, throwable) -> {
System.out.println(res);
});
}
private static String doSomething(String req){
return "["+req+"]";
}
无返回值执行异步请求,不指定线程池采用默认ForkJoinPool.commonPool()
public static void main(String[] args) {
CompletableFuture.runAsync(() -> doSomething("s"));
System.out.println("haha");
}
private static void doSomething(String req){
System.out.println(req);
}