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.

Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus

 

Java tutorial for beginners Copyright © 2012