Runnable interface in Java

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 of subclassing Thread when a task can be done by overriding only run() method of Runnable.

Steps to create a new Thread using Runnable :
1. Create a Runnable implementer and implement run() method.
2. Instantiate Thread class and pass the implementer to the Thread, Thread has a constructor which accepts Runnable instance.
3. Invoke start() of Thread instance, start internally calls run() of the implementer. Invoking start(), creates a new Thread which 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,

filter_none

edit
close

play_arrow

link
brightness_4
code

public class RunnableDemo {
  
    public static void main(String[] args)
    {
        System.out.println("Main thread is- "
                        + Thread.currentThread().getName());
        Thread t1 = new Thread(new RunnableDemo().new RunnableImpl());
        t1.start();
    }
  
    private class RunnableImpl implements Runnable {
  
        public void run()
        {
            System.out.println(Thread.currentThread().getName()
                             + ", executing run() method!");
        }
    }
}

chevron_right


Output:

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

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

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



filter_none

edit
close

play_arrow

link
brightness_4
code

import java.io.FileNotFoundException;
  
public class RunnableDemo {
  
    public static void main(String[] args)
    {
        System.out.println("Main thread is- " +
                          Thread.currentThread().getName());
        Thread t1 = new Thread(new RunnableDemo().new RunnableImpl());
        t1.start();
    }
  
    private class RunnableImpl implements Runnable {
  
        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();
        }
    }
}

chevron_right


Output:

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)

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.

References :
http://www.javargon.com/2016/11/javalangrunnable-interface.html



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.