Runnable Interface in Java



An interface is like a reference type, similar to a class that enforces the rules that the class must implements(It is a keyword). An interface may have abstract methods i.e. methods without a definition and also constants or variables with fixed value.

What is a Runnable interface in Java?

If your class is intended to be executed as a thread, then you can achieve this by implementing a Runnable interface. This belongs to the java.lang package.

The Runnable interface in Java is a functional interface that enables the compiled code of the class to run as a separate thread. The interface is used for developing separate threads, and it is commonly used together with the Thread class.

The interface has one defined function, and one argument, which resembles the void run() method. This method is defined as encapsulated to a class and this is automatically executed by Java when the specific thread is initiated. As it seems, the chunk of code that is to be executed during thread initiation is specified within the run() method.

Runnable Interface functionalities

Here are few things we should remember about the Runnable interface functionalities:

Block activity: Performing block activities such as I/O or setting Thread.sleep in the run() method may result in the specific thread freezing. This may cause system slowdowns or the threads to deadlock. In this instance, however, it is possible to employ an approach of nonblocking I/O or disseminate the blocking tasks across multiple begin threads.

Method references or lambda expressions: Runnable is one of the interfaces that can be assigned as a method reference or as a lambda expression.

To learn the runnable interface in Java, We need to go through the following programs:

  • Class implementing Runnable interface
  • Thread object and lambda expression

Class implementing Runnable interface

In Java, a class implementing the Runnable interface can be executed by a thread. It defines the run() method containing the code to run concurrently with other threads.

Example

In this program, the 'MyRunnable' class implements the Runnable interface. In the main method, an instance of 'MyRunnable' is passed to a Thread object, and the thread is started using the start() method.

class MyRunnable implements Runnable {
   @Override
   public void run() {
       System.out.println("Thread is running...");
   }
}

public class Main {
   public static void main(String[] args) {
   implementation class
       MyRunnable runnable = new MyRunnable();
       Thread thread = new Thread(runnable);
       thread.start();
   }
}

Output

The above program produce the following result ?

Thread is running...

Using lambda expression

Using a lambda expression, we can implement the Runnable interface in Java by directly passing the task to the Thread constructor. This makes the run() method concise and easy to read.

Example

In this program, we are creating a thread object and passing a lambda expression to implement the runnable interface.

public class Main {
   public static void main(String[] args) {
      Thread thread = new Thread(() -> {
          System.out.println("Thread is running...");
      });
      
      thread.start();
   }
}

Output

The above program produce the following result ?

Thread is running...
Revathi Satya Kondra
Revathi Satya Kondra

Technical Content Writer, Tutorialspoint

Updated on: 2025-01-17T19:43:29+05:30

682 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements