How to use Thread.stop() - Theoretically under what conditions can Thread.stop() be used

Viewed 59

First, I understand the problem with Thread.stop() and the reason why it was deprecated, with this and with this. Second, while Thread.stop() has been deprecated since 1.1, it has not been removed.

Theoretically then, while one should never use deprecated API, Thread.stop() is still available for use as of JDK 17.

My question is not about whether to use Thread.stop() or not to use Thread.stop(). My question is, if Thread.stop() needs to be used under what conditions can it be used (if there are any such conditions at all).

For example; in the above links, the primary reason stated for not using Thread.stop() is it may leave objects, that the thread might have locked in inconsistent state. Now, in situations a thread does not lock any object, does not use any System, Runtime, IO, Socket etc. etc. classes, does not use any util classes, does not call synchronized methods etc. only performs calculation in memory, would it be possible to call Thread.stop() on such thread safely?

EDIT#1: Adding a diagram in context of the comments under discussion

sandbox execution of MVEL2 expression

EDIT#2: Adding code to demonstrate Thread.stop()

public class ThreadKillExample {

private Thread secThread;

public void execute() throws Exception{
    
    secThread=new Thread(new SecondaryThread());
    /*
     * Does not make any difference in this case
     */
    //secThread.setDaemon(true);
    secThread.start();
    
    //Check the status of the thread
    System.out.println("Secondary "
            + "Thread state = "+secThread.isAlive());
    
    //Wait for a second
    Thread.sleep(1*1000);
    
    //Kill secondary thread
    secThread.stop();
    Thread.sleep(200);
    System.out.println("Secondary Thread state "
    + "after Thread.stop() = "+secThread.isAlive());
    
    /*
     * Does not make any difference in this case, 
     * the JVM exits clean
     */
    //System.exit(0);
    
}//execute closing

public static final class SecondaryThread 
                            implements Runnable{

    @Override
    public void run() {
        
        AtomicInteger aInt=new AtomicInteger();
        
        for (int i = 0; i < Integer.MAX_VALUE; i++) 
        {aInt.getAndAdd(1);}
        
    }//run closing

}//inner-class closing

public static void main(String[] args) throws Exception{
    
    ThreadKillExample obj=new ThreadKillExample();
    obj.execute();

}//main closing

}//class closing

The trace after execution:

Secondary Thread state = true
Secondary Thread state after Thread.stop() = false
0 Answers
Related