Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

How to Temporarily Suspend a Thread in Java?

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Threads in java are the lightweight sub-processes that can be created by the user. It is used to perform complicated tasks in the background without disturbing the main program. In order to suspend the thread temporarily by making it go through from running state to waiting for the state. The concept used in achieving the goal is the suspend() function.

Method: How the threads can be suspended temporarily

  • Creating ‘MyThread Class which is extending thejava.lang.Thread class. It has a run() function which contains some code to perform.
  • In the main function, a ‘MyThread Class object is created ‘thread’ which is named as ‘GFG’ by using the setName() function.
  • Start the thread to perform its tasks by calling the start() function and it starts executing the code written in the run() function.

Sometimes there is an urgency to suspend these threads for some reasons. So here the program shows how the thread is suspended temporarily by using the suspend() function. The thread will go from running state to waiting state. This function makes a thread temporarily cease execution. The thread will remain in waiting for the state until we resume it. So, in this program the thread is kept suspended till the sleep time i.e. 5 seconds (taken in this program) is over, and then we resume it by using the resume() function.

Syntax: In order to get the ID number of the current thread is already created

Thread.currentThread( ).getId( ) ;

Methods Used:  

  1. start(): This is a method to start the functioning of a thread.
  2. setName(): This is a method used to set the name of a thread that is created.
  3. sleep(time): This is a method used to sleep the thread for some milliseconds time.
  4. suspend(): This is a method used to suspend the thread. The thread will remain suspended and won’t perform its tasks until it is resumed.
  5. resume(): This is a method used to resume the suspended thread.



// Java program to suspend a thread temporarily
// Importing all classes from
// java.util package
import java.util.*;
// Class- MyThread
class MyThread extends Thread {
    // Remember : Method can be executed multiple times
    public void run()
        // Try block to check if any exception occurs
        try {
            // Print and display the running thread
            // using currentThread() method
                "Thread " + Thread.currentThread().getId()
                + " is running");
        // Catch block to handle the exceptions
        catch (Exception e) {
            // Message to be printed if
            // the exception is encountered
            System.out.println("Exception is caught");
// Class-Main
public class GFG {
    // Main Driver Method
    public static void main(String[] args) throws Exception
        // Creating a thread
        MyThread thread = new MyThread();
        // Naming thread as "GFG"
        // Start the functioning of a thread
        // Sleeping thread for specific amount of time
        // Thread GFG suspended temporarily
        // Display message
            "Thread going to sleep for 5 seconds");
        // Sleeping thread for specific amount of time
        // Display message
        System.out.println("Thread Resumed");
        // Thread GFG resumed

Output : 

Thread 13 is running
Thread Suspended
Thread going to sleep for 5 seconds
Thread Resumed


My Personal Notes arrow_drop_up
Last Updated : 13 Jul, 2021
Like Article
Save Article
Similar Reads
Related Tutorials