Every thread may perform different tasks. Sometimes, it becomes necessay to suspend the execution of a thread for a period of time. This can be done by using suspend() method of the class. We can restart the thread by using resume() method.stop() is used to stop the thread, it cannot be restarted again.
public static void main(String aa)
Thread thrd1=new Thread();
Thread thrd2=new Thread();
System.out.println("SUSPENDING FIRST THREAD");
System.out.println("RESUMING FIRST THREAD");
System.out.println("SUSPENDING SECOND THREAD");
System.out.println("RESUMING SECOND THREAD");
System.out.println("INTERUPTED MAIN THREAD");
System.out.println("INTERRUPTED MAIN THREAD");
System.out.println("EXITING MAIN THREAD");
Inside the main() a new instance of thread is created.
The inside the try block, the first thread is put to sleep for 1000 miliseconds, after which the System.out.println statement prints the statement “SUSPENDING FIRST THREAD”.
Then, the current execution of the first thread is suspended using the method suspend() method.
The thread is again put to sleep, after which the System.out.println statement prints the statement “RESUMING FIRST THREAD”.
Now the first thread that was suspended is resumed with the method resume().
Then, the System. out. println statement prints the statement “SUSPENDING SECOND THREAD”.
Now the current execution of the second thread is suspended using method suspend().
The second thread is put to sleep, after which the System.out.println statement prints the statement ”RESUMING SECOND THREAD”
The second thread that was suspended is resumed using resume().
The catch method prints statement “MAIN THREAD INTERRUPTED”(error) if it encounters an error.
Then, the two threads are joined using join() before which a System.out.println prints the statement “Applying joins”(joins two threads).
If an error occurs while joining the threads, the catch throws an exception with the statement“INTERRUPTED MAIN THREAD”.
Finally the System.out.println statement prints a message “EXITING MAIN THREAD”