Can we start a thread twice in java

No, Once you have started a thread, it can not be started again. If you try to start thread again , it will throw IllegalThreadStateException

Lets understand with the help of example:

package org.arpit.java2blog;

class FirstThread extends Thread {

 public void run() {
  System.out.println("Thread is running");
 }

}

public class StartThreadAgainMain {

 public static void main(String[] args) {
  FirstThread ft = new FirstThread();
  ft.start();
  ft.start();

 }

}

When you run above program , you will get below output:

Thread is runningException in thread “main”
java.lang.IllegalThreadStateException
at java.lang.Thread.start(Thread.java:705)
at org.arpit.java2blog.StartThreadAgainMain.main(StartThreadAgainMain.java:16)

As you can see when we started thread twice, it threw IllegalThreadStateException.

If you try to start thread again , it will throw IllegalThreadStateException

You can go through core java interview questions for beginners and experienced for more such questions.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.