In Java multithreading, which method contains the actual body of work executed by a Thread (i.e., the code that runs on the thread's stack when scheduled by the JVM scheduler)?

Difficulty: Easy

Correct Answer: run();

Explanation:


Introduction / Context:
Understanding the lifecycle of a Java thread is essential for writing concurrent programs. A frequent confusion is between the method that starts a thread and the method that actually executes on the new thread.



Given Data / Assumptions:

  • A class extends Thread or implements Runnable.
  • The thread lifecycle includes states such as New, Runnable, Running, Blocked/Waiting, and Terminated.
  • The JVM scheduler invokes user code on a separate call stack when appropriate.


Concept / Approach:
The start() method asks the JVM to create a new thread of execution, whereas the run() method is the entry point for the work performed by that thread. The developer places the thread's body (business logic) inside run().



Step-by-Step Solution:

1) Override run() in a Thread subclass or provide a Runnable whose run() implements the task.2) Call start() on the Thread instance to transition from New to Runnable; the JVM will then invoke run() on the new thread.3) The code inside run() is what executes concurrently.


Verification / Alternative check:
Calling run() directly from the main thread executes synchronously (no new thread). Only start() spawns a new thread that then calls run().



Why Other Options Are Wrong:

  • start() → starts the thread but doesn't contain the work.
  • stop() → deprecated, dangerous; not the body.
  • main() → process entry point, not a thread's body.


Common Pitfalls:
Beginners often call run() directly, mistakenly assuming it starts a new thread.



Final Answer:
run();

More Questions from Threads

Discussion & Comments

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