Preface
I have written several articles on the implementation of asynchronous calls using @Async, and I have also received feedback from many children's shoes. Among them, the most problems are about the use of futures and the timeout control of asynchronous execution. So I will talk about the handling of these two problems together.
If you don't know the use of @Async annotations, you can check out the previous article, as follows:
Define asynchronous tasks
First, we first use the @Async annotation to define an asynchronous task. This method returns the Future type, as follows:
@Slf4j@Componentpublic class Task { public static Random random = new Random(); @Async("taskExecutor") public Future<String> run() throws Exception { long sleep = random.nextInt(10000); log.info("Start the task, time to take: " + sleep + "msils"); Thread.sleep(sleep); log.info("Complete the task"); return new AsyncResult<>("test"); }}Tips: What is the Future type?
Future is an interface that cancels the execution results of specific Runnable or Callable tasks, checks whether the query is completed, and obtains the results. If necessary, you can get the execution result through the get method, which blocks until the task returns the result.
Its interface definition is as follows:
public interface Future<V> { boolean cancel(boolean mayInterruptIfRunning); boolean isCancelled(); boolean isDone(); V get() throws InterruptedException, ExecutionException; V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException;}It declares five methods like this:
In other words, Future provides three functions:
Test execution and definition timeout
After completing the asynchronous task definition that returns Future, we try to implement a unit test to use this Future to complete the execution of the task, such as:
@Slf4j@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTestpublic class ApplicationTests { @Autowired private Task task; @Test public void test() throws Exception { Future<String> futureResult = task.run(); String result = futureResult.get(5, TimeUnit.SECONDS); log.info(result); }}In the above code, we also define the timeout time for the thread to execute in the get method. By executing this test, we can observe that when the execution time exceeds 5 seconds, a timeout exception will be thrown here. The execution thread can be released back to the thread pool due to the execution timeout, so as not to block and occupy resources.
Complete example:
Readers can choose the following two repositories to view Chapter4-1-4 projects according to their preferences:
Github: https://github.com/dyc87112/SpringBoot-Learning/
Gitee: https://gitee.com/diidispace/SpringBoot-Learning/
Local download: http://xiazai.VeVB.COM/201805/yuanma/SpringBoot-Learning(VeVB.COM).rar
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.