From the course: Java Threads

Unlock the full course today

Join today to access over 24,400 courses taught by industry experts.

Extend a thread or implement a Runnable?

Extend a thread or implement a Runnable? - Java Tutorial

From the course: Java Threads

Extend a thread or implement a Runnable?

- [Instructor] The two ways to create a thread in Java each have advantages and disadvantages. The first way that is extending the class that you want to run as a separate thread by the java.lang.Thread class is a convenient way. This is because you just have to override the run method in your class to implement the running behavior of your thread. And then, instantiate your class without having to pass a runnable instance to its constructive when creating a new thread object. The second way is not much different, since you implement the runnable interface and override the run method to implement the running behavior. The difference here is having to instant your runnable class first, and then creating a thread instance passing in that runnable instance to its constructor. If you use the first way, you have to keep in mind that your class will not be able to extend any other classes since Java does not allow…

Contents