Liao Xuefeng concept Java11 multithreaded programming -1 -5 interrupt thread thread

1. interrupt thread:

  • If a thread needs to perform a task for a long time, you may need to interrupt thread. Scene: a 100M download files from the network, the user interrupts the download tasks during the download process.
  • Interrupt thread other threads to the thread that sends a signal to end the execution run after the thread receives a signal () method

1.1 interrupt thread

Need to detect isInterrupted () mark, other thread interrupts the thread method by calling interrupt ()

class HelloThread extends Thread{
    public void run(){
        while(!isInterrupted()){
            System.out.println("Hello");
        }
    }
}
class Main{
    public static void main(String[] args) throws Exception{
        Thread t = new HelloThread();
        t.start();
        Thread.sleep(1000);
        t.interrupt();
    }
}

1.2 interrupt-pending thread

If a thread is in a wait state, the thread will capture InterruptedException. Capture InterruptedException to illustrate the other thread on which to invoke the interrupt () method under, usually an immediate end to the thread should run.

class HelloThread extends Thread{
    public void run(){
        while(!isInterrupted()){
            System.out.println("Hello");
            try{
                Thread.sleep(100);
            }catch (InterruptedException e){
                e.printStackTrace();
                return;
            }
        }
    }
}

1.3 Setting running flag interrupt thread

class HelloThread extends Thread{
    public volatile boolean running = true;
    public void run(){
        while(running){
            System.out.println("Hello"); 
        }
    }
}
class Main{
    public static void main(String[] args) throws Exception{
        HelloThread t = new HelloThread();
        t.start();
        Thread.sleep(1000);
        t.running = false;
    }
}

Variables shared between threads need to use the volatile keyword tags, to ensure that the thread can read the updated value of the variable to

the Java virtual machine, the values of variables stored in the main memory, but when threads access a variable, will first get a copy of the and save their work memory. If the value of the thread to modify variables, virtual opportunity at some point to return the modified value is written to main memory, but this time is uncertain. This causes the thread to update a variable, the variable read or updated by another thread before.

As a main memory value is true, the thread 1 is rewritten to a value of false, the value of the virtual machine 1 back thread after the writing to the main memory is uncertain time, when executing thread 2, the thread 2 reads the value may still be true, and not false updated thread 1.

The purpose of the volatile keyword tells the virtual machine:

  • Every time variable access, always get the latest value of main memory
  • After each modify variables immediately written back to main memory

The volatile keyword visibility problem is solved:

  • When a thread modifies a value shared variables, other threads can see the value immediately after the modification

Example 2

2.1 interrupt () Interrupts this thread

class InterrptThread extends Thread{
    public void run(){
        while(!isInterrupted()){
            System.out.println("Hello");
            try{
                Thread.sleep(100);
            }catch (InterruptedException ex){
                System.out.println("Interrupted!");
                break;
            }
        }
        System.out.println("Thread end");
    }
}
public class InterruptTest {
    public static void main(String[] args) throws InterruptedException{
        Thread t = new InterrptThread();
        t.start();
        Thread.sleep(1000);
        t.interrupt();
        System.out.println("main end");
    }
}

2.2 Use flag interrupt thread

class InterrptThread extends Thread{
    volatile boolean running = true;
    public void run(){
        while(running){
            System.out.println("Hello");
            try{
                Thread.sleep(100);
            }catch (InterruptedException ex){
                System.out.println("Interrupted!");
                break;
            }
        }
        System.out.println("Thread end");
    }
}
public class InterruptTest {
    public static void main(String[] args) throws InterruptedException{
        InterrptThread t = new InterrptThread();
        t.start();
        Thread.sleep(1000);
        t.running = false;
        System.out.println("main end");
    }
}

3 Summary:

  • Call interrupt () method can interrupt a thread
  • By detecting isInterrupted () flag to get whether the current thread is interrupted
  • 如果线程处于等待状态,该线程会捕获InterruptedException
  • isInterrupted()为true或者捕获了InterruptedException都应该立刻结束
  • 通过标志位判断需要正确使用volatile关键字
  • volatile关键字解决了共享变量在线程件的可见性问题

Guess you like

Origin www.cnblogs.com/csj2018/p/10994626.html