The life cycle of java Concurrent Programming learning thread - interrupt(7)

resume,suspend,stop

  1. resume and suspend are used together, and the suspend method is easy to cause deadlock.
  2. The stop method does not guarantee the normal resource release of the thread

interrupt

  1. interrupt() method: mark the thread to stop, set the thread's interrupt state to true, and do not force the thread to be interrupted immediately. Whether the thread is interrupted is determined by the thread itself.
  2. isInterrupted() method: determines whether the current thread is interrupted, and does not clear the interrupt flag.
  3. interrupted() method: judge whether the current thread is interrupted and clear the interrupt flag.

If there is an exception, the interrupt status is set to false.

Example

Example 1

public class InterruptThread extends Thread {
    @Override
    public void run() {
        while (true) {
        }
    }

    public static void main(String[] args) throws InterruptedException {
        InterruptThread thread = new InterruptThread();
        thread.start();
        System.out.println(thread.getState());
        sleep(1000);
        thread.interrupt();
        System.out.println(thread.getState());
        System.out.println(thread.isInterrupted());
    }
}

The operation results are as follows

It can be seen that although the interrupt state is true, the program is still running, so interrupt does not force thread interruption.

Example 2

public class InterruptThread2 extends Thread {
    @Override
    public void run() {
        while (!isInterrupted()) {
        }
        System.out.println("Interrupted");
    }

    public static void main(String[] args) throws InterruptedException {
        InterruptThread2 thread = new InterruptThread2();
        thread.start();
        System.out.println(thread.getState());
        sleep(1000);
        thread.interrupt();
        System.out.println(thread.getState());
        System.out.println(thread.isInterrupted());
    }
}

The operation results are as follows:

The difference with example 1 is that by judging the interrupt state, we can process our own business logic. Such a design brings great benefit and flexibility to the program.

Example 3

public class InterruptWait extends Thread {
    @Override
    public void run() {
        waitFun();
    }

    public synchronized void waitFun(){
        try {
            wait();
        } catch (InterruptedException e) {
            System.out.println("Excuse me for waiting");
        }

    }

    public static void main(String[] args) throws InterruptedException {
        InterruptWait thread = new InterruptWait();
        thread.start();
        System.out.println(thread.getState());
        sleep(1000);
        thread.interrupt();
        sleep(1000);
        System.out.println(thread.getState());
        System.out.println(thread.isInterrupted());
        sleep(1000);
        System.out.println(thread.getState());
    }
}

The operation results are as follows:

Interrupt wait method. Note that the interrupt status changes to false after an exception is thrown.

Example 4

public class InterruptWait extends Thread {
    @Override
    public void run() {
        waitFun();
    }

    public synchronized void waitFun(){
        try {
            wait();
        } catch (InterruptedException e) {
            System.out.println("Excuse me for waiting");
        }

    }

    public static void main(String[] args) throws InterruptedException {
        InterruptWait thread = new InterruptWait();
        thread.start();
        System.out.println(thread.getState());
        sleep(1000);
        thread.interrupt();
        sleep(1000);
        System.out.println(thread.getState());
        System.out.println(thread.isInterrupted());
        sleep(1000);
        System.out.println(thread.getState());
    }
}

The operation results are as follows:

The result is the same as above. After an exception is thrown, the interrupt status becomes false.

Example 5

public class InterruptSync extends Thread {
    @Override
    public void run() {
        syncFun();
    }

    public static synchronized void syncFun() {
        while (true) {
        }
    }

    public static void main(String[] args) throws InterruptedException {
        InterruptSync thread = new InterruptSync();
        InterruptSync thread2 = new InterruptSync();
        thread.start();
        sleep(1000);
        thread2.start();
        sleep(1000);

        System.out.println(thread.getState());
        System.out.println(thread2.getState());

        thread2.interrupt();
        sleep(1000);
        System.out.println(thread2.getState());
        System.out.println(thread2.isInterrupted());

    }
}

The operation results are as follows:

No throwing exception, the result is the same as example 1.

Tags: Java

Posted on Sat, 02 Nov 2019 16:29:27 -0700 by stevesweetland