Java/J2EE and SOA (349 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (5 Blogs)
  • Java/J2EE and SOA (346 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

What is Runnable Interface in Java and how to implement it?

Last updated on Jun 17,2021 11.1K Views

11 / 14 Blog from Java OOPS

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:

What is the Runnable Interface in Java?

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.

Steps for using a Runnable interface in Java

Below I have listed down the various steps involved in implementing the Runnable interface in Java:

  1. The first step is to create a class that implements the Runnable interface.
  2. Now, you need to override the run method in the Runnable class.
  3. Next, you need to pass the Runnable object as a parameter to the constructor of the Thread class object while creating it. Now, this Thread object is capable of executing our Runnable class.
  4. Finally, you need to invoke the Thread object’s start method.

Implementing Runnable Interface

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.

Upcoming Batches For Java Course Online
Course NameDateDetails
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
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

What is Runnable Interface in Java and how to implement it?

edureka.co