Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Threading Error IllegalThreadState thread already started

Tags:

java

android

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();

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?

like image 553
Androider Avatar asked Apr 11 '11 22:04

Androider


2 Answers

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).

like image 173
MByD Avatar answered Nov 08 '22 14:11

MByD


Do you create a new instance for myThread reference using new? You can only perform myThread.start() once on a single instance.

Checking if it is alive is not the right way. Create a new instance.

like image 34
chemical Avatar answered Nov 08 '22 12:11

chemical