0

Whats the fundamental difference between extending Thread and implementing Runnable?

22nd Nov 2017, 4:22 PM
Nilabhra C
Nilabhra C - avatar
2 ответов
0
One difference between implementing Runnable and extending Thread is that by extending Thread , each of your threads has a unique object associated with it, whereas implementing Runnable , many threads can share the same object instance. A class that implements Runnable is not a thread and just a class. For a Runnable to become a Thread, You need to create an instance of Thread and passing itself in as the target. In most cases, the Runnable interface should be used if you are only planning to override the run() method and no other Thread methods. This is important because classes should not be subclassed unless the programmer intends on modifying or enhancing the fundamental behavior of the class. When there is a need to extend a superclass, implementing the Runnable interface is more appropriate than using the Thread class. Because we can extend another class while implementing Runnable interface to make a thread. But if we just extend the Thread class we can't inherit from any other class.
22nd Nov 2017, 7:34 PM
Wasula Benjamin
Wasula Benjamin - avatar
0
thanks..
26th Nov 2017, 6:16 AM
Nilabhra C
Nilabhra C - avatar