为了账号安全,请及时绑定邮箱和手机立即绑定

Java:带有 ListenableFuture 的番石榴期货 API

Java:带有 ListenableFuture 的番石榴期货 API

30秒到达战场 2023-06-14 14:25:22
我试图通过简单的示例更好地理解番石榴 API:首先我实例化返回“hello”的 ListenableFuture 然后我使用 Futures.transform() 将我的“hello”转换为“HELLO”但我没有得到任何结果。这是我的代码(我删除了 ListenableFuture 实现中的其他方法以使事情变得更容易):public static void main(String[] args) throws InterruptedException, ExecutionException {        ListenableFuture<String> future = getString();        ListenableFuture<String> future2 = Futures.transform(future, new Function<String,String>() {            @Override            public String apply(String input) {                return input.toUpperCase();            }        });        System.out.println(future.get());  //print "hello"        System.out.println(future2.get());  //blocking, never ends...no result    }    private static ListenableFuture<String> getString() {        return new ListenableFuture<String>() {            @Override            public String get() throws InterruptedException, ExecutionException {                return "hello";            }        };    }
查看完整描述

1 回答

?
HUX布斯

TA贡献1876条经验 获得超6个赞

这可能表示您为简化操作而删除的那些“ListenableFuture 实现中的其他方法”中的错误。

与其实施您自己的ListenableFuture,不如使用Futures.immediateFuture("hello")来获得正确的实施。


查看完整回答
反对 回复 2023-06-14
  • 1 回答
  • 0 关注
  • 81 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信