Java - Thread State Exception

java, multithreading

Solution

As far as I know, this error means that it can't start a new Thread because it is already running.

No, it means you can't start a thread which has already been started.

You can't restart a thread, which is what you're trying to do. From the documentation for `start()`:

Throws: IllegalThreadStateException - if the thread was already started.

You should probably be using an `ExecutorService` instead, at a guess - but it's not really clear what you're trying to do.

Problem

I got the following error when I try to start again my thread. ``` Exception in thread "Thread-1" java.lang.IllegalThreadStateException at java.lang.Thread.start(Unknown Source) at com.jrat.server.Server.run(Server.java:159) ``` Here is the line: ``` if (!t.isAlive()) t.start(); ``` The code can be executed many times as it is in a loop (socket handler). As far as I know, this error means that it can't start a new Thread because it is already running. What's weird is I have a isAlive before. Any idea why it is like that? Thanks.

Original source