Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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 8 根据某些条件停止/完成循环的CompletableFuture CompletableFuture=CompletableFuture.completedFuture(null); 对于(int _retry=1;_retry){ if(Strings.isNullOrEmpty(lastretalstatus)| |(!“SUCCESS”.equals(lastretalstatus))){ 返回剂量计算(财务计量); }否则{ 返回CompletableFuture.completedFuture(空); } }) .handle((响应,可丢弃)->{ if(可丢弃!=null){ throwable=CompletableFutures.UnwappletionStateException(throwable); 返回throwable.getMessage(); } 返回“成功”; }); } 回归未来;_Java 8_Completable Future - Fatal编程技术网

Java 8 根据某些条件停止/完成循环的CompletableFuture CompletableFuture=CompletableFuture.completedFuture(null); 对于(int _retry=1;_retry){ if(Strings.isNullOrEmpty(lastretalstatus)| |(!“SUCCESS”.equals(lastretalstatus))){ 返回剂量计算(财务计量); }否则{ 返回CompletableFuture.completedFuture(空); } }) .handle((响应,可丢弃)->{ if(可丢弃!=null){ throwable=CompletableFutures.UnwappletionStateException(throwable); 返回throwable.getMessage(); } 返回“成功”; }); } 回归未来;

Java 8 根据某些条件停止/完成循环的CompletableFuture CompletableFuture=CompletableFuture.completedFuture(null); 对于(int _retry=1;_retry){ if(Strings.isNullOrEmpty(lastretalstatus)| |(!“SUCCESS”.equals(lastretalstatus))){ 返回剂量计算(财务计量); }否则{ 返回CompletableFuture.completedFuture(空); } }) .handle((响应,可丢弃)->{ if(可丢弃!=null){ throwable=CompletableFutures.UnwappletionStateException(throwable); 返回throwable.getMessage(); } 返回“成功”; }); } 回归未来;,java-8,completable-future,Java 8,Completable Future,在上面的代码中,我最近开始研究completablefutures——正如您所看到的,我正在用for循环链接我的futures。如果handle返回“SUCCESS”,我希望链停止。大概是这样的: CompletableFuture<String> future = CompletableFuture.completedFuture(null); for(int _retrial = 1 ; _retrial <= getRetrialCountBasedOnSomeLogic

在上面的代码中,我最近开始研究completablefutures——正如您所看到的,我正在用for循环链接我的futures。如果handle返回“SUCCESS”,我希望链停止。大概是这样的:

CompletableFuture<String> future = CompletableFuture.completedFuture(null);
for(int _retrial = 1 ; _retrial <= getRetrialCountBasedOnSomeLogic() ; _retrial++) {
  int finalRetrial = _retrial;
  future = future
      .thenCompose(lastRetrialStatus -> {
        if(Strings.isNullOrEmpty(lastRetrialStatus) || (!"SUCCESS".equals(lastRetrialStatus))) {
          return doSomeCalculations(finalRetrial);
        } else {
          return CompletableFuture.completedFuture(null);
        }
      })
      .handle((response, throwable) -> {
        if(throwable != null) {
          throwable = CompletableFutures.unwrapCompletionStateException(throwable);
          return throwable.getMessage();
        }
        return "SUCCESS";
      });
}
return future;
CompletableFuture=CompletableFuture.completedFuture(null);
对于(int _retry=1;_retrydosomecalculations(finalRetrial))
.handle((响应,可丢弃)->{
if(可丢弃!=null){
throwable=CompletableFutures.UnwappletionStateException(throwable);
返回throwable.getMessage();
}
**这是一个成功,我不需要再寻找这个循环的任何通道。让我们在这里结束,返回“成功”**
});
}
回归未来;

只需将链接重试操作作为处理程序的一部分,仅在成功状态已知时执行。由于通过lambda表达式定义的函数不能引用自身,因此需要一个可以调用的方法,例如

CompletableFuture<String> future = CompletableFuture.completedFuture(null);
for(int _retrial = 1 ; _retrial <= getRetrialCountBasedOnSomeLogic() ; _retrial++) {
  int finalRetrial = _retrial;
  future = future
      .thenCompose(lastRetrialStatus -> doSomeCalculations(finalRetrial))
      .handle((response, throwable) -> {
        if(throwable != null) {
          throwable = CompletableFutures.unwrapCompletionStateException(throwable);
          return throwable.getMessage();
        }
        **Its a success, I dont need to look for any further passes of this loop. Lets end here by returning "SUCCESS"**
      });
}
return future;
如果
maxTries
不是常数,但需要重新计算
getRetrialCountBasedOnSomeLogic()
,则此变量也可以使用,就像循环所做的那样:

private static CompletableFuture<String> yourMethod(int currentTry, int maxTries) {
    CompletableFuture<String> future
        = doSomeCalculations(currentTry).thenApply(x -> "SUCCESS");
    int nextTry = currentTry + 1;
    return nextTry <= maxTries?
        future.thenApply(CompletableFuture::completedFuture)
            .exceptionally(t -> yourMethod(nextTry, maxTries))
            .thenCompose(Function.identity()):
        future.exceptionally(t ->
            CompletableFutures.unwrapCompletionStateException(t).getMessage());
}
静态CompletableMethod(){
返回方法(1);
}
静态CompletableFuture方法(int currentTry){
int maxTries=getRetrealCountBasedOnSomeLogic();
完全的未来
=doSomeCalculations(currentTry)。然后应用(x->“SUCCESS”);
int nextTry=currentTry+1;
返回nextTry方法(nextTry))
.thenCompose(Function.identity()):
未来。例外(t->
CompletableFutures.UnwapCompletionStateException(t.getMessage());
}

太棒了!!!为什么我没有想到这么简单的实现,实际上我没有想到递归。谢谢你,兄弟。
private static CompletableFuture<String> yourMethod(int currentTry, int maxTries) {
    CompletableFuture<String> future
        = doSomeCalculations(currentTry).thenApply(x -> "SUCCESS");
    int nextTry = currentTry + 1;
    return nextTry <= maxTries?
        future.thenApply(CompletableFuture::completedFuture)
            .exceptionally(t -> yourMethod(nextTry, maxTries))
            .thenCompose(Function.identity()):
        future.exceptionally(t ->
            CompletableFutures.unwrapCompletionStateException(t).getMessage());
}
static CompletableFuture<String> yourMethod() {
    return yourMethod(1);
}

static CompletableFuture<String> yourMethod(int currentTry) {
    int maxTries = getRetrialCountBasedOnSomeLogic();
    CompletableFuture<String> future
        = doSomeCalculations(currentTry).thenApply(x -> "SUCCESS");
    int nextTry = currentTry + 1;
    return nextTry <= maxTries?
        future.thenApply(CompletableFuture::completedFuture)
            .exceptionally(t -> yourMethod(nextTry))
            .thenCompose(Function.identity()):
        future.exceptionally(t ->
            CompletableFutures.unwrapCompletionStateException(t).getMessage());
}