Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java—有一个长时间运行的方法返回它';执行状态_Java - Fatal编程技术网

Java—有一个长时间运行的方法返回它';执行状态

Java—有一个长时间运行的方法返回它';执行状态,java,Java,我有一个长期运行的方法,我希望能够在每一步结束时更新其他内容 class A { private B b; callLongRunning() { b.longRunningMethod() } class B { longRunningMethod(){ doSomethingThatTakesAWhile(); doSomethingThatTakesAWhile(); doSomethingThatTakesAWhile(); doSomethin

我有一个长期运行的方法,我希望能够在每一步结束时更新其他内容

class A {

private B b;

callLongRunning() {
    b.longRunningMethod()
}


class B {

longRunningMethod(){
   doSomethingThatTakesAWhile();
   doSomethingThatTakesAWhile();
   doSomethingThatTakesAWhile();
   doSomethingThatTakesAWhile();
}
每次调用B.doSomethingthatstakesawhile()后,我希望“返回”一条消息,以便类a可以用一些信息更新注册表

我正在寻找Rx.Observables,但我不知道如何在这个场景中使用它

我想这样做:

A类{
私人B,;
callLongRunning(){
onPush(state=>updateDb(state));
onFinish(state=>doSomethingElse(state));
b、 longRunningMethod(someSpecialStateObject)
//一切结束后再继续
}
B类{
longRunningMethod(someSpecialStateObject){
dosomethingthatakesawhile();
someSpecialStateObject.push(“完成步骤1”);
dosomethingthatakesawhile();
someSpecialStateObject.push(“完成步骤2”);
dosomethingthatakesawhile();
someSpecialStateObject.push(“完成步骤3”);
dosomethingthatakesawhile();
someSpecialStateObject.finish(“完成了一切”);
}

以下是我的结论:

公共可观察执行(){
返回可见。创建(发射器->{
emitter.onNext(“步骤1”);
doStep1();
emitter.onNext(“步骤2”);
doStep2();
onNext(“所有步骤已完成”);
emitter.onCompleted();
},发射器。背压模式。最新版本);
}
final String[]lastMessage={”“};
myClass.execute().toBlocking().forEach(s->{
日志;
lastMessage[0]=s;
});
DoSomethingWithLastMessage(lastMessage[0]);

我只希望emitter.onCompleted()能够返回一个值。

简单的解决方案是创建一个Progress类,并在longRunningMethod()中的每个步骤后更新它。当然,如果需求变得更精细,它可能会变得复杂。是的,但是有没有一个著名的库提供这种支持?只需使用java中的普通Observable类和observer接口。基本示例:我正在检查,但似乎不支持“finishing”(我可以用一个特定的值通知并中断执行,但仍然)并且没有简单的方法来处理异常。很容易导致执行受阻。但是谢谢你的建议,我正在进一步研究它。