Home  >  Article  >  Java  >  Java thread interruption interrupt and stop

Java thread interruption interrupt and stop

王林
王林forward
2019-11-26 14:53:402604browse

Java thread interruption interrupt and stop

interrupt method

When we call the thread's sleep method or join method, we can put some threads in a waiting state and call the current The thread's interrupt() method can interrupt the blocking state. The interrupt method will not let the thread end.

public void interrupt();// 中断线程的阻塞状态

This method will throw an InterruptedException exception.

Recommended java related learning videos: java course

Case: Demonstrates the waiting state of interrupt sleep

Thread class:

 package com.pbteach.thread;
 public class MyThread extends Thread {
    
        @Override
        public void run() {
    
            for(int x = 0 ; x < 100 ; x++) {
                System.out.println(Thread.currentThread().getName() + "----" + x );
                if(x == 10) {
                    try {
                        TimeUnit.SECONDS.sleep(10);     // 线程休眠以后,该线程就处于阻塞状态
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

Test class:

package com.pbteach.thread;
public class ThreadDemo {

    public static void main(String[] args) {

        // 创建MyThread线程对象
        MyThread t1 = new MyThread();
        t1.setName("pbteach-01");

        // 启动线程
        t1.start();

        try {
            // 主线程休眠2秒
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 中断t1线程的休眠
        t1.interrupt();

    }

}

Output result:

...
pbteach-01----10
java.lang.InterruptedException: sleep interrupted
	at java.base/java.lang.Thread.sleep(Native Method)
	at java.base/java.lang.Thread.sleep(Thread.java:339)
	at java.base/java.util.concurrent.TimeUnit.sleep(TimeUnit.java:446)
	at com.pbteach.javase.thread.api.demo14.MyThread.run(MyThread.java:14)
pbteach-01----11
...

Through the output result of the console, we can see that the interrupted method does not end the current thread, but changes the blocking state of the thread Interrupted, after interrupting the blocking state, thread pbteach-01 continues to execute.

stop method

Call the stop method of the thread to terminate the execution of the thread.

public final void stop()  // 终止线程的执行

Thread class

package com.pbteach.thread;
    public class MyThread extends Thread {
    
        @Override
        public void run() {
    
            for(int x = 0 ; x < 100 ; x++) {
                System.out.println(Thread.currentThread().getName() + "----" + x );
                if(x == 10) {
                    try {
                        TimeUnit.SECONDS.sleep(10);     // 线程休眠以后,该线程就处于阻塞状态
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

Test class:

package com.pbteach.thread;
    public class ThreadDemo1 {
    
        public static void main(String[] args) {
    
            // 创建MyThread线程对象
            MyThread t1 = new MyThread();
            t1.setName("pbteach-01");
    
            // 启动线程
            t1.start();
    
            try {
                // 主线程休眠2秒
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    
            // 终止线程t1的执行
            t1.stop();
    
        }
    
    }

Output result:

...
pbteach-01----9
pbteach-01----10

There is no abnormal output in the console , the program ends, and the "pbteach-01" thread does not continue execution.

Recommended related article tutorials: Introduction to java programming

The above is the detailed content of Java thread interruption interrupt and stop. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete