当我尝试重新启动我的线程时出现以下错误。
Exception in thread "Thread-1" java.lang.IllegalThreadStateException
at java.lang.Thread.start(Unknown Source)
at com.jrat.server.Server.run(Server.java:159)
这是一行:
if (!t.isAlive()) t.start();
代码可以在循环(套接字处理程序)中执行多次。 据我所知,这个错误意味着它不能启动一个新的线程,因为它已经在运行了。奇怪的是我之前有一个 isAlive。
知道为什么会这样吗?
谢谢。
最佳答案
As far as I know, this error means that it can't start a new Thread because it is already running.
不,这意味着您不能启动已经启动的线程。
您不能重新启动 一个线程,而这正是您想要做的。来自 the documentation for start()
:
Throws: IllegalThreadStateException - if the thread was already started.
您可能应该使用 ExecutorService
来代替,猜测 - 但您并不清楚您要做什么。
关于Java - 线程状态异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10207369/