我試圖通過簡單的示例更好地理解番石榴 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")
來獲得正確的實施。
添加回答
舉報
0/150
提交
取消