Given:public class Main {
public static void main(String[] args)
throws InterruptedException {
Thread t = new Thread(() -> System.out.print("run "));
System.out.print(t.getState() + " ");
t.start();
t.join();
System.out.print(t.getState() + " ");
t.start();
System.out.print("done");
}
}What is the result?
Choose one.
A thread moves NEW → RUNNABLE → TERMINATED and can be started exactly once.
Before start() the thread is NEW. start() runs the task, which prints run, and join() waits until the thread is TERMINATED. Calling start() a second time on the same Thread object throws IllegalThreadStateException, an unchecked exception, so the program ends before printing done.
- Read the state before start(): NEW.
- start() plus join() runs the task to completion: run is printed and the state is TERMINATED.
- Recall that start() may be called only once per Thread object.
- The second start() throws IllegalThreadStateException and done is skipped.
Exam tip: Each Thread object is single-use: a second start() throws IllegalThreadStateException.
Java Threads, Virtual Threads, Executors and Scoped Values (1Z0-831) — the lesson that teaches this.