一旦调用 setException 方法,Settable Future 对象会发生什么?

What happens to a Settable Future object once the setException method is called on it?

我有一个 Settable Future 对象 "temp",它设置了上下文。 temp.addListener(new Runnable{...}) 方法也被调用,基本上是注册一个监听器。但是最终,如果出现任何异常,则调用 temp.setException() 。如果调用setException,监听器是否会被注销或上下文是否会被清除(基本上设置异常后Settable Future对象是否会被破坏)?

代码流程是这样的:-

  1. temp.setContext({temp.set(//some value is set if everything goes right) || temp.setException(//set exception if something comes up)});

  2. temp.addListener(new Runnable{ run(){temp.get()}})

当您调用 setException() 时,ListenableFuture will run its listener. If you want to write code that will not be run in that case, you can use Futures.addCallback 而不是 addListener。 (addCallback 允许您将代码指定为 运行 仅在成功的情况下或仅在失败的情况下。)

至于上下文发生了什么:上下文不是 ListenableFuture API 的一部分。不知道是你加的还是别的库加的。您必须查阅 class 才能了解它的行为方式。