Java - topic state exception

I got the following error when I try to start 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 since it is in a loop (socket handler). As far as I know, this error means that it cannot start a new thread because it is already running. Which is strange, I have isAlive before.

Any idea why this is?

Thank.

+3
source share
5 answers

As far as I know, this error means that it cannot start a new thread because it is already running.

No, this means that you cannot start a thread that is already running.

You cannot restart the thread you are trying to do. From the documentation forstart() :

Throws: IllegalThreadStateException - .

, ExecutorService, , .

+8

start , . .

+2

, , , , . .

, . , , , .

, ExecutorService.

+1

Vodka, creating a new stream every time you have a task, are expensive. You must use the thread pool. Basically, this is that you have a pool of n threads, and you send jobs to it. If some thread is free, it will complete your task. If a thread executes with its task, it returns to the pool, waiting for some other task.

Try using ExecutorService to combine threads.

+1
source

I found the easiest way:

Every time I need to start a new thread, I create a new one:

Thread t = new Thread()
{
    public void run()
    {
        // Do your deal here
    }
};
t.start();
0
source

All Articles