|
How to create a thread?
There are two ways:
- Extend java.lang.Thread Class
- Implement Runnable interface
Differences between both the approaches:
- Since Runnable is an interface, and hence you get all the benefits of interface (like Multiple Inheritance). This is why in case of large applications, implementing Runnable interface is recommended. In fact, the Thread class in itself implements Runnable interface.
- A class that implements Runnable is a class, not a thread.
- This also means that in case of extending Thread class, we get an unique object associated with each of our Threads. In case of implementing Runnable, many threads can share the same object instance.
|