The following block of code creates a Thread using a Runnable target:Runnable target = new MyRunnable();Thread myThread = new Thread(target);Which of the following classes can be used to create the target, so that the preceding code compiles correctly?public class MyRunnable extends Runnable{public void run(){}}public class MyRunnable extends Object{public void run(){}}public class MyRunnable implements Runnable{public void run(){}}public class MyRunnable implements Runnable{void run(){}}
Question
The following block of code creates a Thread using a Runnable target:Runnable target = new MyRunnable();Thread myThread = new Thread(target);Which of the following classes can be used to create the target, so that the preceding code compiles correctly?public class MyRunnable extends Runnable{public void run(){}}public class MyRunnable extends Object{public void run(){}}public class MyRunnable implements Runnable{public void run(){}}public class MyRunnable implements Runnable{void run(){}}
Solution
The correct class that can be used to create the target, so that the preceding code compiles correctly is:
public class MyRunnable implements Runnable{
public void run(){}
}
Explanation:
In Java, Runnable is an interface, not a class, so you cannot extend it, you can only implement it. Therefore, the first option is incorrect.
The second option is incorrect because MyRunnable extends Object and defines a run method, but it does not implement the Runnable interface. Therefore, it cannot be used as a Runnable target.
The third option is correct because MyRunnable implements the Runnable interface and defines the run method. This is the correct way to create a class that can be used as a Runnable target.
The fourth option is incorrect because the run method in the Runnable interface is public, and you cannot reduce the visibility of a method when you override it. Therefore, defining run as package-private (no modifier) in MyRunnable is a compilation error.
Similar Questions
A Runnable thread is the thread state that creates an instance of the Thread class, but before the invocation of the start class.*1 pointTrueFalse
Select the correct answerWhich of the following is a correct constructor for a thread object?OptionsThread(Runnable a, String str);Thread(Runnable a, ThreadGroup t);Thread(int priority);Thread(Runnable a, int priority);
What will be the output of the program?class MyThread extends Thread { MyThread() {} MyThread(Runnable r) {super(r); } public void run() { System.out.print("Inside Thread "); } } class MyRunnable implements Runnable { public void run() { System.out.print(" Inside Runnable"); } } class Test { public static void main(String[] args) { new MyThread().start(); new MyThread(new MyRunnable()).start(); } }Prints "Inside Thread Inside Thread"Prints "Inside Thread Inside Runnable"Does not compileThrows exception at runtime
One of the following is not a method used to create a thread.*1 pointExtending a Thread classImplementing a Runnable interfaceExtending a MultiThread class
Which method of the Thread class is used to start a new thread of execution?Question 2Answera.start()b.run()c.execute()d.begin()
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.