详解Java CompletableFuture使用方法以及与FutureTask的区别
程序员文章站
2022-03-04 11:26:50
目录futuretask 创建异步任务创建任务1. .supplyasync2. .runasync异步回调1. .thenapply2. .thenaccept3. .exceptionally4....
总的来说简洁了futuretask与线程池的配合使用
没啥太大区别吧我觉得, 使用方法不一样, 多了一些方法 ???
futuretask 创建异步任务
futuretask<string> stringfuturetask = new futuretask<>(() -> { return "aa"; }); executorservice.execute(stringfuturetask); system.out.println(stringfuturetask.get()); completablefuture<string> future1 = completablefuture.supplyasync(() -> { return "aa"; }, executorservice); // 不用手动提交了 system.out.println(future1.get());
还有很多异步回调, 组合处理
创建任务
1. .supplyasync
创建一个带返回值的任务
2. .runasync
创建一个不带返回值的任务
executorservice executorservice = executors.newfixedthreadpool(1); // 带返回值 completablefuture future = completablefuture.supplyasync(() -> { try { system.out.println("future " + new date()); thread.sleep(2000); } catch (interruptedexception e) { e.printstacktrace(); } return "aaa"; }, executorservice); // 推荐使用
以上两个方法都有两个构造方法, 默认不指定自定义线程池, 他会指定默认的提交任务的方法
// 查看cpu的核数是否大于1核 private static final boolean usecommonpool = (forkjoinpool.getcommonpoolparallelism() > 1); // 如果大于1核 则调用execute方法, 每次创建一个线程 private static final executor asyncpool = usecommonpool ? forkjoinpool.commonpool() : new threadpertaskexecutor(); static final class threadpertaskexecutor implements executor { public void execute(runnable r) { new thread(r).start(); } }
所以推荐自定义线程池的方式
异步回调
指的是 异步任务结束后调用的任务
1. .thenapply
带返回值的异步调用函数, 有入参, 有出参
2. .thenaccept
不带返回值的异步回调函数, 有入参
completablefuture future = completablefuture.supplyasync(() -> { try { system.out.println("future " + new date()); thread.sleep(2000); } catch (interruptedexception e) { e.printstacktrace(); } return "aaa"; }, executorservice); // future执行完之后执行的异步任务 completablefuture<string> thenapply = future.thenapply((result) -> { system.out.println("future2 " +new date()); system.out.println(result); return "bbb" + result; });
3. .exceptionally
异步任务出现异常调用的回调方法
completablefuture future = completablefuture.supplyasync(() -> { try { system.out.println("future " + new date()); thread.sleep(2000); int a = 1 / 0; } catch (interruptedexception e) { e.printstacktrace(); } return "aaa"; }, executorservice); completablefuture<string> exceptionally = future.exceptionally((result) -> { system.out.println("future3 " + result); return "bbb" + result; }); // 出现异常则返回异常, 没异常则返回future的返回值 system.out.println(exceptionally.get());
去掉异常
4. .whencomplete
当主任务出现异常时, 会终止任务,get的时候会抛出主任务的异常, 入参值为null, 否则正常运行
completablefuture future = completablefuture.supplyasync(() -> { try { system.out.println("future " + new date()); thread.sleep(2000); int a = 1/0; } catch (interruptedexception e) { e.printstacktrace(); } return "aaa"; }, executorservice); completablefuture<string> exceptionally = future.whencomplete((result, error) -> { system.out.println("future3 " + result); system.out.println("future3 " + error); }); system.out.println(exceptionally.get());
去掉异常
组合处理
....
就是将多个任务组合起来执行, 时间原因, 这里我就不介绍了, 大家另行百度吧
到此这篇关于详解java completablefuture使用方法的文章就介绍到这了,更多相关java completablefuture内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
推荐阅读
-
MySQL存储引擎以及MyISAM与InnoDB的区别详解
-
详解Java中方法next()和nextLine()的区别与易错点
-
Java中BufferedReader与Scanner读入的区别详解
-
天猫魔盘与小米wifi和360随身wifi哪个好以及三者之间的区别对比详解
-
详解Java抽象类与普通类的区别
-
详解Java CompletableFuture使用方法以及与FutureTask的区别
-
JQuery事件中mouseover与mouseenter以及mouseout与mouseleave的区别详解
-
Java中的equals()方法以及与"=="的区别
-
Kotlin与Java的区别详解
-
【Java注解用法】@Autowired 与@Resource的区别以及@Qualifier的介绍