Analysis of future in Java and Scala

With the increase of CPU cores, asynchronous programming model has been more and more applied in the field of concurrency. Because Scala is a functional language and naturally supports asynchronous programming model, today we mainly look at futrue in Java and scala to take you into the door of asynchronous programming.

Future

Many students may ask, what is the relationship between futrue and asynchronous programming? From the surface meaning of future, a future object can see a future result, which is very similar to the concept of asynchronous execution. You just execute it yourself, as long as you convey the final result to me. The thread does not have to wait for the result all the time. You can perform other operations when a specific asynchronous task is executed, for example:

async work

We are now performing the task of cooking, which requires operations such as cooking, cooking and setting tableware. If we perform this task through the concept of asynchrony, for example, cooking may take a long time, but the process of cooking does not need our management. We can use this time to cook, and there may be free time in the process of cooking, We can set up tableware. When the electric rice cooker informs us that the rice is cooked and the dishes are cooked, we can finally start eating. Therefore, the above "cooking - > rice" and "cooking - > dishes" can be regarded as a future process.

Future in Java

In the early version of Java, we can't get the execution result of the thread. Whether we inherit the thread class or implement the runnable interface, we can't get the execution result of the thread. Therefore, we can only do some business logic operations in the run method executed by the thread. However, with the release of Java 5, it brings us callable and future interfaces, We can use the characteristics of these two interfaces to obtain the execution results of threads.

Callable interface

Generally speaking, the callable interface is also a thread execution class interface. What is the difference between it and the runnable interface? Let's first look at their definitions:

1. Callable interface:

2. Runnable interface:

From the above definition, we can see that the biggest difference between the two is whether the corresponding execution method has a return value. The call method in the callable interface has a return value, which is why we can get the return value or exception information executed by a thread through the callable interface.

Future interface

As mentioned above, since the callable interface can return the results of thread execution, why do you need the future interface? Because the execution result of the callable interface is only a future result value, if we need to obtain specific results, we must use the future interface. In addition, the callable interface needs to entrust the submit task of executorservice to execute. Let's see how it is defined:

summary

The above is the future in Java and scala introduced by Xiaobian. I hope it will help you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

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