Home > Back-end >  Why recator Mono and CompletableFuture conversion block?
Why recator Mono and CompletableFuture conversion block?

Time:02-28

 private Mono Get (String id) {
Return Mono. Just (id). FlatMap (s - & gt; {
Try {
The info (" sleep thread ");
Thread.sleep (100000);
} the catch (Exception e) {
The info (" error is {} ", e);
}
Return Mono. Just (" u ");
});
}

Public static void main (String [] args) {
T=new testMain testMain ();
Mono Observables=t.g et (" id "). The timeout (Duration. OfMillis (10));
The info (" convert ");
CompletableFuture Result=observables. ToFuture (.) handleAsync ((x, th) - & gt; {
If (th!=null) {
The log. The error (" cache have error is {} ", th);
return null;
}
Return the x;
});


}


As above the following code example, encapsulated in the get method synchronization method for mono, expected after 10 ms, CompletableFuture immediately processing timeout exception, but in fact CompletableFuture part will have to wait until the thread block after the 100 s, to enter, is this why

CodePudding user response:

You set up a thread to sleep 100 s, in the call t.g et (" id ") method have been in this thread, etc., are not returned by the Mono objects to call a timeout (Duration. OfMillis (10)) function, who is to judge the timeout

CodePudding user response:

reference 1st floor also night reply:
you set up a thread to sleep 100 s, in the call t.g et (" id ") method when the thread has been such a, did not return Mono objects to call a timeout (Duration. OfMillis (10)) function, who is to judge the timeout


T.g et (" id ") is not submitted to the asynchronous Mono, want him to continue to perform the following process, rather than wait
  • Related