From the course: Java Threads
Unlock the full course today
Join today to access over 24,400 courses taught by industry experts.
Creating a thread: Implementing the Runnable interface - Java Tutorial
From the course: Java Threads
Creating a thread: Implementing the Runnable interface
- [Instructor] The second way to create a thread is by implementing the Runnable interface. To demonstrate this way of creating a thread, I've added a Java class named MyRunnable. Next, I'm implementing the MyRunnable class by the Runnable interface. So I'll type implements Runnable. Now you can see Intellij showing that there's an error. It says, Class MyRunnable must either be declared abstract or implement abstract method run in Runnable. This is because the Runnable interface defines just one method named run. And any class implementing the Runnable interface should implement this run method. You can do this either by typing the syntax of the run method or, since you are using the IDE, simply by clicking on the implement method's link here on the error message. So yes, we want to implement the public void run method of the Runnable interface. Go ahead and click OK. And there you have it, the skeleton of the…
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.
Contents
-
-
-
-
Thread class and Runnable interface2m 17s
-
(Locked)
Creating a thread: Extending the Thread class4m 4s
-
(Locked)
Creating a thread: Implementing the Runnable interface3m 31s
-
(Locked)
Starting a thread7m
-
(Locked)
Extend a thread or implement a Runnable?2m 27s
-
(Locked)
Challenge: Webinar polling system3m 25s
-
(Locked)
Solution: Webinar polling system3m 33s
-
-
-