How to Temporarily Suspend a Thread in Java?


Threads are an important aspect of Java programs. They are also known as lightweight processes. Every program in Java has at least a main thread. They play a very important role to run multiple tasks at the same time. They run in the background and do not affect the execution of the main program. The use of multiple threads simultaneously is called multithreading.

States of a Thread

A thread can exist in either of the following states. It has a complete lifecycle from its creation to destruction. The thread lifecycle states are-

  • New- It is the first stage when the thread is created. It is not yet executed.

  • Runnable- It is the state when is thread is running or is ready to run.

  • Blocked/waiting- It is the state when the thread is waiting for resources and is inactive.

  • Suspended- It is the state when the thread is temporarily inactive.

  • Terminated- It is the state when the thread has stopped its execution.

Temporarily Suspend a Thread in Java

A thread has to be in the suspended state if we want to suspend it. In this state, the thread is temporarily suspended, i.e., it is inactive. The suspend() method is used to suspend the thread temporarily. It will get suspended until resumed in the program. We can resume the suspended thread by the resume() method.

Syntax

public final void suspend()

The suspend() method is a final method that means it cannot be overridden by the subclasses. Also, as the return data type is void, it does not return any value to the user. The method is public.

The suspend() method stops the thread temporarily until the resume() method is called on it.

Example

Following is an example to use the suspend() method to temporarily suspend a thread in Java.

import java.io.*;
public class Main extends Thread {
   public void run()
   {
      try {
      //print the current thread
         System.out.println("Current thread is thread number " + Thread.currentThread().getName());
      }
      catch (Exception e) {
         System.out.println(e);
      }
   }

   public static void main(String[] args) throws Exception
   {

      //1 thread created
      Main t1=new Main();

      //running the threads
      t1.start();

      //putting it to sleep for 3ms
      t1.sleep(300);

      //now suspend the thread 
      t1.suspend();
      System.out.println("Thread is suspended");

      //resume the thread
      t1.resume();
      
      System.out.println("Thread is Resumed");
   }
}

Output

Following is the output of the above code

Current thread is thread number Thread-0
Thread is suspended
Thread is Resumed

Explanation

In the above code, we have created a thread. We run that thread and then put it to sleep for 3 milliseconds. Then to temporarily suspend a thread, we have used the suspend() method. It temporarily stops the execution of that thread. Later, the thread is resumed by the resume() method.

Hence, we can conclude that the suspend() method can be used to temporarily suspend a thread in Java. Although this method is deprecated, it is advisable to know the consequences of deadlock and use this.

Updated on: 24-Jul-2023

308 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements