Examine the following code and select the correct options.
public class Main { public static void main(String[] args) { Thread bug = new Thread() { public void run() { System.out.print("check bugs"); }/*w w w . j a va2 s. c o m*/ }; Thread t = new Thread(bug); t.run(); } }
run()
with t.start()
will throw a compilation exception.run()
with t.start()
will generate the same output on the system's console.b, f
The following code creates an anonymous class that subclasses class Thread.
Its instance is referred by bug, a reference variable of type Thread.
Thread bug = new Thread() { public void run() { System.out.print("check bugs"); } };
Because class Thread implements the Runnable interface, you can pass its instance as a target object to instantiate another Thread instance, t:.
Thread t = new Thread(bug);
The variable t refers to an anonymous class instance that overrides its method run()
.
So calling t.run()
executes the overridden method run()
and prints check bugs only once.
Option (f) is correct.
Even though calling t.run()
doesn't start a separate thread of execution and t.start()
does, both will print check bugs once on the system's console.