From the course: Java EE: Concurrency and Multithreading
Unlock the full course today
Join today to access over 24,800 courses taught by industry experts.
Using the Runnable interface to create and run a thread
From the course: Java EE: Concurrency and Multithreading
Using the Runnable interface to create and run a thread
- [Instructor] So we have seen how to create a thread using the thread class in your Java application. Now let's understand how to create a thread using the Runnable interface. Runnable interface, like the thread class, is present under the java.lang package. And like the thread class again, Runnable interface has also got the run method declared, which is going to specify the job of the thread to be spawned, right? Now, whenever you want to create threads in your system, this runnable interface option is always preferred. That's because in Java we all know that we can have a class that implements multiple interfaces but you cannot have a class that extends more than one class, right? So if you try to extend the thread class you wouldn't be able to extend any other class. So we generally go with the option of the runnable interface to create threads in our multi-threaded Java applications. So let's go ahead and do a demo for this. The use case is the same. We are still going to have…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.