Multiple Threads are repeatedly started in Java.
During the interview, I was often asked questions about multithreading:
During the test today, the following code throws an exception: java. lang. IllegalThreadStateException
public static void main(String[] args)throws Exception{ Test_Thread temp = new Test_Thread(); Test_Thread temp1 = new Test_Thread(); Thread t = new Thread(temp); Thread t1 = new Thread(temp1); for(int i=0;i<50;i++){ if(i%2 == 0){ t.start(); } else { t1.start(); } } }
Change to the following to run smoothly.
public static void main(String[] args)throws Exception{ Test_Thread temp = new Test_Thread(); Test_Thread temp1 = new Test_Thread(); // Thread t = new Thread(temp); // Thread t1 = new Thread(temp1); for(int i=0;i<50;i++){ if(i%2 == 0){ new Thread(temp).start(); } else { new Thread(temp1).start(); } } }
Conclusion: The thread cannot call start () repeatedly, that is, a single thread cannot start again.