Callable and future of Java concurrency thread pool_ Power node Java college sorting

Introduction to callable and future

Callable and future are an interesting pair. We need them when we need to get the execution results of threads. Callable is used to generate results and future is used to obtain results.

1. Callable

Callable is an interface that contains only one call () method. Callable is a task that returns results and may throw exceptions. For ease of understanding, we can compare callable to a runnable interface, and callable's call () method is similar to runnable's run () method. The source code of callable is as follows:

Note: from this, we can see that callable supports generics.

2. Future

Future is an interface. It is used to represent the results of asynchronous calculations. It provides a method to check whether the calculation is completed, so as to wait for the completion of the calculation and obtain the results of the calculation. The source code of future is as follows:

Description: future is used to represent the result of asynchronous calculation. Its implementation class is futuretask. Before explaining futuretask, let's take a look at the relationship diagram between callable, future and futuretask, as follows:

explain:

(01) runnablefuture is an interface, which inherits the two interfaces of runnable and future. The source code of runnablefuture is as follows:

(02) futuretask implements the runnablefuture interface. Therefore, we also say that it implements the future interface.

Examples and source code analysis (based on jdk1.7.0_40)

Let's take a look at the basic usage of callable and future through an example, and then analyze the implementation principle of the example.

Operation results:

four thousand nine hundred and fifty

Result description:

In the main thread main, create a new thread pool through newsinglethreadexecutor(). The callable object C1 is then created, and then through pool Submit (C1) submits C1 to the thread pool for processing, and saves the returned result to the future object F1. Then we passed F1 Get() gets the results saved in callable; Finally, through pool Shutdown() closes the thread pool.

1. submit()

Submit() in Java / util / concurrent / abstractexecutorservice Java. Its source code is as follows:

Note: submit() creates the runnablefuture object ftask through newtaskfor (task). Its source code is as follows:

2. Futuretask constructor

The constructor of futuretask is as follows:

3. Run() method of futuretask

Let's go back to the source code of submit (). After newtaskfor() creates a new ftask object, the task will be executed through execute (ftask). At this time, ftask is executed as a runnable object, and finally its run () method will be called; The run () method of ftask is in Java / util / concurrent / futuretask Java. The source code is as follows:

Note: the call () method of the callable object will be executed in run (), and the result will be finally saved to the result, and the result will be saved through set (result). After calling the get () method of FutureTask, the value returned by set (result) is returned.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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