It is usually better programming practice to use the Runnable interface rather than overriding the Thread class's run method.
Java does not support multiple inheritance, so classes that are subclasses of Thread cannot be subclasses of any other class. If you use the Runnable interface, however, you can create threads using classes that already have subclasses.
This approach is helpful when constructing your class hierarchy. Rather than saying that an object of your class "is a" thread, your class is associated with a thread that executes its code.
A further advantage of using the Runnable interface's run
method is that this can make it easier for the run
method to access protected methods and variables of its superclasses.
That is, because the run
method belongs to a subclass, it gains access to methods and variables denied to non-subclassed classes.
No comments:
Post a Comment