From the course: Java Threads

Thread class and Runnable interface - Java Tutorial

From the course: Java Threads

Thread class and Runnable interface

- [Instructor] Java being an object-oriented programming language, a Java thread is an object like any other Java object. A thread is an instance of java.lang.Thread class or any subclass of it. The Thread class encapsulates the attributes and behavior of a thread. A thread has attributes like id, name, priority, state, and etcetera, and behavior like start, sleep, join, interrupt, and etcetera. An object of this Thread class or its subclass can represent a separate path of execution within a program. It can run in parallel with multiple other similar threads. The running behavior of a thread is represented by the java.lang.Runnable interface. It is an abstract representation of the running behavior of a thread. The Runnable interface has just one method, public void run. The run method should be implemented putting in it the block of code that you need executed as a thread. In other words, the run method tells the thread what to do. For the thread to get the Runnable behavior, it has to implement the run method. That's why the java.lang.Thread class implements the Runnable interface. This is why you see a public void run method available in the Thread class. This run method should be overridden to implement how your thread should behave when it's running. Basically any subclass of the Thread class should provide its own implementation to the run method so that it gets the Running behavior. You use the Thread class to create multiple separate parts of execution within your Java program. You implement the Runnable interface to specify the running behavior of your thread.

Contents