Start a thread twice

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Can we start a thread twice?

No. After starting a thread, it can never be started again. If you do so, an IllegalThreadStateException is thrown. In such case, thread will run once but for second time, it will throw exception.

Let's understand it by the example given below:

  1. public class TestThreadTwice1 extends Thread{  
  2.  public void run(){  
  3.    System.out.println("running...");  
  4.  }  
  5.  public static void main(String args[]){  
  6.   TestThreadTwice1 t1=new TestThreadTwice1();  
  7.   t1.start();  
  8.   t1.start();  
  9.  }  
  10. }  

Test it Now

       running
       Exception in thread "main" java.lang.IllegalThreadStateException