Java的线程错误IllegalThreadState线程已经启动 [英] Java Threading Error IllegalThreadState thread already started
问题描述
每当我开始我的主题我总是做此项检查。我没有找到任何地方,我呼吁启动线程而不做以下
Whenever I start my thread I always do this check. I did not find anywhere that I called start on thread without doing the check below
if (!myThread.isAlive())
myThread.start();
不过,我最终IllegalThreadStateException:线程已经启动。这实际上崩溃我的应用程序(机器人)。那么,有没有一些其他的检查,我需要启动一个线程之前做?
Nevertheless, I end up with IllegalThreadStateException : Thread already started. This actually crashes my app (android). So is there some other check I need to do before starting up a thread?
推荐答案
您应该检查线程已经开始使用<一个href="http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#getState%28%29">getState()并启动它仅当它的状态是NEW ,否则,创建新的线程(如果需要)。
You should check if the thread has already started using getState() and start it only if its state is NEW, otherwise create new thread (if needed).
这篇关于Java的线程错误IllegalThreadState线程已经启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!