why there are two way of using thread in java? [duplicate]
extends Thread
:your thread creates unique object and associate with it
implements Runnable
:it shares the same object to multiple threads
Another thing to note, since you can extend only one class in Java, if you extends Thread
, you can't extend another class. If you choose to implement Runnable
, you can extend class then.
Technically, there is only one way: implement Runnable
. Thread
, incidentally, does just that, so extending it you trivially satisfy the interface requirement.
Just another reason why we use each type of threading.
Extending Thread
class will not give you an option to extend any other class. But if you implement Runnable
interface you could extend other classes in your class..
So depending on your design requirement you could use either of the menthods.