Follow on Facebook

Like and Share on Facebook

Saturday, January 31, 2015

Can we start a thread twice?


Can we start a thread twice?

No. After staring a thread, it can never be started again. If you does so, an IllegalThreadStateException is thrown. For Example:

public class TestThreadTwice1 extends Thread{  
 public void run(){  
   System.out.println("running...");  
 }  
 public static void main(String args[]){  
  TestThreadTwice1 t1=new TestThreadTwice1();  
  t1.start();  
  t1.start();  
 }  
}  

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

No comments:

Post a Comment