Thread creation in Java: which two techniques correctly start work on a new thread of execution?

Difficulty: Easy

Correct Answer: 1 and 4

Explanation:


Introduction / Context:
This question targets the two canonical ways to define work that runs on a separate thread in Java: subclassing Thread and overriding run(), or implementing Runnable and supplying an instance to a Thread.


Given Data / Assumptions:

  • Statements listed: (1) Extend java.lang.Thread and override run(); (2) Extend java.lang.Runnable and override start(); (3) Implement java.lang.Thread and implement run(); (4) Implement java.lang.Runnable and implement the run() method; (5) Implement java.lang.Thread and implement start().


Concept / Approach:
Runnable is an interface whose single method, run(), contains the task body. Thread is a concrete class with start(), which arranges to execute run() on a new thread. You never “implement Thread”; you can extend it. start() should not be overridden just to run work; its job is to create the new thread and then invoke run().


Step-by-Step Solution:
(1) Extend Thread and override run(): valid. Start by calling new Subclass().start().(2) Extend Runnable and override start(): invalid; Runnable is an interface, cannot be “extended,” and it has no start().(3) Implement Thread and implement run(): invalid; Thread is a class, not an interface.(4) Implement Runnable and implement run(): valid; pass the Runnable to new Thread(r).start().(5) Implement Thread and implement start(): invalid for the same reason as (3).


Verification / Alternative check:
Write two minimal examples: class T extends Thread { public void run(){...} } and class R implements Runnable { public void run(){...} }. In both cases, call start() on a Thread object to launch the task.


Why Other Options Are Wrong:
They misuse the distinction between classes and interfaces or override the wrong method.


Common Pitfalls:
Calling run() directly instead of start(); trying to “implement Thread”.


Final Answer:
1 and 4

More Questions from Threads

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion