> 文档中心 > 第14章 多线程(2) InterruptedException与interrupted状态区别+线程的状态

第14章 多线程(2) InterruptedException与interrupted状态区别+线程的状态


前言

!!!昨日内容更正:今天测昨天的中断问题,发现

while(!Thread.currentThread().isInterrupted()) 这一句始终为 false

原因是这一句,当一个类主动抛出 InterruptedException ,会清除 interrupted 状态(不能用 interrupted 状态判别是否中断

所以昨天的内容分两种情况,一种是不抛这个异常的,使用循环结束条件中断

❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤

public class Main {    public static void main(String[] args) throws InterruptedException {Thread t = new Thread(new MyRunnble());t.start();Thread.sleep(1000); t.interrupt();    }     static class MyRunnble implements Runnable { @Override public void run() {     //注意,这里写成while(true)是停不了的!!!,它没有interrupt打断自动跳出     while (!Thread.currentThread().isInterrupted()) {  System.out.println("alive");      } }    } }

❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤

运行结果:

🚗🚓🚕🛺🚙🚌🚐🚎🚑🚒🚚🚛🚜🚘🚔🚖🚍🦽🦼🛹🚲🛴🛵🏍

🚗🚓🚕🛺🚙🚌🚐🚎🚑🚒🚚🚛🚜🚘🚔🚖🚍🦽🦼🛹🚲🛴🛵🏍

抛异常的,用catch+break中断:

❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤

public class Main {    public static void main(String[] args) throws InterruptedException {Thread t = new Thread(new MyRunnble());t.start();Thread.sleep(6000);  t.interrupt();    }      static class MyRunnble implements Runnable { @Override public void run() {     //注意,这里写成while(true)是停不了的!!!,它没有interrupt打断自动跳出     while (!Thread.currentThread().isInterrupted()) {  System.out.println("alive");  try {      Thread.sleep(5000);  } catch (InterruptedException e) {      System.out.println("中断了!");      break;  }} }    }  }

❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤

运行结果:

🚗🚓🚕🛺🚙🚌🚐🚎🚑🚒🚚🚛🚜🚘🚔🚖🚍🦽🦼🛹🚲🛴🛵🏍

🚗🚓🚕🛺🚙🚌🚐🚎🚑🚒🚚🚛🚜🚘🚔🚖🚍🦽🦼🛹🚲🛴🛵🏍

 14.3 线程状态

🍰 New 新生:新创建的线程。人只有经历过新生之后才可以被称之为人,同样的线程也必须经过新生之后,才能成为真正的线程。

🍰 Runnable 可运行 (就绪+运行):人活几岁后,就变得活蹦乱跳了,然而体力有限,走走停停。同样的线程随是能执行的状态,就叫做可运行

🍰 Blocked 被阻塞:上厕所的时候,所有坑位都有人了,必须等别人出来才能上厕所

🍰Waiting 等待:坐车回家,军人优先(应该的)。普通人等待军人坐车完成后,再进行乘车。

🍰Timed waiting 计时等待:等攒到100块,就去买零食,最多等10天。超过10天就不等了。

🍰Terminated 被终止:遇到车祸,人生结束。线程在此状态后就消亡。

 

 14.3.2 新生线程

使用 new 关键字创建线程,线程可以先不执行,而先完成其他准备工作。

 14.3.2  可运行线程

调用 start() 启动线程,状态变为可运行状态

注意,可运行不完全等于正在运行:

可运行可能没有运行,处于就绪状态,取决于操作系统给线程提供的运行时间;

一个线程开始后,可以不用一直运行,可能执行过程稿中被中断,用于更好的使用CPU资源.

 14.3.3 被阻塞线程和等待线程

这两种状态都线程都不活动

🍰 阻塞:线程获得锁时,被其他线程持有(被动

🍰 等待:到达某个条件时,线程把资源让给其他线程,直到达到某个条件时才继续执行(主动

🍰 计时等待:到达某个条件或者超期,则继续执行。(主动,自动停止

 14.3.4 被终止的线程

终止的可能性:

🍰 正常结束,run() 方法执行完(自然死亡,寿终正寝)

🍰 因为没有捕获的异常终止,比如检查异常直接抛,运行时异常未捕获(意外身亡

 14.4 线程属性

 14.4.1 线程优先级

🍰 默认优先级 5

大致可以定位到是这一句之后设置的,由于是 native 方法,需要查看 C++ 源码,感兴趣的可以自己去查

🍰 可通过 setPriority(priority) 设置,值域范围是 [1,10]

🍰 实际优先级数目可能是系统相关的,不要过度依赖于优先级的正确性!

 14.4.2 守护线程

守护线程:为其他线程提供服务的线程。如果其他线程都结束,守护线程会自动退出(海王暖男,为守护而生,但是可以有100个老婆,一个老婆活着它就不死)

设置守护:t.setDaemon(true);

 14.4.3 未捕获异常处理器

线程还会出现一些运行时异常,导致线程意外终止,可使用未捕获异常处理器进行统一记录和处理 t.setUncaughtExceptionHandler(handler):

❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤

public class Main {    public static void main(String[] args) throws InterruptedException {Thread t = new Thread(new MyRunnble());t.start();Thread.sleep(6000); t.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {    @Override    public void uncaughtException(Thread t, Throwable e) { e.printStackTrace();    }});t.interrupt();    }     static class MyRunnble implements Runnable { @Override public void run() {     //注意,这里写成while(true)是停不了的!!!,它没有interrupt打断自动跳出     while (!Thread.currentThread().isInterrupted()) {  System.out.println("alive");  System.out.println(1/0);     } }    }}

❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤

运行结果:

🚗🚓🚕🛺🚙🚌🚐🚎🚑🚒🚚🚛🚜🚘🚔🚖🚍🦽🦼🛹🚲🛴🛵🏍

 

🚗🚓🚕🛺🚙🚌🚐🚎🚑🚒🚚🚛🚜🚘🚔🚖🚍🦽🦼🛹🚲🛴🛵🏍

总结

🍰 InterruptedException 异常不会设 interrupted 标志位,这两个中断情况,需要分别进行处理

🍰 线程状态:新生(新建),可运行(start=就绪+运行),阻塞(被动),等待(主动),计时等待(主动,超时自动退出)

🍰 线程可以设置优先级,但因为优先级依赖操作系统,程序正确性不应依赖于优先级

🍰 守护线程为其他线程服务,在其他线程都退出时会自动退出

🍰 可统一捕获线程的未捕获异常,进行单独处理

相关内容:选择 《Java核心技术 卷1》查找相关笔记

评论🌹点赞👍收藏✨关注👀,是送给作者最好的礼物,愿我们共同学习,一起进步

如果对作者发布的内容感兴趣,可点击下方关注公众号 钰娘娘知识汇总 查看更多作者文章哦!

QQ头像吧