# java.lang.IllegalThreadStateException

- **ID:** `java/illegal-thread-state-exception`
- **领域:** java
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

当尝试启动一个已经启动的线程或线程处于请求操作无效状态时抛出。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Java 8 | active | — | — |
| Java 11 | active | — | — |
| Java 17 | active | — | — |
| Java 21 | active | — | — |

## 解决方案

1. ```
   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.
   ```
3. ```
   If restarting is needed, create a new Thread instance for each execution: `thread = new Thread(task); thread.start();`.
   ```

## 无效尝试

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