Which method must be defined by a class implementing the java lang Runnable interface

1. 

Which method must be defined by a class implementing the java.lang.Runnable interface?

[A]. void run()
[B]. public void run()
[C]. public void start()
[D]. void run(int priority)

Answer: Option B

Explanation:

Option B is correct because in an interface all methods are abstract by default therefore they must be overridden by the implementing class. The Runnable interface only contains 1 method, the void run() method therefore it must be implemented.

Option A and D are incorrect because they are narrowing the access privileges i.e. package(default) access is narrower than public access.

Option C is not method in the Runnable interface therefore it is incorrect.

View Discussion

Improve Article

Save Article

Like Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Like Article

    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 a 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 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 1

    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!");

            }

        }

    }

    Output:

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

    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. What happens when Runnable encounters an exception ? Runnable 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 2

    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!");

                try {

                    throw new FileNotFoundException();

                }

                catch (FileNotFoundException e) {

                    System.out.println("Must catch here!");

                    e.printStackTrace();

                }

                int r = 1 / 0;

            }

        }

    }

    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)

    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.


    • All Known Subinterfaces: RunnableFuture<V>, RunnableScheduledFuture<V> All Known Implementing Classes: AsyncBoxView.ChildState, ForkJoinWorkerThread, FutureTask, RenderableImageProducer, SwingWorker, Thread, TimerTask
      public interface Runnable

      The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments called run.

      This interface is designed to provide a common protocol for objects that wish to execute code while they are active. For example, Runnable is implemented by class Thread. Being active simply means that a thread has been started and has not yet been stopped.

      In addition, Runnable provides the means for a class to be active while not subclassing Thread. A class that implements Runnable can run without subclassing Thread by instantiating a Thread instance and passing itself in as the target. In most cases, the Runnable interface should be used if you are only planning to override the run() method and no other Thread methods. This is important because classes should not be subclassed unless the programmer intends on modifying or enhancing the fundamental behavior of the class.

      Since: JDK1.0 See Also:Thread, Callable

        • void run()

          When an object implementing interface Runnable is used to create a thread, starting the thread causes the object's run method to be called in that separately executing thread.

          The general contract of the method run is that it may take any action whatsoever.

          See Also:Thread.run()

    Submit a bug or feature
    For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
    Copyright © 1993, 2020, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.