Suppose MyThread
extends java.lang.Thread.
MyRunnable
implements java.lang.Runnable but does not extend Thread.
Both classes have no-args constructors.
Which of the following cause a thread in the JVM to begin execution?
Choose all correct options.
A. (new MyThread()).start(); B. (new MyThread()).run(); C. (new MyRunnable()).run(); D. (new Thread(new MyRunnable())) E. .start();
A, D.
A is correct because the start()
method of Thread causes a thread to begin execution of the thread object's run()
method.
Calling run()
directly as in B and C just causes the run()
method to execute in the current thread.
D creates a new thread whose target is an instance of MyRunnable; this is the typical way to use the Runnable interface.