Java Task Timeout processing mechanism implementation __java

Source: Internet
Author: User
1 Problem Description

In the development of application software, there is often a need to implement a method to perform a task that does not exceed the specified value and, if the timeout occurs, the caller either interrupts its execution or makes it return, regardless of whether the method is likely to succeed in the future. This is the problem of timeout processing.

Based on whether the method of performing the task is asynchronous, the problem can be analyzed from two aspects: if the method is executed sequentially, the control of the entire program when the method executes is in the method of executing the task, the method caller is powerless to timeout the task, and can only hope that the method of executing the task can determine whether to timeout in each round of the task. So that it can be returned at any time; If the task method executes asynchronously, that is, the method that executes the task is another thread, then the task thread's timeout interrupt processing is implemented through collaboration between the main thread and the threads of the task thread. 2 Solutions

Based on the analysis of the problem, we can propose three kinds of solutions, a synchronous solution and two asynchronous solutions. 2.1 Serial Timeout processing

Serial timeout processing means that the program has only one thread, and the caller invokes the task method, which is timed out entirely by the method that performs the task.

This scenario usually requires that the tasks be cycled, that the computations within each loop are more complex, the execution time is longer or uncertain. The way to perform a task is to plan the task algorithm code while also considering timing out. The usual code framework is as follows:

1 public void Runtask (long timeout) {
2 Long Begintime=system.currenttimemillis ();
3 Task Execution Preparation
4 Perform the following for the task algorithm
5 while ((System.currenttimemillis ()-begintime<timeout) && (logical judgment of task itself) {

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.