Set the method execution timeout in Java

Source: Internet
Author: User

Set the method execution timeout in Java
Java. util. concurrent. Future represents the result of an asynchronous calculation. It provides a method to check whether the calculation is complete or is still in the waiting state, and also provides a method to obtain the calculation result. After the computation is complete, the get method can only be used to obtain the execution result. If necessary, this method will be blocked. You can cancel the calculation by using the cancel method. Once the calculation is complete, it cannot be canceled. Main method: cancel (): cancel the task get (): Wait until the task is completed, and obtain the execution result get (long timeout, TimeUnit unit ): wait for the task to be executed within the specified time. If the task times out, an exception is thrown. The following is a specific code snippet: I think of an Application Scenario: return the QR code string to the front-end and start a thread at the same time, run a Future task to query the asynchronous notification push record. If the push record has already been pushed, the task is canceled. If the push record times out, the message is automatically pushed to the frontend, inform the payment timeout.

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.