
1. preface
Java 1.5 provides java.util.concurrent.Future
Interface, which is very useful when handling asynchronous calls and concurrent processing. Today let's study this interface. in JDK on Future
It is described as follows:
A Future represents the result of an asynchronous computation. Methods are provided to check if the computation is complete, to wait for its completion, and to retrieve the result of the computation. The result can only be retrieved using method get when the computation has completed, blocking if necessary until it is ready. Cancellation is performed by the cancel method. Additional methods are provided to determine if the task completed normally or was cancelled. Once a computation has completed, the computation cannot be cancelled. If you would like to use a Future for the sake of cancellability but not provide a usable result, you can declare types of the form Future<?& gt; and return null as a result of the underlying task.
The general meaning is: Future
Is a container interface for asynchronous calculation results. It provides a state to check whether the calculation is completed while waiting for the asynchronous calculation to complete, and obtains the calculation results after the asynchronous calculation is completed and can only be passed by get
Method to get the result, and block if the asynchronous calculation has not completed. Of course, you can pass the method before the asynchronous calculation is completed cancel
Method cancels, and marks a canceled status if the asynchronous calculation is canceled. If you want asynchronous calculations to be canceled without providing usable calculation results, you can declare that if you use Future for cancellability but without providing usable results Future<?& gt;
Form type, and returns null
As a result of low-level tasks.
2. usage scenarios
Java multi-threading mechanism, without Future
Previously, if you wanted to save the calculation results of multiple threads, you had to wait for the method to finish calculating. During this period, you could only wait. No matter how time-consuming your calculation logic was, this was obviously unreasonable. We want to deal with time-consuming tasks while doing other things rather than waiting, and that's what Future
why it was designed.
We can know from the above instructions Future
Used to process time-consuming asynchronous calculation tasks, it will be blocked and suspended until the result is calculated unless the calculation is canceled. The general usage scenarios for utilizing these features are as follows:
- High-consumption scientific computing scenarios, such as tensor computing in deep learning.
- Massive and complex structured data processing (big data processing).
- Asynchronous large file downloading, asynchronous crawler crawling data.
- Some other time-consuming, data-intensive asynchronous Web Services.
3. Future
Let's learn more in the futureFuture
Methods provided.
3.1 cancel
boolean cancel(boolean mayInterruptIfRunning) Calling this method attempts to cancel execution of the task. This attempt will fail if the task has been completed, cancelled, or cannot be cancelled. When the method is called, the task has not yet started, the method call succeeds and the task will no longer execute. If the task has been started, then mayInterruptIfRunning
Parameter determines whether the thread executing this task should be interrupted with an attempt to stop the task. Called after this method returnsisDone
method returns true
。subsequent calls isCancelled
Always return the return value of the first call.
3.2 isCancelled
boolean isCancelled() If the task is cancelled before completion, it will be returned true
。
Please note that task cancellation is a proactive behavior.
3.3 isDone
boolean isDone() The task has ended. Return if the task is completed, cancelled, or abnormal. true
。
3.4 get
V get() throws InterruptedException, ExecutionException Calling this method waits before getting the calculation results. The result will be returned as soon as the calculation is completed. It also has an overloaded method V get(long timeout, TimeUnit unit) If the task calculation result is not returned within unit time, the time out will be exceeded and the task will end immediately.
4. summary
this article describes how to Java Solve the problem of obtaining results for asynchronous tasks,Future
It is just a prescribed paradigm, through which we can make full use of computing resources to process tasks in parallel and obtain the calculation results of the tasks at the appropriate time. Due to space reasons, we will introduce in the next article Future
A common implementation of FutureTask
Pay more attention.
Comments0