Home > Java > javaTutorial > body text

Java thread interruption interrupt and stop

王林
Release: 2019-11-26 14:53:40
forward
2696 people have browsed it

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();// 中断线程的阻塞状态
Copy after login

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();
                    }
                }
            }
        }
    }
Copy after login

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();

    }

}
Copy after login

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
...
Copy after login

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()  // 终止线程的执行
Copy after login

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();
                    }
                }
            }
        }
    }
Copy after login

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();
    
        }
    
    }
Copy after login

Output result:

...
pbteach-01----9
pbteach-01----10
Copy after login

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template