Futuretask source code analysis (recommended)

Futuretask only implements the runnablefuture interface: @ h_ 404_ 1@

This interface inherits Java Lang. runnable and future interfaces inherit the characteristics of these two interfaces@ H_ 404_ 1@

1. It is not necessary to directly inherit the thread to generate subclasses. As long as the run method is implemented and the instance is passed into the thread constructor, the thread can execute the run method of the instance (thread (runnable))@ H_ 404_ 1@

2. The task can be executed independently. When get obtains the task execution result, it can block until the execution result is completed. You can also interrupt execution, judge execution status, etc@ H_ 404_ 1@

Futuretask is an asynchronous task executor that supports cancellation behavior. This class implements the methods of the future interface@ H_ 404_ 1@

For example: 1 Cancel task execution @ h_ 404_ 1@

2. Query whether the task has been completed @ h_ 404_ 1@

3. Get the task execution result ("get" the task must be executed to get the result, otherwise it will be blocked until the task is completed). @ h_404_1@

Note: once the task is completed, you cannot cancel the task or restart the task. (unless the task is run in runandreset mode at the beginning) @ h_ 404_ 1 @ futuretask supports the implementation class of callable or runnable. The futuretask instance can be executed by the executor@ H_ 404_ 1@

Source code (very simple):

summary

The above is all about the source code analysis (recommendation) of futuretask in this paper. I hope it can help you. Interested friends can refer to: Java uses future to obtain multithreading results in time, and talk about the wonderful use of future in Java multithreading (with source code), futuretask usage and usage scenarios. If you have any questions, you can leave a message at any time. Welcome to communicate and discuss together.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>