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.
public final void stop()
Another syntax:
public final void stop(Throwable obj)
obj
: An instance of Throwable class.
It does not return any value.
SecurityException: This exception comes when the current thread cannot change this.
thread.
In the code snippet below, We are using the deprecated method stop()
to stop the thread2
execution:
// Load Thread classimport java.lang.Thread;// Main classpublic 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 millisecondssleep(2000);} catch(InterruptedException e){System.out.println(e);}i++;}System.out.println();}public static void main(String args[]) {// creating three threadsDemoThread thread1=new DemoThread();DemoThread thread2=new DemoThread();DemoThread thread3=new DemoThread();DemoThread thread4=new DemoThread();DemoThread thread5=new DemoThread();// call run() methodthread1.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 thread2thread2.stop();System.out.println("thread2 is stopped");}}