Skip to content Skip to sidebar Skip to footer

When I Run Thread Second Time: Java.lang.illegalthreadstateexception: Thread Already Started

I have Thrad and Handler: Handler handler = new Handler() { @Override public void handleMessage(android.os.Message msg) { super.handleMessage(msg); //do som

Solution 1:

You should check state of that thread before starting it.

if (thread.getState() == Thread.State.NEW)
{
     thread.start();
}

Solution 2:

Its not a good Idea to start a Thread more then once. You have to check Whether a Thread is already started or not. if Thread not started yet

if(!thread.isAlive()){
thread.start();
}

The Better Idea is to Create new Thread instance.

Solution 3:

At the end of run(), your thread dies. If you want to keep it alive, then add a blocking queue to the thread and make run() a big while loop that reads from the queue. Instead of calling start for each message, just add it to the queue instead.

Of course, you still have to call start() once (when your program initializes).

Post a Comment for "When I Run Thread Second Time: Java.lang.illegalthreadstateexception: Thread Already Started"