javatutelearn.com

Easy Learning


Home

Search Javatutelearn.com :



suspend() method of thread

  • suspend() method puts the thread in waiting state. Suspended thread can be resumed using resume() method.
  • suspend() method is used to stop the execution of thread for a while

Syntax

public final void suspend()

Example

 public class  susexamp1  extends  Thread {
    
    public void  run() {
        for(int i=0;i<5;i++){
            System.out.println("Thread name: "  + Thread.currentThread().getName());
        }
    }
  public static void  main(String args[]) {
      susexamp1 t1 = new susexamp1();
      susexamp1 t2 = new susexamp1();
      
      t1.start();
      t2.start();
      
      t2.suspend();
      
      t2.resume();
      
       
  }    
}

Output

Output will vary in each run

Thread name: Thread-1
Thread name: Thread-1
Thread name: Thread-0
Thread name: Thread-1
Thread name: Thread-1
Thread name: Thread-1
Thread name: Thread-0
Thread name: Thread-0
Thread name: Thread-0
Thread name: Thread-0





© Copyright 2016-2024 by javatutelearn.com. All Rights Reserved.