What is the use Runnable vs Thread in Java?



Suraj Said

+0 -0

Runnable is an interface that defines a single method, run(), which contains the code that will be executed by the thread.
To use the Runnable interface, you would create a class that implements the Runnable interface and overrides the run() method.
Then, you would create an instance of that class, and pass it to the constructor of a Thread object, which would then be started using the start() method.
Thread is a class that extends Object and Runnable interface. It also has a run() method, but it also provides additional methods and fields for working with threads.
The main difference between using Runnable and Thread is that a class can only extend one class, but it can implement multiple interfaces. So, if you have a class that needs to extend another class, you can use the Runnable interface to create a thread.



Submit Your Response