(2) Implement the Runnable interface and define the run() method.
Implement the Runnable Interface and then instantiate an object of the class.
We need to override the run() method into our class which is the only method that needs to be implemented.
The run() method contains the logic of the thread.
When creating threads, there are two reasons why implementing the Runnable interface may be preferable to extending the Thread class:
- Extending the Thread class means that the subclass cannot extend any other class, whereas a class implementing the Runnable interface has this option.
- A class might only be interested in being runnable, and therefore, inheriting the full overhead of the Thread class would be excessive. Further the class can implement multiple interfaces, in addition to Runnable.
No comments:
Post a Comment