Home » Java Programming » Threads

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?

Correct Answer: public class MyRunnable implements Runnable{public void run(){}}

Explanation:

The class correctly implements the Runnable interface with a legal public void run() method.


Option A is incorrect because interfaces are not extended; they are implemented.


Option B is incorrect because even though the class would compile and it has a valid public void run() method, it does not implement the Runnable interface, so the compiler would complain when creating a Thread with an instance of it.


Option D is incorrect because the run() method must be public.


← Previous Question Next Question→

Discussion & Comments

RickyTic

Embark into the massive galaxy of EVE Online. Start your journey today. Trade alongside hundreds of thousands of players worldwide. <a href=https://www.eveonline.com/signup?invc=46758c20-63e3-4816-aa0e-f91cff26ade4>Free registration</a>

Join Discussion