Java Thread.start() invoked twice on the same thread — what happens? class MyThread extends Thread { public static void main(String [] args) { MyThread t = new MyThread(); t.start(); System.out.print("one. "); t.start(); System.out.print("two. "); } public void run() { System.out.print("Thread "); } } Choose the correct outcome.

Difficulty: Easy

Correct Answer: An exception occurs at runtime.

Explanation:


Introduction / Context:
This question targets lifecycle rules for java.lang.Thread. Calling start() transitions a new thread from NEW to RUNNABLE. A second call to start() on the same Thread object violates the lifecycle and results in a runtime exception.



Given Data / Assumptions:

  • t is constructed, then start() is called twice.
  • run() prints "Thread ".
  • There are prints on the main thread: "one. " and "two. ".


Concept / Approach:
Thread.start() may be invoked only once per Thread object. A second call throws IllegalThreadStateException, an unchecked RuntimeException. Therefore the program compiles, begins running, and then fails at the second start(). Output produced before the exception (such as "Thread " and/or "one. ") is nondeterministic, but the guaranteed outcome is a runtime exception.



Step-by-Step Solution:

Call t.start() → valid; run() may execute concurrently.Print "one. ".Call t.start() again → IllegalThreadStateException is thrown.Execution terminates abnormally; any subsequent prints may not execute.


Verification / Alternative check:
Replace the second start() with a fresh Thread object or call run() directly (which does not start a new thread) to avoid the exception.



Why Other Options Are Wrong:

  • Compilation succeeds; the error is at runtime.
  • Deterministic combined output is impossible due to exception and scheduling.


Common Pitfalls:
Confusing run() with start(); run() can be called multiple times but runs on the current thread.



Final Answer:
An exception occurs at runtime.

More Questions from Threads

Discussion & Comments

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