In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
What is Runnable, Callable, Future, FutureTask and their applications? I believe many inexperienced people are at a loss about this. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Generally, there are only two ways to create a thread, one is to inherit Thread, and the other is to implement the Runnable interface. But a fatal disadvantage of these two creation methods is that there is no return value, which is very distressing if there is no return value. It is a bit troublesome to use shared variables or other means of communication to get the result of thread processing.
It is also generally discouraged to use inheritance Thread to create threads, because Java has only single inheritance and cannot inherit more than one. But Runnable is an interface, so you can have your implementation class implement multiple interfaces at the same time. And then you have to go to the thread pool. If you used Runnable to implement it before, you can pass Runnable directly to the thread pool for management.
After Java1.5, there are Callable and Future. These two kinds can provide the result of thread execution!
Next, we briefly introduce Runnable, Callable, Future, and FutureTask.
RunnableRunnable is an interface, simply a method, implement the run method, write the code you want to execute in the run method, but no task returns the interface, and cannot throw an exception.
Please click to enter a picture description
Simple application
Please click to enter a picture description
CallableCallable is also an interface, which is simply a call method. Just write the code you want to execute in the call method, and return the result of the execution. The difference with Runnable is that it has a return result and can throw an exception! Generally used with ThreadPoolExecutor.
Simple application, it can not be used directly with Thread.
FutureFuture is also an interface, which can cancel a specific Runnable or Callable task, determine whether the task has been cancelled, query whether the task is completed, and get the result of the task. If it is Runnable, the return result is null (the following will analyze why the task of Runnable, Future can also return the result). There are several methods in the interface. Note that both get methods block the current thread calling get until the result is returned or the current thread wakes up until it times out.
Simple application
FutureTask because Future is just an interface, it cannot be used directly to create objects, so you have the following FutureTask.
FutureTask is no longer an interface, it's a class. It implements the RunnableFuture interface
Public class FutureTask implements RunnableFuture
The RunnableFuture interface inherits Runnable and Future.
Public interface RunnableFuture extends Runnable, Future
So it can be executed by threads as Runnable, and it can also have the operations of Future. Its two constructors are as follows
Simple application
Thread pool application generally we use thread pool when we use multi-thread, and we usually use ThreadPoolExecutor to create thread pool. My last article has talked about ThreadPoolExecutor, and here I would like to add the usage of submit.
That is, if we need to return the execution result of the task, we have to call the submit method instead of execute.
It's not mysterious that submit encapsulates our mission and then execute.
The newTaskFor method is new with a FutureTask return.
So all three methods actually convert task to FutureTask, and if task is Callable, assign the value directly. If it is Runnable, it will be converted to Callable and then assigned.
Task is the case of Callable.
Task is the case of Runnable.
Read the source code to unveil the mystery, because Future needs to return results, so the internal task must be Callable. If task is Runnable, I will make a fake, steal a day, pack a Callable vest outside the Runnable, and write the returned results at the time of construction.
If you are calling Future submit (Runnable task); submit the task, construct it directly as RunnableFuture ftask = newTaskFor (task, null); plug a null directly. So when you call Future.get, you get null.
So what's the use of this? Just use energy to determine that the task has been executed, which is similar to Thread.join.
There is also a Future submit (Runnable task, T result); you may wonder what's the use of this. When you pass in a result,Future.get, you return the same result. Yes, but it's OK to use it this way!
In this way, you can get the revised results!
After reading the above, have you mastered the methods of Runnable, Callable, Future, FutureTask and their applications? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.