我的当前代码使用一系列异步进程来产生结果。我需要将每个进程包装在一个同步方法中,以便每个进程可以通过返回值访问其结果。我想使用执行器服务来实现这一点,以便允许同时发生多个进程。我觉得Future可能与我的实现相关,但我无法找到一个好的方法来实现它。
我现在拥有的:
public class DoAJob {
ResultObject result;
public void stepOne() {
// Passes self in for a callback
otherComponent.doStepOne(this);
}
// Called back by otherComponent once it has completed doStepOne
public void stepTwo(IntermediateData d) {
otherComponent.doStepTwo(this, d);
}
// Called back by otherComponent once it has completed doStepTwo
public void stepThree(ResultObject resultFromOtherComponent) {
result = resultFromOtherComponent;
//Done with process
}
}
这在内部运作得相当不错,但现在我需要将我的流程映射到一个带有返回值的同步方法中,例如:
public ResultObject getResult(){
// ??? What goes here ???
}
有没有关于如何优雅地实现这个的好主意?