怎么给网站加友情链接,wordpress新建网站后台无法登陆,产品ui设计公司,wordpress app插件下载Java 中的 CompletableFuture 提供了多种方法来支持任务链式调用。这些方法允许你将一组操作链接在一起#xff0c;形成一个任务链#xff0c;每一个任务只有在上一个任务成功完成后才会被执行。现在#xff0c;我们来看一下一些常用的链接任务的方法#xff1a;
thenAppl…Java 中的 CompletableFuture 提供了多种方法来支持任务链式调用。这些方法允许你将一组操作链接在一起形成一个任务链每一个任务只有在上一个任务成功完成后才会被执行。现在我们来看一下一些常用的链接任务的方法
thenApply()这个方法可以接收一个 Function 实例用来处理上一个阶段计算后的结果生成一个新的 CompletableFuture:
CompletableFutureInteger future CompletableFuture.supplyAsync(() - {return 100;
});
CompletableFutureString future2 future.thenApply(i - i * 2).thenApply(i - i.toString());thenAccept()这个方法和 thenApply() 很类似但是不同的是它的入参是一个 Consumer它没有返回值
CompletableFutureVoid future CompletableFuture.supplyAsync(() - {return 100;
});
future.thenAccept(System.out::println);thenRun()这个方法既不需要上一阶段的结果也没有返回值它接收一个 Runnable 参数
CompletableFutureVoid future CompletableFuture.supplyAsync(() - {return 100;
});
future.thenRun(() - System.out.println(Finished));thenCompose()这个方法接收一个 Function它的入参是上一阶段的结果返回值必须是一个新的 CompletableFuture用于链接两个 CompletableFuture
CompletableFutureInteger future CompletableFuture.supplyAsync(() - {return 100;
});
CompletableFutureInteger future2 future.thenCompose(i - CompletableFuture.supplyAsync(() - i * 2));以上方法都是异步的也就是说它们返回的 CompletableFuture 对象完成的时间不受代码顺序的约束。此外所有这些方法都有一个Async的版本比如 thenApplyAsync()他们可以让后续的阶段异步的执行也就是在新的线程里更进一步提高程序的并发性能。
结合这些方法我们可以创建出一连串的任务每个任务都是在上一个任务完成之后开始这就是 CompletableFuture 任务链的实现方式。