The source code is for testing only and should not be used commercially. The source code comes from the Internet. If there is any infringement, please contact me to remove it.
Java 中的Future接口

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 FutureTaskPay more attention.

read more
Resource download
PriceFree
The use is limited to testing, experiments, and research purposes. It is prohibited for all commercial operations. This team is not responsible for any illegal behavior of users during use. Please self-test all source codes! There is no guarantee of the integrity and validity of your source code. All source code is collected from the entire network
Original link:https://bcbccb.cn/en/4472.html, please indicate the source for reprinting. Disclaimer: This resource has not been authorized by the original rights holder and is not commercially available. It can only be used to learn and analyze the underlying code, CSS, etc., and is prohibited for commercial purposes. Any relevant disputes and legal liabilities arising from unauthorized commercial use shall be fully borne by the user. Everyone is responsible to support genuine copies. Please delete them within 24 hours after downloading. Thank you for your support!
1

Comments0

五星棋牌广西地方玩法 柳州麻将_来宾麻将_南宁麻将
Five-star chess Guangxi local play Liuzhou Mahjong_Guest Mahjong_Nanning Mahjong
Someone bought it 5 minutes ago Go and have a look

Site Announcements

The source code (theme/plug-in/application source code) and other resources provided by this site are only for learning and exchange

Commercial use is prohibited, otherwise all consequences will be borne by the downloading user!

Some resources are collected or copied online. If they infringe on your legitimate rights and interests, please write to us.

Currently, members have a big reward, and the current price for a lifetime member is 299 gold coins.Recent price adjustments

Join quickly, opportunities wait for no one! immediately participated in

Captcha

Fast login to social accounts

en_USEnglish