All for Joomla All for Webmasters

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. }

Output:

 running
 Exception in thread "main" java.lang.IllegalThreadStateException
PinIt
submit to reddit

Leave a Reply

Top