How to Temporarily Suspend a Thread in Java? Last Updated : 13 Jul, 2021 Comments Improve Suggest changes Like Article Like Report 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 the 'java.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: start(): This is a method to start the functioning of a thread.setName(): This is a method used to set the name of a thread that is created.sleep(time): This is a method used to sleep the thread for some milliseconds time.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.resume(): This is a method used to resume the suspended thread.Example: Java // 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 System.out.println( "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" thread.setName("GFG"); // Start the functioning of a thread thread.start(); // Sleeping thread for specific amount of time Thread.sleep(500); // Thread GFG suspended temporarily thread.suspend(); // Display message System.out.println( "Thread going to sleep for 5 seconds"); // Sleeping thread for specific amount of time Thread.sleep(5000); // Display message System.out.println("Thread Resumed"); // Thread GFG resumed thread.resume(); } } Output : Thread 13 is running Thread Suspended Thread going to sleep for 5 seconds Thread Resumed Comment More infoAdvertise with us Next Article How to Temporarily Suspend a Thread in Java? Y yashgupta2808 Follow Improve Article Tags : Java Java Programs Java-Multithreading Practice Tags : Java Similar Reads How to Temporarily Stop a Thread in Java? The suspend() method of thread class puts the thread from running to waiting state. This method is employed if you would like to prevent the thread execution and begin it again when a particular event occurs. This method allows a thread to temporarily cease execution. The suspended thread is often r 2 min read How to Monitor a Thread's Status in Java? The Java language support thread synchronization through the use of monitors. A monitor is associated with a specific data item and functions as a lock on that data. When a thread holds the monitor for some data item, other threads are locked out and cannot inspect or modify the data. In order to mo 3 min read How to Display all Threads Status in Java? Threads are light-weight processes within a process.. Multithreading in java is a feature that allows concurrent execution of two or more parts of a program to maximize the utilization of CPU. here the approach to retrieve the state of the thread is via getState() method of the Thread class. A java 2 min read How to Use Locks in Multi-Threaded Java Program? A lock may be a more flexible and complicated thread synchronization mechanism than the standard synchronized block. A lock may be a tool for controlling access to a shared resource by multiple threads. Commonly, a lock provides exclusive access to a shared resource: just one thread at a time can ac 6 min read How to make ArrayList Thread-Safe in Java? In Java, Thread is the smallest unit of execution within the program. It represents an independent path of execution that can run concurrently with other threads. When dealing with multi-threaded applications, where multiple threads are accessing and modifying data concurrently, it's crucial to ensu 3 min read How to Solve Deadlock using Threads in Java? If two threads are waiting for each other forever such type of infinite waiting is called deadlock in java. Synchronized keyword is the only reason for deadlock situation hence while using synchronized keyword we have to take special care. There is no resolution technique for deadlock, but several p 6 min read Java Program to Run Multiple Threads Thread is a lightweight process. A process in execution is called a program. A subpart of a program is called a thread. Threads allow a program to operate more efficiently by doing multiple things at the same time performing complicated tasks in the background without interrupting the main program e 2 min read How to Get the Id of a Current Running Thread in Java? The getId() method of Thread class returns the identifier of the invoked thread. The thread ID is a positive long number generated when this thread was created. The thread ID is unique and remains unchanged during its lifetime. When a thread is terminated, this thread ID may be reused. Java allows c 4 min read Java Program to Create a Thread Thread can be referred to as a lightweight process. Thread uses fewer resources to create and exist in the process; thread shares process resources. The main thread of Java is the thread that is started when the program starts. The slave thread is created as a result of the main thread. This is the 4 min read How to Check if a Thread Holds Lock on a Particular Object in Java? Java language is one of the most popular languages for many years. One of the most advantageous features of java programming is Multithreading. Multithreading allows executing a single program into many small parts of the program with the maximum utilization of the CPU. Threads are a lightweight pro 3 min read Like