In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "the use of Future in Java". In the daily operation, I believe that many people have doubts about the use of Future in Java. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "the use of Future in Java". Next, please follow the editor to study!
When developing multithreading, we sometimes need to return the processing results of the child threads, but unfortunately the Runable interface has no return value of type void. One might think of implementing this requirement in the form of instance variables. Of course, the way of instance variables is also possible, as has been described in other articles, you should pay special attention to the use of instance variables, because instance variables may have thread safety problems. In fact, we have already provided such an interface Callable interface in Java. Let's look at the definition of the Callable interface.
Only one method call () method is defined in the interface, not the run () method, which should be noted that they all implement thread execution asynchronously. Let's demonstrate it.
We call the submit () method of the ExecutorService interface to add our task, and the execution result is no different from the Runable interface. The only difference is that the Runable interface overrides the run () method and the Callable interface overrides the call () method. But a more powerful feature of the Callable interface is that it supports return types. The result of asynchronous execution can be returned through the Callable interface.
The submit () method of the ExecutorService interface returns a Future type. Using Future, you can get the task data after asynchronous execution in the Callable interface. Let's see that the output time of the two mainthreads is a little different. This is because when the get () method in the Future interface is called, the current thread is blocked waiting for the end of the subtask, because we have a delay of 10 seconds in the subtask, so there is a deviation between the two output times. There are several other methods in the Future interface.
Although an exception was thrown, the task was finished. If we change the parameter to true, the thread interrupts immediately regardless of whether the current thread finishes execution or not.
At this point, the study of "how to use Future in Java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.