并发编程基础三(线程停止)

106 阅读4分钟
#回顾:
	在java编程语言中,线程停止指的是线程的生命周期结束,分为正常执行结束和意外结束。

#考考你:
	1.在java编程语言中,线程停止的情况有哪些?
	2.在java编程语言中,如何人为优雅的停止线程?
	3.在java编程语言中,为什么说坚决不要调用stop方法停止线程?

停止线程案例

正常停止

启动线程,调用run方法执行,当run方法正常执行结束,则线程正常停止。

package com.anan.thread.stopthread;

/**
 * 启动线程,调用run方法执行,当run方法正常执行结束,则线程正常停止
 */
public class NormalStopThreadDemo {

    public static void main(String[] args) {
        // 创建线程对象
        Runnable r1 = new MyRunnable();
        Thread t1 = new Thread(r1);

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

    }
}

/**
 * 实现Runnable接口,创建线程
 */
class MyRunnable implements Runnable{

    public void run() {
        System.out.println("线程准备开始执行......");
        System.out.println("......线程执行中......");
        System.out.println("线程执行结束......");
    }
}

image.png

意外停止

启动线程,调用run方法执行,当run方法发生异常后,则线程意外停止。

package com.anan.thread.stopthread;

/**
 * 启动线程,调用run方法执行,当run方法发生异常后,则线程意外停止
 */
public class AccidentStopThreadDemo {

    public static void main(String[] args) {
        // 创建线程对象
        Runnable r1 = new MyRunnable2();
        Thread t1 = new Thread(r1);

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

    }
}

/**
 * 实现Runnable接口,创建线程
 */
class MyRunnable2 implements Runnable{

    public void run() {
        System.out.println("线程准备开始执行......");
        System.out.println("......线程执行中......");
        // 发生意外
        int i = 1/0;

        System.out.println("线程执行结束......");
    }
}

image.png

中断停止

中断信号停止

启动线程,调用run方法执行,通过线程对象调用interrupt中断方法,发送中断信号停止线程。

package com.anan.thread.stopthread;

import java.util.concurrent.TimeUnit;

/**
 * 启动线程,调用run方法执行,通过线程对象调用interrupt中断方法,发送中断信号停止线程
 */
public class InterruptSignalStopThread {

    public static void main(String[] args) throws InterruptedException {
        // 创建线程对象
        Runnable r1 = new MyRunnable3();
        Thread t1 = new Thread(r1);

        // 启动线程
        t1.start();
        // 主线程休眠1毫秒后,向t1线程发送中断信号
        TimeUnit.MILLISECONDS.sleep(1);
        t1.interrupt();
    }
}

/**
 * 实现Runnable接口,创建线程
 */
class MyRunnable3 implements Runnable{

    public void run() {
        System.out.println("线程准备开始执行......");
        // 循环执行任务,直到收到中断信号为止
        // isInterrupted()方法,返回线程是否被中断
        int i = 0;
        while ( ! Thread.currentThread().isInterrupted()){
            i++;
            System.out.println("......线程第【" + i +"】次执行中......");
        }
        System.out.println("收到中断信号,线程在第【" + i + "】次执行结束......");
    }
}

image.png

响应中断停止

启动线程,调用run方法执行,在run方法中有可响应中断的操作,比如:sleep、wait等。通过线程对象调用interrupt中断方法,发送中断信号响应中断停止。

package com.anan.thread.stopthread;

import java.util.concurrent.TimeUnit;

/**
 * 启动线程,调用run方法执行,在run方法中有可响应中断的操作,
 * 比如:sleep、wait等。通过线程对象调用interrupt中断方法,发送中断信号响应中断停止
 */
public class ResponseInterruptSignalStopThread {

    public static void main(String[] args) throws InterruptedException {
        // 创建线程对象
        Runnable r1 = new MyRunnable4();
        Thread t1 = new Thread(r1);

        // 启动线程
        t1.start();
        // 主线程休眠1毫秒后,向t1线程发送中断信号
        TimeUnit.MILLISECONDS.sleep(1);
        t1.interrupt();
    }
}

/**
 * 实现Runnable接口,创建线程
 */
class MyRunnable4 implements Runnable{

    public void run() {
        System.out.println("线程准备开始执行......");
        // 循环执行任务,直到收到中断信号,通过sleep方法响应中断
        int i = 0;
        try{

            while ( i <= 100000000){
                i++;
                System.out.println("......线程第【" + i +"】次执行中......");

                // 休眠10毫秒
                TimeUnit.MILLISECONDS.sleep(10);
            }
        }catch (InterruptedException e){
            System.out.println("收到中断信号,sleep方法响应中断,线程在第【" + i + "】次执行结束......");
            e.printStackTrace();
        }

    }
}

image.png

结语

#考考你答案:
	1.在java编程语言中,线程停止的情况有哪些?
		1.1.在java编程语言中,线程停止的情况有:正常停止、意外停止、中断停止
		1.2.在实际项目开发中,我们追求正常停止、中断停止。避免意外停止
		
	2.在java编程语言中,如何人为优雅的停止线程?
		2.1.在java编程语言中,通过调用线程对象的interrupt()方法,发送中断信号的方式,人为优雅的停止线程
		2.2.所谓人为优雅,即指与线程协商的方式停止线程,而不是强制停止线程,最终的停止权交由线程本身来控制
		
	3.在java编程语言中,为什么说坚决不要调用stop方法停止线程?
		3.1.因为如果使用stop方法停止线程,它是一种暴力手段,即强制停止线程,有可能会导致线程任务执行的不完整,不安全。且在较新版本的jdk中,已经设置为过期的方法,不再推荐使用。
	
#参考截图,来自jdk1.8

image.png