In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces what is the difference between Callable and Runnable, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Callable and Runnable
Java.lang.Runnable is an interface with only one run () method
Public interface Runnable {public abstract void run ();}
The return value of the run () method is void, so it cannot return any results after the task is executed.
Callable is an interface under the java.util.concurrent package, and there is only one call () method, which is similar to the run () method of java.lang.Runnable. Classes that implement the Callable interface and classes that implement the Runnable interface are tasks that can be performed by other threads.
Public interface Callable {V call () throws Exception;}
You can see that the call () method has a return value, and you can return the result of the execution
The difference between Callable and Runnable:
1. The call () method is defined in Callable, and the run () method is defined in Runnable
2. The call () method in Callable can return the result after executing the task, but the run () method in Runnable cannot get the return value.
3. The call () method in Callable defines throws Exception to throw an exception, which can be caught by the main thread when the main thread Future.get (). The run () method in Runnable does not define throwing an exception, and it will be thrown when an exception occurs when the task is running, because the RuntimeException will be thrown even without default, but the exception cannot be obtained by the main thread.
4. Run the Callable task to get a Future object that represents the result of asynchronous operation.
1. Why do you need Callable when you have Runnable? what is the difference between them?
Both Runnable and Callable indicate the task executed, but the difference is that the Runnable.run () method does not return a value, while Callable.call () has a return value
But in fact, the thread still executes the Runnable.run () method when executing the task, so when using ThreadPoolExecutor.submit (), it encapsulates Callable as FutureTask, and FutureTask is the implementation class of Runnable and Future, so when executing the task of Callable, the thread actually executes the run () method of FutureTask, which encapsulates the logic of calling Callable.call () and returning the result.
If there is an exception in the execution of the Runnable task, the main thread does not know it, while if there is an exception in the execution of the Callable task, it will throw a java.util.concurrent.ExecutionException in Future.get (), which encapsulates the real exception.
2. How does Future.get () get the thread return value?
First of all, thanks to the definition of the return value by the Callable.call () method, after the Callable task is submitted, the Callable is encapsulated as a FutureTask, which can be executed either as a Runnable or as a Future to obtain the return value, and the FutureTask.run () method calls the task code in Callable.call (). Before the task is completed, if the main thread uses Future.get (), it is actually calling FutureTask.get (), which determines that the state of the task is not finished, adds the main thread to the waiters wait list, and suspends the main thread. After the task finishes, FutureTask wakes up all threads waiting to get the return value, and the FutureTask.get () of the main thread returns. Therefore, the main thread and the running thread use FutureTask as a bridge to get the thread return value.
3. Can Future.cancel () really cancel the execution of the task?
The first answer is "not necessarily", according to the method annotation "Attempts to cancel execution of this task" in JDK, that is, an attempt to cancel the executed task. If the task is executing and the parameter mayInterruptIfRunning passes true when cancel () is called, the interrupt () method is called on the thread of execution. So the question becomes, can the interrupt () method interrupt thread execution? The interrupt () method does not interrupt the running thread. What this method actually does is throw an interrupt signal when the thread is blocked so that the thread can exit the blocked state. More specifically, if a thread is blocked by Object.wait (), Thread.join (), Thread.sleep (), and so on, it will receive an interrupt exception (InterruptedException), thus terminating the blocked state early.
If the thread is not blocked, calling interrupt () will not work, so even if the thread is blocking the state and throws the InterruptedException, whether the thread can actually cancel execution depends on whether the InterruptedException is captured in the code and whether there is the corresponding judgment logic for the interrupt flag.
Thank you for reading this article carefully. I hope the article "what's the difference between Callable and Runnable" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.