Return CompletableFuture< Void>或CompletableFuture<?&gt ;? [英] Return CompletableFuture<Void> or CompletableFuture<?>?

查看:71
本文介绍了Return CompletableFuture< Void>或CompletableFuture<?&gt ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个异步方法,该方法返回 CompletableFuture .未来的唯一目的是跟踪方法的完成时间,而不是结果.返回 CompletableFuture< Void> CompletableFuture<?> 会更好吗?是否有理由偏爱另一个,或者它们可以互换?

最好使用 CompletableFuture< Void> .

根据此答案CompletableFuture. The only purpose of the future is to track when the method is complete, not its result. Would it be better to return CompletableFuture<Void> or CompletableFuture<?>? Is there a reason to prefer one or the other, or are they interchangeable?

Note that I'm only asking about return types, not parameter lists, variable declarations, or other contexts.

解决方案

It is best to use CompletableFuture<Void>.

According to this answer found by Sotirios Delimanolis, Future<?> is a minor API flaw. In Java 6 the submit() method used a Future<Object> internally, and so its return type was set to Future<?>. In Java 7 the implementation changed to use Future<Void> internally, but it was too late to change the API so the return value stayed as Future<?>.

Newer Java APIs use Future<Void> and CompletableFuture<Void>. Those are the examples we should follow.

这篇关于Return CompletableFuture&lt; Void&gt;或CompletableFuture&lt;?&gt ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆