No, we can’t restart a Thread again. If we try to restart a thread then we will get a runtime exception “IllegalThreadStateException”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class MyThread extends Thread { public void run() { System.out.println("Child thread"); } } class Main { public static void main(String[] args) { MyThread t=new MyThread(); t.start(); System.out.println("Thread started"); t.start(); System.out.println("Restart a thread"); } } |
Output:
1 2 3 4 5 |
Thread started Child thread Exception in thread "main" java.lang.IllegalThreadStateException at java.lang.Thread.start(Thread.java:708) at Main.main(Main.java:22) |