Heim  >  Artikel  >  Java  >  Was ist der Unterschied zwischen der orTimeout()-Methode und der completeOnTimeOut()-Methode in Java 9?

Was ist der Unterschied zwischen der orTimeout()-Methode und der completeOnTimeOut()-Methode in Java 9?

WBOY
WBOYnach vorne
2023-08-27 22:53:041023Durchsuche

Die Methoden

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

orTimeout() und completeOnTimeOut() sind beide in der Klasse CompletableFuture definiert. Diese beiden Methoden wurden in Java 9 eingeführt. Die Methode orTimeout() kann verwendet werden, um anzugeben, dass das Programm die Ausführung stoppt und eine TimeoutException strong> auslöst, wenn eine bestimmte Aufgabe nicht innerhalb einer bestimmten Zeit abgeschlossen wird, während die Methode completeOnTimeOut() mit dem bereitgestellten Wert abgeschlossen wird CompletableFuture. Wenn nicht, wird der Vorgang vor dem angegebenen Timeout abgeschlossen. Die chinesische Übersetzung der

orTimeout()-Syntax

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

Example

lautet:

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);
   }
}

Output

<strong>25
</strong>

completeOnTimeOut() Die chinesische Übersetzung von

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

Example

ist:

Beispiel

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);
   }
}

Ausgabe

<strong>25</strong>

Das obige ist der detaillierte Inhalt vonWas ist der Unterschied zwischen der orTimeout()-Methode und der completeOnTimeOut()-Methode in Java 9?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Dieser Artikel ist reproduziert unter:tutorialspoint.com. Bei Verstößen wenden Sie sich bitte an admin@php.cn löschen