Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
The Runnable interface in Java is the core element when you are working with Threads. Any Java class intending to execute threads must implement the Runnable interface. Through the medium of this article, I will give you complete insights into the Runnable interface in Java and how to implement it.
Below are the topics covered in this article:
java.lang.Runnable is a type of functional interface, designed to provide a standard protocol for objects that intend to execute code while they are still active. In other words, it is the primary template for the objects which want to be executed by a thread. Moreover, the Runnable interface provides a means for a class to be active without having to subclass Thread. The class implementing the Runnable interface in Java can run without subclassing Thread. All you need to do is to instantiate a Thread instance and pass it in as the target. This interface is mostly implemented when no other method than the run() method is intended to be used. This interface defines a single method of no arguments called run() which holds the code that needs to be executed by the thread. Thus the classes implement the Runnable interface needs to override the run().
This method returns nothing thus it is defined with a void data type. Below is the method declaration:
Syntax:
public void run()
Let’s now move ahead and see what are the various steps involved to use the Runnable interface in Java.
Below I have listed down the various steps involved in implementing the Runnable interface in Java:
Below I have shown a demo to implement the Runnable interface in Java.
package edureka; public class EduRunnableDemo { public static void main(String[] args) { System.out.println("From main() : " + Thread.currentThread().getName()); System.out.println("Creating Runnable Instance..."); Runnable runnable = new Runnable() { @Override public void run() { System.out.println("From run() : " + Thread.currentThread().getName()); } }; System.out.println("Creating a Thread Instance..."); Thread thread = new Thread(runnable); System.out.println("Launching a Thread..."); thread.start(); } }
This code will generate the following output:
From main() : main Creating Runnable Instance... Creating a Thread Instance... Launching a Thread... From run() : Thread-0
With this, we come to the end of this article on Runnable Interface in Java. If you want to know more about Java you can refer to our other Java Blogs.
Now that you have understood what is Runnable Interface in Java, check out the Java Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA Training and Certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
Got a question for us? Please mention it in the comments section of this “Runnable Interface in Java” article and we will get back to you as soon as possible.
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 21st December,2024 21st December SAT&SUN (Weekend Batch) | View Details |
Java Course Online | Class Starts on 1st March,2025 1st March SAT&SUN (Weekend Batch) | View Details |
edureka.co