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.