What is Thread stop() function in java?

This Thread.stop() method is used to terminate the thread execution. Once thread executed is halted by stop() method, start() function cannot restart the thread execution.

stop() function has been deprecated in the latest versions of java.

Syntax


public final void stop()  

Another syntax:


public final void stop(Throwable obj)

Parameters

obj: An instance of Throwable class.

Return value

It does not return any value.

SecurityException: This exception comes when the current thread cannot change this. thread.

Example

In the code snippet below, We are using the deprecated method stop() to stop the thread2 execution:

// Load Thread class
import java.lang.Thread;
// Main class
public class DemoThread extends Thread implements Runnable {
public void run() {
int i = 0;
while(i != 5){
try {
System.out.println(Thread.currentThread().getName());
// thread to sleep for 2000 milliseconds
sleep(2000);
} catch(InterruptedException e){
System.out.println(e);
}
i++;
}
System.out.println();
}
public static void main(String args[]) {
// creating three threads
DemoThread thread1=new DemoThread();
DemoThread thread2=new DemoThread();
DemoThread thread3=new DemoThread();
DemoThread thread4=new DemoThread();
DemoThread thread5=new DemoThread();
// call run() method
thread1.start();
thread1.setName("thread1");
thread2.start();
thread2.setName("thread2");
thread3.start();
thread3.setName("thread3");
thread4.start();
thread4.setName("thread4");
thread5.start();
thread5.setName("thread5");
// stop thread2
thread2.stop();
System.out.println("thread2 is stopped");
}
}

Free Resources