Actually, what other ways are available apart from extending the Thread class and implementing the Runnable interface?
Actually, what other ways are available apart from extending the Thread class and implementing
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There is exactly one way to create a new thread in Java and that is to instantiate
java.lang.Thread(to actually run that thread you also need to callstart()).Everything else that creates threads in Java code falls back to this one way behind the cover (e.g. a
ThreadFactoryimplementation will instantiateThreadobjects at some point, …).There are two different ways to specify which code to run in that Thread:
java.lang.Runnableand pass an instance of the class implementing it to theThreadconstructor.Threaditself and override itsrun()method.The first approach (implementing
Runnable) is usually considered the more correct approach because you don’t usually create a new “kind” of Thread, but simply want to run some code (i.e. aRunnable) in a dedicated thread.