Java-Stopping Thread

Stopping Thread

Whenever we want to stop a thread from running further, we may do so by calling its stop( ) method. This method causes the thread to move to the dead state. A thread will also move to the dead state automatically when it reaches the end of its method. The stop( ) method may be used when the premature death of a thread is desired.

Program

sds u

Program Source

class NewThread extends Thread {
    
    public void run()
    {
        for(int i=1;i<=6;i++)
        {
            try {
                Thread.sleep(1000);
            }catch(InterruptedException ex)
            {
                System.out.println("ThreadOne Interrupted");
            }
            System.out.println("ThreadOne  : "+i);
        }
        System.out.println("Exiting ThreadOne");
    }
}

public class Javaapp {
    
    public static void main(String[] args) {
        
        NewThread th1 = new NewThread();
        th1.start();
        
        for(int i=1;i<=6;i++)
        {
            try {
                Thread.sleep(1000);
            }catch(InterruptedException ex)
            {
                System.out.println("ThreadMain Interrupted");
            }
            System.out.println("ThreadMain : "+i);
            if(i==3)
            {
                th1.stop();
            }
        }
        System.out.println("Exiting ThreadMain");
    }
}

Leave a Comment