Given that a static method doIt()
in the class Work represents work to be done, which block of code will succeed in starting a new thread that will do the work?.
Select the one correct answer.
(a) Runnable r = new Runnable() { public void run() { Work.doIt();/*from ww w.j a va2 s. c o m*/ } }; Thread t = new Thread(r); t.start(); (b) Thread t = new Thread() { public void start() { Work.doIt(); } }; t.start(); (c) Runnable r = new Runnable() { public void run() { Work.doIt(); } }; r.start(); (d) Thread t = new Thread(new Work()); t.start(); (e) Runnable t = new Runnable() { public void run() { Work.doIt(); } }; t.run();
(a)
A Thread object executes the run()
method of a Runnable object on a separate thread when started.
A Runnable object can be given when constructing a Thread object.
If no Runnable object is supplied, the Thread object (which implements the Runnable interface) will execute its own run()
method.
A thread is initiated using the start()
method of the Thread object.