This set of Java MCQs on “Implementing Runnable interface for Threads”.
1. Which of these method is used to implement Runnable interface? Answer: b 2. Which of these method is used to begin the execution of a thread? Answer: b 3. Which of these statement is incorrect? Answer: d 4. What will be the output of the following Java code? a) My Thread 5. What will be the output of the following Java code? a) My Thread 6. What will be the output of the following Java code? a) My Thread 7. What will be the output of the following Java code? a) Thread[New Thread,0,main] 8. What will be the output of the following Java code? a) true
a) stop()
b) run()
c) runThread()
d) stopThread()
Clarification: To implement Runnable interface, a class needs only to implement a single method called run().
a) run()
b) start()
c) runThread()
d) startThread()
Clarification: None.
a) A thread can be formed by implementing Runnable interface only
b) A thread can be formed by a class that extends Thread class
c) start() method is used to begin execution of the thread
d) run() method is used to begin execution of a thread before start() method in special cases
Clarification: run() method is used to define the code that constitutes the new thread, it contains the code to be executed. start() method is used to begin execution of the thread that is execution of run(). run() itself is never used for starting execution of the thread.
class newthread implements Runnable
{
Thread t;
newthread()
{
t = new Thread(this,"My Thread");
t.start();
}
public void run()
{
System.out.println(t.getName());
}
}
class multithreaded_programing
{
public static void main(String args[])
{
new newthread();
}
}
b) Thread[My Thread,5,main]
c) Compilation Error
d) Runtime Error
Clarification: None.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
My Thread
class newthread implements Runnable
{
Thread t;
newthread()
{
t = new Thread(this,"My Thread");
t.start();
}
public void run()
{
System.out.println(t);
}
}
class multithreaded_programing
{
public static void main(String args[])
{
new newthread();
}
}
b) Thread[My Thread,5,main]
c) Compilation Error
d) Runtime Error
Clarification: None.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
Thread[My Thread,5,main]
class newthread implements Runnable
{
Thread t;
newthread()
{
t = new Thread(this,"My Thread");
t.start();
}
}
class multithreaded_programing
{
public static void main(String args[])
{
new newthread();
}
}
b) Thread[My Thread,5,main]
c) Compilation Error
d) Runtime Error
Clarification: Thread t has been made by using Runnable interface, hence it is necessary to use inherited abstract method run() method to specify instructions to be implemented on the thread, since no run() method is used it gives a compilation error.
Output:
$ javac multithreaded_programing.java
The type newthread must implement the inherited abstract method Runnable.run()
class newthread implements Runnable
{
Thread t;
newthread()
{
t = new Thread(this,"New Thread");
t.start();
}
public void run()
{
t.setPriority(Thread.MAX_PRIORITY);
System.out.println(t);
}
}
class multithreaded_programing
{
public static void main(String args[])
{
new newthread();
}
}
b) Thread[New Thread,1,main]
c) Thread[New Thread,5,main]
d) Thread[New Thread,10,main]
Clarification: Thread t has been made with default priority value 5 but in run method the priority has been explicitly changed to MAX_PRIORITY of class thread, that is 10 by code ‘t.setPriority(Thread.MAX_PRIORITY);’ using the setPriority function of thread t.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
Thread[New Thread,10,main]
class newthread implements Runnable
{
Thread t;
newthread()
{
t1 = new Thread(this,"Thread_1");
t2 = new Thread(this,"Thread_2");
t1.start();
t2.start();
}
public void run()
{
t2.setPriority(Thread.MAX_PRIORITY);
System.out.print(t1.equals(t2));
}
}
class multithreaded_programing
{
public static void main(String args[])
{
new newthread();
}
}
b) false
c) truetrue
d) falsefalse
Clarification: Threads t1 & t2 are created by class newthread that is implementing runnable interface, hence both the threads are provided their own run() method specifying the actions to be taken. When constructor of newthread class is called first the run() method of t1 executes than the run method of t2 printing 2 times “false” as both the threads are not equal one is having different priority than other, hence falsefalse is printed.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
falsefalse