What happens if we start a thread twice is a basic multithreading concepts in java. In fact, we cannot start a thread twice on the same instance of a thread. It is illegal to call start() method of a thread class twice on same instance of a thread. On first call of start() method thread will start working properly, but, on the second call of start() method IllegalThreadStateException will be thrown.
Here is the java code example and output:
public class Sample extends Thread {
public static void main(String[] args) {
Thread t = new Thread( new Sample());
t.start(); // will create a new thread and call run() method internally.
t.start();
}
@Override
public void run() {
System.out.println("This method is executed by : " + Thread.currentThread().getName()+"\n");
}
}
Output:
Exception in thread “main” This method is executed by : Thread-1
java.lang.IllegalThreadStateException
at java.lang.Thread.start(Unknown Source)
at Sample.main(Sample.java:8)