Suspending and Resuming Threads

Suspending and Resuming  is used to temporarily suspend the execution of the thread. When called, the thread will be suspended and will not continue executing until it is resumed.

In Java, the suspend() and resume() methods were deprecated in Java 2 and have been discouraged for use due to potential thread safety issues and risks of thread deadlock. It is generally recommended to use other thread coordination mechanisms, such as the wait() and notify() methods or higher-level concurrency utilities provided by the java.util.concurrent package.

However, for reference purposes, here is the syntax for the deprecated suspend() and resume() methods:

suspend() method:

public void suspend()
This method pauses the execution of the thread it is called on. The thread remains in a suspended state until the resume() method is called.
Code language: JavaScript (javascript)

 resume() method:

public void resume()
This method resumes the execution of a thread that has been suspended using the suspend() method.Code language: JavaScript (javascript)

 Program Implementation

In this example, we’ll simulate a classroom scenario with students represented as threads. We’ll use the deprecated suspend() and resume() methods to pause and resume the students’ activities.

In this example, the StudentThread class represents a student thread. The run() method includes a check in a while loop to see if the student is suspended. If suspended, the thread yields, allowing other threads to execute. Once resumed, the loop continues normally.

class Student extends Thread {
    private String name;
    
    // Constructor to set the student's name
    public Student(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        try {
            // Simulating the student doing some work
            System.out.println(name + " is studying...");
            Thread.sleep(2000);
            
            // Suspend the student (the teacher wants them to stop temporarily)
            System.out.println(name + " has been suspended by the teacher.");
            Thread.sleep(5000);  // The student is suspended for 5 seconds
            
            // Resume the student after suspension
            System.out.println(name + " is now allowed to resume studying.");
        } catch (InterruptedException e) {
            System.out.println(name + " was interrupted while studying.");
        }
    }
}

public class ClassroomSimulation {
    public static void main(String[] args) {
        // Creating student threads with Indian names
        Student student1 = new Student("Rajesh");
        Student student2 = new Student("Ramesh");
        Student student3 = new Student("Raju");
        Student student4 = new Student("Rakesh");
        
        // Starting all the students' threads
        student1.start();
        student2.start();
        student3.start();
        student4.start();

        try {
            // Simulating the teacher suspending and resuming students
            Thread.sleep(3000);  // Let students study for a bit
            student1.suspend();  
            System.out.println("Teacher suspended Rajesh.");
            
            Thread.sleep(2000);  // Teacher waits for a moment
            
            student1.resume();  
            System.out.println("Teacher resumed Rajesh.");
            
            Thread.sleep(5000);  // Let students study a bit more
            
            student2.suspend();  
            System.out.println("Teacher suspended Priya.");
            
            Thread.sleep(3000);  // Teacher waits again
            
            student2.resume();  
            System.out.println("Teacher resumed Priya.");
        } catch (InterruptedException e) {
            System.out.println("The teacher was interrupted.");
        }
    }
}

/*
Rajesh is studying...
Ramesh is studying...
Raju is studying...
Rakesh is studying...
Rajesh has been suspended by the teacher.
Teacher suspended Rajesh.
Ramesh is studying...
Rakesh is studying...
Teacher resumed Rajesh.
Rajesh is now allowed to resume studying.
Ramesh has been suspended by the teacher.
Teacher suspended Ramesh.
Raju is studying...
Teacher resumed Ramesh.
Ramesh is now allowed to resume studying.
*/

The suspend() and resume() methods were originally introduced in Java to provide a way to pause and resume threads, offering direct control over a thread’s execution. However, these methods are now deprecated because they can cause serious problems such as deadlocks,if a thread is suspended while holding a lock, other threads waiting for that lock may be permanently blocked. As a result, their use is discouraged in favor of safer alternatives like using flags (boolean variables) in combination with wait() and notify(), or higher-level concurrency APIs like ExecutorService. While they serve well for simple educational demonstrations, they are not recommended for use in real-world applications due to the risks they introduce.

Scroll to Top