Java Concurrency - stop(), interrupt()

2024. 10. 26. 14:22JAVA & SPRING/자바

반응형

How to properly stop a Thread


After a thread is created and started, there are some reasons to stop a thread. But it is not easy to stop the thread safely and reliably since the Java language does not have a mechanism to ensure the immediate and correct stop of the thread. However, it provides interrupt, which is a collaborative mechanism.

You can use the interrupt() method to notify a thread that it should interrupt execution, and the interrupted thread has the power of decision, i.e., it can not only decide when to respond to this interrupt and when to stop, but it can also ignore to stop the thread.

 

Although we can force to stop the thread halfway, but it causes unexpected problems. 

 

stop(), interrupt() methods


// Using interrupt() method, we can find that there is no effect since it seems to ignore us completely.
public class StopThread implements Runnable {
    @Override
    public void run() {
        System.out.println("Start moving...");
        for (int i = 0; i < 6; i++) {
            // Simulation of time required to move
            int j = 50000;
            while(j > 0) j--;
            System.out.println(i + "batches have been moved");
        }
        System.out.println("End of moving...");
    }
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new StopThread());
        thread.start();
        // Try to stop it later.
        Thread.sleep(2);
        //thread.stop(); // It is deprecated
        thread.interrupt();
    }
}

Java provides stop() method to force to stop the thread. but it has been deprecated.

As I said, the interrupt() method can ignore to stop the thread. In order to avoid this, the thread recognizes the interrupt flag.
When a specified thread is interrupted, calling Thread.currentThread().isInterrupted() within the thread will return true, and the processing logic after being interrupted can be done based on this.

The interrupt() method interrupt is valid; this is a standard way of handling it.

 

 

While interrupting a certain thread, the thread is sleeping


public class InterruptWithSleep implements Runnable {
    @Override
    public void run() {
        System.out.println("Start moving...");
        for (int i = 0; i < 100; i++) {
            try{
                Thread.sleep(1);
                System.out.println(i + " batches have been moved");
            } catch (Exception e) {
                System.out.println("error: "+ e.getMessage());
                break;
            }
        }
        System.out.println("End of moving...");
    }
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new InterruptWithSleep());
        thread.start();
        // Try to stop it later.
        Thread.sleep(3);
        thread.interrupt();
    }
}

 

If the sleep method is used in thread processing, interrupts during sleep can also be responded to without checking the interrupt flag.

반응형