How to control the timeout time in Java asynchronous method
In Java development, we often encounter situations where we need to call asynchronous methods. However, since the execution time of asynchronous methods cannot be determined, we often need to time out the method within a certain time range to avoid long-term blockage.
This article will introduce how to control the timeout time of an asynchronous method in Java.
1. Use CompleteFuture
Java 8 introduces CompleteFuture, a powerful asynchronous processing tool that provides rich methods for handling asynchronous tasks.
We can use the CompleteFuture method to set the timeout time.
First, we can use the CompletableFuturecompleteOnTimeout
Method sets the return value of the timeout.
The sample code is as follows:
CompletableFuture<String> future = (() -> { // Asynchronous operation return "result"; }); ("Timeout Return Value", 3, );
In the example above, the return value of the asynchronous operation is "Result", and if it is not completed after more than 3 seconds, the "Timeout Return Value" will be returned.
In addition, we can also use the CompletableFutureorTimeout
The method sets the timeout time and throws a TimeoutException exception.
The sample code is as follows:
CompletableFuture<String> future = (() -> { // Asynchronous operation return "result"; }); (3, ) .exceptionally(e -> { if (e instanceof TimeoutException) { ("Method Timeout"); } return null; });
In the example above, if the asynchronous operation has not completed for more than 3 seconds, a TimeoutException exception is thrown.
2. Use ExecutorService
In addition to using CompletableFuture, we can also use ExecutorService to control the timeout of the asynchronous method.
First, we need to create an ExecutorService object and set the timeout. Then, we can submit the asynchronous task through the submit method and use the get method to get the return result, and pass in the timeout time.
The sample code is as follows:
ExecutorService executor = (); Future<String> future = (() -> { // Asynchronous operation return "result"; }); try { String result = (3, ); // Processing returns the result} catch (InterruptedException | ExecutionException | TimeoutException e) { (); // Timeout processing} finally { (); }
In the above example, if the asynchronous operation is not completed for more than 3 seconds, a TimeoutException is thrown and we can timeout in the catch block.
Although CompletableFuture is more flexible and more powerful, ExecutorService is still a more common way to control the timeout of asynchronous methods.
Summarize
To control the timeout of an asynchronous method in Java, you can use CompletableFuture or ExecutorService.
Using CompletableFuture allows you to set the timeout time and get the timeout return value, while ExecutorService is more common and simple to use. According to actual needs, choose the appropriate way to control the timeout time of the asynchronous method to improve the stability and performance of the system.
The above is personal experience. I hope you can give you a reference and I hope you can support me more.
References:
- CompletableFuture Documentation
- ExecutorService Documentation