Home > Java > javaTutorial > What is the difference between orTimeout() method and completeOnTimeOut() method in Java 9?

What is the difference between orTimeout() method and completeOnTimeOut() method in Java 9?

WBOY
Release: 2023-08-27 22:53:04
forward
1129 people have browsed it

Java 9中orTimeout()方法和completeOnTimeOut()方法之间的区别是什么?

##orTimeout() and completeOnTimeOut() methods are both defined in the CompletableFuture class. This method was introduced in Java 9. The orTimeout() method can be used to specify that if a given task is not completed within a specific time, the program stops execution and throws TimeoutException strong>while completeOnTimeOut() Method completes CompletableFuture using the provided value. If not, it will complete before the given timeout. The Chinese translation of

orTimeout()'s syntax

<strong>public CompletableFuture<T> orTimeout(long timeout, TimeUnit unit)</strong>
Copy after login

Example

is:

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;
      <strong>CompletableFuture</strong>.supplyAsync(() -> {
         try {
            TimeUnit.SECONDS.sleep(5);
         } catch(InterruptedException e) {
            e.printStackTrace();
         }
         return a + b;
      })
      .<strong>orTimeout</strong>(4, TimeUnit.SECONDS)
      .<strong>whenComplete</strong>((result, exception) -> {
         System.out.println(result);
         if(exception != null)
            exception.printStackTrace();
      });
      TimeUnit.SECONDS.sleep(10);
   }
}
Copy after login

Output

<strong>25
</strong>
Copy after login

completeOnTimeOut() The Chinese translation of syntax

<strong>public CompletableFuture<T> completeOnTimeout(T value, long timeout, TimeUnit unit)</strong>
Copy after login

Example

is:

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;
      <strong>CompletableFuture</strong>.supplyAsync(() -> {
         try {
            TimeUnit.SECONDS.sleep(5);
         } catch(InterruptedException e) {
            e.printStackTrace();
         }
         return a + b;
      })
      .<strong>completeOnTimeout</strong>(0, 4, TimeUnit.SECONDS)
      .<strong>thenAccept</strong>(result -> System.out.println(result));
      TimeUnit.SECONDS.sleep(10);
   }
}
Copy after login

Output

<strong>25</strong>
Copy after login

The above is the detailed content of What is the difference between orTimeout() method and completeOnTimeOut() method in Java 9?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template