public CompletableFuture orTimeout(long timeout, TimeUnit unit)
Example
的中文翻译为:
示例
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
public class OrTimeoutMethodTest {
public static void main(String args[]) throws InterruptedException {
int a = 10;
int b = 15;
CompletableFuture.supplyAsync(() -> {
try {
TimeUnit.SECONDS.sleep(5);
} catch(InterruptedException e) {
e.printStackTrace();
}
return a + b;
})
.orTimeout(4, TimeUnit.SECONDS)
.whenComplete((result, exception) -> {
System.out.println(result);
if(exception != null)
exception.printStackTrace();
});
TimeUnit.SECONDS.sleep(10);
}
}
登录后复制
输出
25
登录后复制
completeOnTimeOut() 语法
public CompletableFuture completeOnTimeout(T value, long timeout, TimeUnit unit)
登录后复制
Example
的中文翻译为:
示例
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
public class CompleteOnTimeOutMethodTest {
public static void main(String args[]) throws InterruptedException {
int a = 10;
int b = 15;
CompletableFuture.supplyAsync(() -> {
try {
TimeUnit.SECONDS.sleep(5);
} catch(InterruptedException e) {
e.printStackTrace();
}
return a + b;
})
.completeOnTimeout(0, 4, TimeUnit.SECONDS)
.thenAccept(result -> System.out.println(result));
TimeUnit.SECONDS.sleep(10);
}
}
登录后复制
输出
25
登录后复制
以上就是Java 9中orTimeout()方法和completeOnTimeOut()方法之间的区别是什么?的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!