# java.lang.IllegalThreadStateException

- **ID:** `java/illegal-thread-state-exception`
- **Domain:** java
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 85%

## Root Cause

Thrown when attempting to start a thread that has already been started or when the thread is in an invalid state for the requested operation.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| Java 8 | active | — | — |
| Java 11 | active | — | — |
| Java 17 | active | — | — |
| Java 21 | active | — | — |

## Workarounds

1. **Check the thread's state before starting: `if (thread.getState() == Thread.State.NEW) { thread.start(); }`. This ensures start() is only called once.** (95% success)
   ```
   Check the thread's state before starting: `if (thread.getState() == Thread.State.NEW) { thread.start(); }`. This ensures start() is only called once.
   ```
2. **Use a thread pool (e.g., ExecutorService) instead of managing threads manually. Submit tasks to the pool, which handles thread lifecycle correctly.** (90% success)
   ```
   Use a thread pool (e.g., ExecutorService) instead of managing threads manually. Submit tasks to the pool, which handles thread lifecycle correctly.
   ```
3. **If restarting is needed, create a new Thread instance for each execution: `thread = new Thread(task); thread.start();`.** (85% success)
   ```
   If restarting is needed, create a new Thread instance for each execution: `thread = new Thread(task); thread.start();`.
   ```

## Dead Ends

- **** — Thread cannot be restarted; the exception indicates a logic error that will recur. (90% fail)
- **** — Synchronization does not prevent calling start() on an already started thread; the check must be explicit. (70% fail)
- **** — Thread.stop() is deprecated and unsafe; it may leave the thread in an inconsistent state. (80% fail)
