In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how java implements the Callable interface to create thread classes, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Implement the Callable interface to create a thread class
The Callable interface has been provided since Java5, which is an enhanced version of the Runnable interface. The Callable interface provides a call () method as the thread execution body, the call () method can have a return value, and the call () method can declare that an exception is thrown.
Boolean cancel (boolean may) attempts to cancel the associated Callable task in the Future.
V get () returns the return value of the call () method in the Call task. Calling this method makes it look like thread blocking, and you have to wait for the child thread to finish before you get the return value.
V get (long timeout,TimeUnit unit) returns the return value of the call () method in the Call task. This method allows the program to block at most the time specified by timeout and unit. If the specified time has elapsed, and if no value is returned after the specified time, a TimeoutException exception will be thrown.
Boolean isCancelled () returns true if it is cancelled before the Callable task completes normally.
Boolean isDone () returns true if the Callable task has been completed.
The implementation steps of Runnable:
Create an implementation class for the Callable interface and implement the call () method, which acts as the thread's execution body, and the call () method has a return value.
Use the FutrueTask class to wrap the Callable object.
Create and start a new thread using the FutrueTask object as the target of the Thread object.
Enable the get () method of the FutrueTask object to get the return value of the child thread.
Public class CallableDemo implements Callable {
Public static void main (String args []) {
FutureTask futureTask = new FutureTask (new CallableDemo ())
New Thread (futureTask) .start ()
Try {
System.out.println ("child thread return value:" + futureTask.get ())
} catch (InterruptedException e) {
E.printStackTrace ()
} catch (ExecutionException e) {
E.printStackTrace ()
}
If (futureTask.isDone ()) {
System.out.println (Thread end)
}
}
@ Override
Public Integer call () throws Exception {
System.out.println (Thread start)
Int ss = 0
For (int I = 0; I < 20; iTunes +) {
Ss + = I
}
Return ss
}
} these are all the contents of the article "how java implements the Callable interface to create thread classes". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.