This week's giveaway is in the Cloud/Virtualization forum. We're giving away four copies of Production-Ready Serverless (Operational Best Practices) and have Yan Cui on-line! See this thread for details.
Every Thread has a Runnable -- sometimes the Runnable is the Thread itself. In any case, it's the Runnable's job to provide a run() method, and it's the Thread's job to provide the magic machinery to execute in a separate thread.
Hello, You are asking the differences between the Thread class and the Runnable interface. Both are used to make threads. Implimenting the Runnable interface is appropiate than using the Thread class. Because we can extend another class while making a thread. But if we just extend the Thread class we can't inherit from another class. There is another difference. We can create threads easily using the Thread class than using the Runnable interface. Because we can just make an object from our class.But when implementing the Runnable interface we have to do a lot making thread objects giving arguments and so on. For furthur explanations see the JLS thanks
Rajith has right, a thread is class and it implements the Runnable - a thread is a runnable and it is used by threads(direct or indirect). The diference between the two is that one is a implementation(the Thread) and the other one (Runnable) is an interface. In the oop "words" the Thread IS A Runnable(extend) because it implements the Runnable interface, can be also Thread HAS A Runnable(composition) - because on a thread you can set the threads target(via its contructor).
Difference Between Threads class & Runnable Interface is::
java has 2 ways of creating the threads... - implementing an interface - Extending a class.
Extending the class is the way java inherits methods and variable from a parent class. In this case one can only extend or inherit from a single parent class.
This limitation within java can be overcome by implementing the interfaces, which is the most common way to create threads.
Interfaces provide a way for the programmers to lay the groundwork of a class. They are used to design the requirements for a set of classes to implement. The inteface sets everything up, and the class or classes that implement the interface do all the work. The different set of classes that implement the interface have to follow the same rules.
I think i've cleared ur doubt abt the Threads class & Runnable Interface.
One difference is that you get more help in validating your code when you implement an interface. This example is a bit trivial:
It compiles fine. You might not realize that you miss-spelled run and wonder why it does not work. Probably more important when writing mouse listeners, etc. If you implemented the interface you would have got a compile time error!
In any case, the main aim of this exercise is to have the run entry point correctly available to the JRE. In this example it would have jumped into Thread's empty run method.
Ed [ December 28, 2004: Message edited by: Eddie Vanda ]
The nice thing about Standards is that there are so many to choose from!
Think about it from an 00 perspective. What's the purpose of subclassing? Remember that we're talking about two different things here-the Thread and the thread'slob. From an 00 view, those
two are very separate activities, and belong in separate classes. The only time you want to subclass/extend
the Thread class,Is if you are making a new and more specific type ofThread.ln other words, if you think of the Thread as the worker, don't extend the Thread class
unless you need more specific worker behaviors. But If all you need Is a new lob to be run by a Thread/worker, then Implement Runnable In a separate,job-specific
(not worker-specific) class. This is a design Issue and not a performance or language Issue.It's perfectly legal to subclass Thread and override the runO method, but It's rarely a good Idea.