Suspending, Resuming, and Stopping Threads

Sometimes, suspending execution of a thread is useful. For example, a separate thread can be used to display the time of day. If the user doesn’t want a clock, then its thread can be suspended. Whatever the case, suspending a thread is a simple matter. Once suspended, restarting the thread is also a simple matter.
The mechanisms to suspend, stop, and resume threads differ between early versions of Java, such as Java 1.0, and modern versions, beginning with Java 2. Although you should use the modern approach for all new code, you still need to understand how these operations were accomplished for earlier Java environments. For example, you may need to update or maintain older, legacy code. You also need to understand why a change was made. For these reasons, the next section describes the original way that the execution of a thread was controlled, followed by a section that describes the modern approach.
Prior to Java 2, a program used suspend( ), resume( ),stop() which are methods defined by Thread, to pause and restart the execution of a thread. They have the form shown below :
final void suspend( )
final void resume( )
final void stop( )

Once a thread has been stopped, it cannot be restarted using resume( ). Example as follows :

class NewThread3 implements Runnable {
	String name; // name of thread
	Thread t;

	NewThread3(String threadname) {
		name = threadname;
		t = new Thread(this, name);
		System.out.println("New thread: " + t);
		t.start(); // Start the thread

	// This is the entry point for thread.
	public void run() {
		try {
			for (int i = 15; i > 0; i--) {
				System.out.println(name + ": " + i);
		} catch (InterruptedException e) {
			System.out.println(name + " interrupted.");
		System.out.println(name + " exiting.");

public class SuspendResume {
	public static void main(String args[]) {
		NewThread3 ob1 = new NewThread3("One");
		NewThread3 ob2 = new NewThread3("Two");

		try {
			System.out.println("Suspending thread One");
			System.out.println("Resuming thread One");
			System.out.println("Suspending thread Two");
			System.out.println("Resuming thread Two");
		} catch (InterruptedException e) {
			System.out.println("Main thread Interrupted");

		// wait for threads to finish
		try {
			System.out.println("Waiting for threads to finish.");
		} catch (InterruptedException e) {
			System.out.println("Main thread Interrupted");

		System.out.println("Main thread exiting.");


New thread: Thread[One,5,main]
New thread: Thread[Two,5,main]
One: 15
Two: 15
Two: 14
One: 14
One: 13
Two: 13
Two: 12
One: 12
Two: 11
One: 11
Two: 10
Suspending thread One
Two: 9
Two: 8
Two: 7
Two: 6
One: 10
Resuming thread One
Suspending thread Two
One: 9
One: 8
One: 7
One: 6
One: 5
Two: 5
Resuming thread Two
Waiting for threads to finish.
Two: 4
One: 4
One: 3
Two: 3
One: 2
Two: 2
Two: 1
One: 1
Two exiting.
One exiting.
Main thread exiting.

You may also like...