Can we start a thread twice

Can we start a thread twice

No. After starting a thread, it can never be started again. If you does 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
java for beginners
java for beginners
java for beginners
CodSir //course // books
...

WikiSir

Can we start a thread twice No. After starting a thread, it can never be started again. If you does 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: 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();  } […]

Tags:

....
...