micdoodle8
micdoodle8

Reputation: 430

IllegalThreadStateException on Thread Start

Some of my users have been reporting IllegalThreadStateExceptions, which lead back to the following code. I was unable to recreate it, and most people are able to execute this part of the code with no issues.

The code is re-executed periodically, so the old thread should be replaced by a new one and started, but that's where the error occurs.

if (head.threadSeal != null)
{
    head.threadSeal.interrupt();
}

head.threadSeal = new ThreadFindSeal();
head.threadSeal.start(); // IllegalThreadStateException here

Relevant stack trace:

java.lang.IllegalThreadStateException
    at java.lang.Thread.start(Thread.java:704)

Am I missing something in the documentation, why would start ever fail on a new thread?

Upvotes: 2

Views: 2075

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279950

The javadoc of Thread#start() states

Throws: IllegalThreadStateException - if the thread was already started.

You haven't give much information, but the following could happen.

First thread executes

head.threadSeal = new ThreadFindSeal();

Second thread then executes

head.threadSeal = new ThreadFindSeal();
head.threadSeal.start();

replacing the Thread and starting it.

The first thread then executes

head.threadSeal.start();

which is called on the same Thread object as before, which was already started.

Upvotes: 2

Related Questions