Java 9 CompletableFuture API Improvements - Delay and Timeout Support
java 9
1
CompletableFuture
2
Male avatar

loveprogramming viết ngày 03/04/2021

Java 9 CompletableFuture API Improvements - Delay and Timeout Support

https://grokonez.com/java/java-9/java-9-completablefuture-api-improvements-delay-timeout-support

To improve Java Future, Java 8 provides CompletableFuture which can execute some code whenever its ready. In this article, we're gonna take a look at new Java 9 CompletableFuture API that supports delay and timeout.

I. Delay


static Executor delayedExecutor(long delay, TimeUnit unit);
static Executor delayedExecutor(long delay, TimeUnit unit, Executor executor);
  • The first method, not specify Executor, returns a new Executor that submits a task to the default executor (general purpose pool ForkJoinPool.commonPool()) after delay time.
  • The second method, with an Executor as parameter, returns a new Executor that submits a task to that input executor after delay time.

Now see an example to illustrate how the method works. We have a CompletableFuture that will delay its completion by 3 seconds. The code could be:


future.completeAsync(supplier, CompletableFuture.delayedExecutor(3, TimeUnit.SECONDS))
        .thenAccept(result -> System.out.println("accept: " + result));

// other statements

The program runs 'other statements' first. 3 seconds later, it call get() method of supplier and pass the result to thenAccept().

II. Timeout

1. orTimeout()


CompletableFuture orTimeout(long timeout, TimeUnit unit);

The method exceptionally completes current CompletableFuture by throwing a TimeoutException if not otherwise completed before the timeout.

For example, we have a doWork() method that takes 5 seconds to return a CompletableFuture. But we set TIMEOUT only 3 seconds.


// TIMEOUT = 3;
// doWork() takes 5 seconds to finish

CompletableFuture future = 
        doWork("JavaSampleApproach")
        .orTimeout(TIMEOUT, TimeUnit.SECONDS)
        .whenComplete((result, error) -> {
            if (error == null) {
                System.out.println("The result is: " + result);
            } else {
                System.out.println("Sorry, timeout in " + TIMEOUT + " seconds");
            }
        });

More at: https://grokonez.com/java/java-9/java-9-completablefuture-api-improvements-delay-timeout-support

Bình luận


White
{{ comment.user.name }}
Bỏ hay Hay
{{comment.like_count}}
Male avatar
{{ comment_error }}
Hủy
   

Hiển thị thử

Chỉnh sửa

Male avatar

loveprogramming

451 bài viết.
79 người follow
Kipalog
{{userFollowed ? 'Following' : 'Follow'}}
Cùng một tác giả
Male avatar
1 0
Tutorial Link: (Link) (Ảnh) Django is a Pythonbased free and opensource web framework that follows the modeltemplateview architectural pattern. A...
loveprogramming viết 6 tháng trước
1 0
Male avatar
1 0
https://loizenai.com/angular11nodejspostgresqlcrudexample/ Angular 11 Node.js PostgreSQL Crud Example (Ảnh) Tutorial: “Angular 11 Node.js Postg...
loveprogramming viết 5 tháng trước
1 0
Male avatar
1 0
Angular Spring Boot jwt Authentication Example Github https://loizenai.com/angularspringbootjwt/ (Ảnh) Tutorial: ” Angular Spring Boot jwt Authe...
loveprogramming viết 5 tháng trước
1 0
{{like_count}}

kipalog

{{ comment_count }}

bình luận

{{liked ? "Đã kipalog" : "Kipalog"}}


Male avatar
{{userFollowed ? 'Following' : 'Follow'}}
451 bài viết.
79 người follow

 Đầu mục bài viết

Vẫn còn nữa! x

Kipalog vẫn còn rất nhiều bài viết hay và chủ đề thú vị chờ bạn khám phá!