Java multithreading – callable and future

Known methods for creating multithreading include inheriting the tree class and implementing the runnable method. In addition, Java also provides a callable interface, which also provides a call () method as the thread executor. However, the call () method is somewhat different from the run () method:

However, Java does not allow callable objects to be used directly as the target of threads. Moreover, the call () method also has a return value -- the call () method is not called directly, but is called as the thread executor. Java provides the future interface to represent the return value of the call () method in the callable interface, and provides a futuretask implementation class for the future interface. This implementation class implements the future interface and the runnable interface, so it can be used as the target of thread.

The steps to create a thread using callable and future are as follows:

Attach the implementation code:

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
分享
二维码
< <上一篇
下一篇>>