Open In App

Java Runnable Interface

Last Updated : 10 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

java.lang.Runnable is an interface that is to be implemented by a class whose instances are intended to be executed by a thread. There are two ways to start a new Thread – Subclass Thread and implement Runnable. There is no need to subclass a Thread when a task can be done by overriding only the run() method of Runnable. 

Steps to Create a New Thread using Runnable 

  1. Create a Runnable implementer and implement the run() method. 
  2. Instantiate the Thread class and pass the implementer to the Thread, Thread has a constructor which accepts Runnable instances.
  3. Invoke start() of Thread instance, start internally calls run() of the implementer.
    • Invoking start() creates a new Thread that executes the code written in run().
    • Calling run() directly doesn’t create and start a new Thread, it will run in the same thread.
    • To start a new line of execution, call start() on the thread. 

Example:

java
// Runnable Interface Implementation
public class Geeks 
{
    private class RunnableImpl implements Runnable 
    {
      	// Overriding the run Method
      	@Override
        public void run()
        {
            System.out.println(Thread.currentThread().getName()
                             + ", executing run() method!");
        }
    }  
  
  	// Main Method
    public static void main(String[] args) 
    {    
      	System.out.println("Main thread is: "
                           + Thread.currentThread().getName());
      
      	// Creating Thread
        Thread t1 = new Thread(new Geeks().new RunnableImpl());
        
      	// Executing the Thread
      	t1.start();
    }
}

Output
Main thread is: main
Thread-0, executing run() method!

Explanation: The output shows two active threads in the program – main thread and Thread-0, main method is executed by the Main thread but invoking the start on RunnableImpl creates and starts a new thread – Thread-0.

Handling Exception in Runnable

Runnable interface can’t throw checked exception but RuntimeException can be thrown from the run(). Uncaught exceptions are handled by the exception handler of the thread, if JVM can’t handle or catch exceptions, it prints the stack trace and terminates the flow. 

Example:

java
// Checking Exceptions in Runnable Interface
import java.io.FileNotFoundException;

public class Geeks
{
    private class RunnableImpl implements Runnable 
    {
      	// Overriding the run method 
      	@Override
        public void run()
        {
            System.out.println(Thread.currentThread().getName()
                             + ", executing run() method!");
            
          	// Checked exception can't be thrown, Runnable must
            // handle checked exception itself
            try {
                throw new FileNotFoundException();
            }
            catch (FileNotFoundException e) {
                System.out.println("Must catch here!");
                e.printStackTrace();
            }

            int r = 1 / 0;
            
          	// Below commented line is an example
            // of thrown RuntimeException.
          
            // throw new NullPointerException();
        }
    }
  
  	public static void main(String[] args)
    {
        System.out.println("Main thread is: " +
                          Thread.currentThread().getName()); 
      
      	// Create a Thread
      	Thread t1 = new Thread(new Geeks().new RunnableImpl());
        
      	// Running the Thread
      	t1.start();
    }
}

Output:

Thread-0, executing run() method!
Must catch here!
java.io.FileNotFoundException
at RunnableDemo$RunnableImpl.run(RunnableDemo.java:25)
at java.lang.Thread.run(Thread.java:745)
Exception in thread "Thread-0" java.lang.ArithmeticException: / by zero
at RunnableDemo$RunnableImpl.run(RunnableDemo.java:31)
at java.lang.Thread.run(Thread.java:745)

Explanation: The output shows that Runnable can’t throw checked exceptions, FileNotFoundException in this case, to the callers, it must handle checked exceptions in the run() but RuntimeExceptions (thrown or auto-generated) are handled by the JVM automatically.



Next Article
Practice Tags :

Similar Reads

  翻译: