javatutelearn.com

Easy Learning


Home

Search Javatutelearn.com :



Stop() method of thread

  • stop() method of thread class in Java stops the execution of a running thread.
  • stop() method is deprecated in Java due to thread-safety issues.

Syntax

public final void stop()

Example

 public class  stopexmp  extends  Thread {
   public void  run(){
       for(int i=0;i<5;i++){
           System.out.println("current thread is "+ Thread.currentThread().getName());
       }
   }
    public static void  main(String args[]) {
      
      stopexmp t1 = new stopexmp();
      stopexmp t2 = new stopexmp();
      
      t1.start();
      t2.start();
      
       for(int i=0;i<5;i++){
            System.out.println( "current thread is "+  Thread.currentThread().getName());
       }
      
      t2.stop();
    }
}

Output

current thread is Thread-0
current thread is Thread-0
current thread is main
current thread is Thread-1
current thread is main
current thread is main
current thread is Thread-0
current thread is Thread-0
current thread is main
current thread is Thread-1
current thread is main
current thread is Thread-0





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