Java Thread.start() invoked twice on the same thread — what happens?\n\nclass MyThread extends Thread \n{\n public static void main(String [] args) \n {\n MyThread t = new MyThread();\n t.start();\n System.out.print("one. ");\n t.start();\n System.out.print("two. ");\n }\n public void run() \n {\n System.out.print("Thread ");\n }\n}\n\nChoose 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