1

In Java I am getting this Exception:

Exception in thread "main" java.lang.IllegalThreadStateException

and here is the code:

class Worker extends Thread
{
   public void run() {
   System.out.println("#");
 }
}

class Seconda extends Thread
{
   public void run() {
       System.out.println("*");
   }
}


public class First
{
   public static void main(String args[]) {
      int contatore=0;
      Worker prova = new Worker();
      Seconda prova2 = new Seconda();
      prova.start();
      prova2.start();
      for(;;) {
               if(!prova.isAlive()) { prova.start();
                                      contatore++;
                                    } 
           if(!prova2.isAlive()) { prova2.start();
                                   contatore++;
                                 }
           if(contatore==50) System.out.println(""); 
          }

   }
 }

What does the exception mean?

  • 2
    Once a thread is terminated you cannot restart it! You have to create a new `Thread` object – fge Mar 31 '14 at 10:47

5 Answers5

1

You are trying to start a thread which has already been started.

What you need to do is create the prova and prova2 inside the for loop?

public static void main(String args[]) {
  int contatore=0;
  //Worker prova = new Worker();
  //Seconda prova2 = new Seconda();
  //prova.start();
  //prova2.start();
  for(;;) {
     Worker prova = new Worker();
     Seconda prova2 = new Seconda();
     if(!prova.isAlive()) { 
        prova.start();
        contatore++;
     } 
     if(!prova2.isAlive()) { 
        prova2.start();
        contatore++;
     }
     if(contatore==50) System.out.println(""); 
     }
}
Himanshu Bhardwaj
  • 4,038
  • 3
  • 17
  • 36
1

You cannot start a thread more than one times. If you start a thread more than onceyou will get a IllegalThreadStateException.

Try,

new Thread(prova).start();
new Thread(prova).start();

if you need to start the same thread.

For More

Community
  • 1
  • 1
Rakesh KR
  • 6,357
  • 5
  • 40
  • 55
0

You must not start a thread multiple times. It can only be started once. See the JavaDoc of Thread.start():

It is never legal to start a thread more than once.

[..]

Throws IllegalThreadStateException if the thread was already started.

Martin Andersson
  • 18,072
  • 9
  • 87
  • 115
0
for(;;) {
               if(!prova.isAlive()) { prova.start();  // trying to start a started thread again
                                      contatore++;
                                    } 
           if(!prova2.isAlive()) { prova2.start();// trying to start a started thread 
                                   contatore++;
                                 }
           if(contatore==50) System.out.println(""); 
          }

   }

You should read about the life cycle of threads. If you try to start an already started thread (it can be alive or otherwise) you will get a IllegalThreadStateException. Please read thread documentation

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
0

You can't just start a Thread more than one time, you should create a new instance of that Thread in order to do the task again.

So you should do something like this:

if(!prova.isAlive()) {
    Worker p = new Worker();
    p.start();
    contatore++;
}

Java doc:

It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution.

Read more about Java Threads

Salah
  • 8,567
  • 3
  • 26
  • 43