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

  • Threads - General Questions
  • Threads - Finding the output
  • Threads - Pointing out the correct statement

1. 

Which three are methods of the Object class?

  1. notify();
  2. notifyAll();
  3. isInterrupted();
  4. synchronized();
  5. interrupt();
  6. wait(long msecs);
  7. sleep(long msecs);
  8. yield();
A. 1, 2, 4
B. 2, 4, 5
C. 1, 2, 6
D. 2, 3, 4


2. 
class X implements Runnable 
{ 
    public static void main(String args[]) 
    {
        /* Missing code? */
    } 
    public void run() {} 
}
Which of the following line of code is suitable to start a thread ?
A. Thread t = new Thread(X);
B. Thread t = new Thread(X); t.start();
C. X run = new X(); Thread t = new Thread(run); t.start();
D. Thread t = new Thread(); x.run();


3. 

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)


4. 

Which class or interface defines the wait(), notify(),and notifyAll() methods?

A. Object
B. Thread
C. Runnable
D. Class


5. 

Assume the following method is properly synchronized and called from a thread A on an object B:

wait(2000);

After calling this method, when will the thread A become a candidate to get another turn at the CPU?

A. After thread A is notified, or after two seconds.
B. After the lock on B is released, or after two seconds.
C. Two seconds after thread A is notified.
D. Two seconds after lock B is released.


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

A directory of Objective Type Questions covering all the Computer Science subjects. Here you can access and discuss Multiple choice questions and answers for various competitive exams and interviews.

Discussion Forum

Que. 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:public void run()

Confused About the Answer? Ask for Details Here
Know Explanation? Add it Here


Similar Questions:


View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save 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

    java

    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

    java

    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.


    Which of the methods should be implemented if any class implements the runnable interface?

    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.

    Which method is implemented runnable interface?

    The runnable interface implementation uses the code inside the run() method and executes it on a concurrent thread. You can either invoke class, create new variables, or call the action in the run() method to implement the runnable interface in Java.

    Which statement is true for any concrete class implementing the Java Lang runnable interface?

    Which two statements are true for any concrete class implementing the java. lang. Runnable interface? You can extend the Runnable interface as long as you override the public run() method.

    Which method of the runnable interface that must be implemented by all threads?

    While creating a thread class we must override the run() method of the Thread class. This method provides an entry point for the thread and you will put your complete business logic inside this method.