Using Suspend and resume method in threads in java


Contents of page :
  • What happens when we call suspend() method?
  • Reason behind these methods being deprecated?
  • Let’s create a program to understand suspend() and resume() in better manner >
  • As suspend() and resume() methods are deprecated and deadlock prone, what methods can be used alternatively?
  • How can improper use of suspend() and resume() methods lead us to Deadlock?

What happens when we call suspend() method?
Suspend() method puts thread from running to waiting state. And thread can go from waiting to runnable state only when resume() method is called on thread. Suspend method is deprecated method.



Resume() method is only used with suspend() method that’s why it’s also deprecated method.

Reason behind these methods being deprecated?
Suspend() and remove() are deprecated methods because if not used properly they might lead to deadlock.

Let’s create a program to understand suspend() and resume() in better manner >
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class SuspendResume {
   public static void main(String[] args) throws InterruptedException {
  
          final Thread thread1=new Thread("Thread-1"){
                 public void run() {
                       System.out.println(Thread.currentThread().getName()+" has started.");
                       for(int i=0;i<5;i++){
                              try {
                                     Thread.sleep(100);
                              } catch (InterruptedException e) {
                                     e.printStackTrace();
                              }
                             
                              System.out.println("i="+i+" ,ThreadName="+
                                                          Thread.currentThread().getName());
                       }  
                       System.out.println(Thread.currentThread().getName()+" has ENDED.");
                 }
          };
         
          Thread thread2=new Thread("Thread-2"){
                 public void run() {
                       System.out.println(Thread.currentThread().getName()+" has started.");
                       for(int i=0;i<5;i++){
                              try {
                                     Thread.sleep(100);
                              } catch (InterruptedException e) {
                                     e.printStackTrace();
                              }
                             
                              System.out.println("i="+i+" ,ThreadName="+
                                                    Thread.currentThread().getName());
                       }  
                       System.out.println(Thread.currentThread().getName()+" has ENDED.");
                 }
          };
         
          thread1.start();
          Thread.sleep(10);//make main thread sleep for 10 millisec,
                                //This minor delay will ensure that Thread-1 starts before Thread-2.
          thread2.start();
          thread1.suspend();//suspend the thread.
          Thread.sleep(1000);
          thread1.resume();
   }
  
}
/*OUTPUT
Thread-1 has started.
Thread-2 has started.
i=0 ,ThreadName=Thread-2
i=1 ,ThreadName=Thread-2
i=2 ,ThreadName=Thread-2
i=3 ,ThreadName=Thread-2
i=4 ,ThreadName=Thread-2
Thread-2 has ENDED.
i=0 ,ThreadName=Thread-1
i=1 ,ThreadName=Thread-1
i=2 ,ThreadName=Thread-1
i=3 ,ThreadName=Thread-1
i=4 ,ThreadName=Thread-1
Thread-1 has ENDED.
*/

Main thread started Thread-1 and then it went into sleep for 10 millisec to ensure that Thread-1 starts before Thread-2.
Thread-1 enters run() method and it called sleeps(100), than main thread got the chance to execute and it went into running state and started Thread-2, then Thread-1 was suspended and main thread called sleep(1000) and Thread-1 waited for resume() method to be called on it, meanwhile Thread-2 completed its execution. After 1000 millisecs were over main thread waked up and executed next statement i.e. thread1.resume() and finally Thread-1 resumed and completed its execution.


As suspend() and resume() methods are deprecated and deadlock prone, what methods can be used alternatively?
As alternate we must try to use sleep method, using sleep() method won't cause any deadlocks, wait and notify() are less deadlock prone.

How can improper use of suspend() and resume() methods lead us to Deadlock?
If the target thread holds a lock on object when it is suspended, no thread can lock this object until the target thread is resumed. If the thread that would resume the target thread attempts to lock this monitor prior to calling resume, it results in deadlock formation.
These deadlocks are generally called Frozen processes.



RELATED LINKS>



Important Thread methods (salient features, usage with programs)>

Join() method - ensure all threads that started from main must end in order in which they started and also main should end in last. Types of join() method-10 salient features of join

Sleep() method in threads - 10 key features with programs

Yield() method in threads - 8 key features with programs

Wait() and notify() methods- Definition, 8 key features, solving consumer producer problem with & without these methods and consequences of not using wait() and notify() methods.

Daemon threads - 12 salient features of Daemon Thread

2 alternate ways to stop thread, as stop() method is deprecated

Using Suspend and resume method in threads



Differences between important thread methods >

Difference between wait() and sleep() method in threads

Differences and similarities between yield() and sleep() in threads

Difference between notify() and notifyAll() methods, with program

Difference between wait() and wait(long timeout) -What will be Thread states


eEdit
Must read for you :