线程八大基础核心三(停止线程)

1.引子

在java多线程并发编程中,有八大基础核心。考考你:
看看都有哪八大基础核心呢?它们分别是:
1.创建线程的方式
2.线程启动
3.线程停止
4.线程生命周期
5.线程相关的方法
6.线程相关的属性
7.线程异常处理
8.线程安全

今天我们从第三个基础核心开始:停止线程

2.考考你

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

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

3.停止线程案例

3.1.正常停止

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

 1 package com.anan.thread.stopthread;
 2 
 3 /**
 4  * 启动线程,调用run方法执行,当run方法正常执行结束,则线程正常停止
 5  */
 6 public class NormalStopThreadDemo {
 7 
 8     public static void main(String[] args) {
 9         // 创建线程对象
10         Runnable r1 = new MyRunnable();
11         Thread t1 = new Thread(r1);
12 
13         // 启动线程
14         t1.start();
15 
16     }
17 }
18 
19 /**
20  * 实现Runnable接口,创建线程
21  */
22 class MyRunnable implements Runnable{
23 
24     public void run() {
25         System.out.println("线程准备开始执行......");
26         System.out.println("......线程执行中......");
27         System.out.println("线程执行结束......");
28     }
29 }

 

 

3.2.意外停止

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

 1 package com.anan.thread.stopthread;
 2 
 3 /**
 4  * 启动线程,调用run方法执行,当run方法发生异常后,则线程意外停止
 5  */
 6 public class AccidentStopThreadDemo {
 7 
 8     public static void main(String[] args) {
 9         // 创建线程对象
10         Runnable r1 = new MyRunnable2();
11         Thread t1 = new Thread(r1);
12 
13         // 启动线程
14         t1.start();
15 
16     }
17 }
18 
19 /**
20  * 实现Runnable接口,创建线程
21  */
22 class MyRunnable2 implements Runnable{
23 
24     public void run() {
25         System.out.println("线程准备开始执行......");
26         System.out.println("......线程执行中......");
27         // 发生意外
28         int i = 1/0;
29 
30         System.out.println("线程执行结束......");
31     }
32 }

 

 

3.3.中断停止

3.3.1.中断信号停止

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

 1 package com.anan.thread.stopthread;
 2 
 3 import java.util.concurrent.TimeUnit;
 4 
 5 /**
 6  * 启动线程,调用run方法执行,通过线程对象调用interrupt中断方法,发送中断信号停止线程
 7  */
 8 public class InterruptSignalStopThread {
 9 
10     public static void main(String[] args) throws InterruptedException {
11         // 创建线程对象
12         Runnable r1 = new MyRunnable3();
13         Thread t1 = new Thread(r1);
14 
15         // 启动线程
16         t1.start();
17         // 主线程休眠1毫秒后,向t1线程发送中断信号
18         TimeUnit.MILLISECONDS.sleep(1);
19         t1.interrupt();
20     }
21 }
22 
23 /**
24  * 实现Runnable接口,创建线程
25  */
26 class MyRunnable3 implements Runnable{
27 
28     public void run() {
29         System.out.println("线程准备开始执行......");
30         // 循环执行任务,直到收到中断信号为止
31         // isInterrupted()方法,返回线程是否被中断
32         int i = 0;
33         while ( ! Thread.currentThread().isInterrupted()){
34             i++;
35             System.out.println("......线程第【" + i +"】次执行中......");
36         }
37         System.out.println("收到中断信号,线程在第【" + i + "】次执行结束......");
38     }
39 }

 

 

3.3.2.响应中断停止

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

 1 package com.anan.thread.stopthread;
 2 
 3 import java.util.concurrent.TimeUnit;
 4 
 5 /**
 6  * 启动线程,调用run方法执行,在run方法中有可响应中断的操作,
 7  * 比如:sleep、wait等。通过线程对象调用interrupt中断方法,发送中断信号响应中断停止
 8  */
 9 public class ResponseInterruptSignalStopThread {
10 
11     public static void main(String[] args) throws InterruptedException {
12         // 创建线程对象
13         Runnable r1 = new MyRunnable4();
14         Thread t1 = new Thread(r1);
15 
16         // 启动线程
17         t1.start();
18         // 主线程休眠1毫秒后,向t1线程发送中断信号
19         TimeUnit.MILLISECONDS.sleep(1);
20         t1.interrupt();
21     }
22 }
23 
24 /**
25  * 实现Runnable接口,创建线程
26  */
27 class MyRunnable4 implements Runnable{
28 
29     public void run() {
30         System.out.println("线程准备开始执行......");
31         // 循环执行任务,直到收到中断信号,通过sleep方法响应中断
32         int i = 0;
33         try{
34 
35             while ( i <= 100000000){
36                 i++;
37                 System.out.println("......线程第【" + i +"】次执行中......");
38 
39                 // 休眠10毫秒
40                 TimeUnit.MILLISECONDS.sleep(10);
41             }
42         }catch (InterruptedException e){
43             System.out.println("收到中断信号,sleep方法响应中断,线程在第【" + i + "】次执行结束......");
44             e.printStackTrace();
45         }
46 
47     }
48 }

 

 

4.讨论分享

#考考你答案:
1.在java编程语言中,线程停止的情况有哪些?
1.1.在java编程语言中,线程停止的情况有:正常停止、意外停止、中断停止
1.2.在实际项目开发中,我们追求正常停止、中断停止。避免意外停止

2.在java编程语言中,如何人为优雅的停止线程?
2.1.在java编程语言中,通过调用线程对象的interrupt()方法,发送中断信号的方式,人为优雅的停止线程
2.2.所谓人为优雅,即指与线程协商的方式停止线程,而不是强制停止线程,最终的停止权交由线程本身来控制

3.在java编程语言中,为什么说坚决不要调用stop方法停止线程?
3.1.因为如果使用stop方法停止线程,它是一种暴力手段,即强制停止线程,有可能会导致线程任务执行的不完整,不安全。且在较新版本的jdk中,已经设置为过期的方法,不再推荐使用。

#参考截图,来自jdk1.8

posted @ 2020-02-05 09:55  小杨【0和1】  阅读(352)  评论(2编辑  收藏  举报