当前位置: 首页 > news >正文

肇庆市住房和城乡房屋建设局网站百度网盘客服人工电话95188

肇庆市住房和城乡房屋建设局网站,百度网盘客服人工电话95188,个人网页设计与实现ppt,网站做优化有几种方式JUC线程中断相关问题总结 线程中断相关问题总结 JUC线程中断相关问题总结一、 sleep 和线程中断之间的关系和特点结论测试验证代码如下 二、 LockSupport 和线程中断之间的关系结论测试验证代码如下 一、 sleep 和线程中断之间的关系和特点 结论 线程调用 Thread.sleep之后会进…

JUC线程中断相关问题总结

线程中断相关问题总结

  • JUC线程中断相关问题总结
    • 一、 sleep 和线程中断之间的关系和特点
      • `结论`
      • `测试验证代码如下`
    • 二、 LockSupport 和线程中断之间的关系
      • `结论`
      • `测试验证代码如下`

一、 sleep 和线程中断之间的关系和特点

结论

  1. 线程调用 Thread.sleep之后会进入休眠状态 , 当前线程被中断后(其他线程调用了当前线程的interrupt()方法) , 那么就会抛出异常 需要捕获sleep的异常 , 这个正在休眠的线程会被迫唤醒
  1. 休眠的线程被中断后 唤醒后 其中断标记位会重置为false (可通过 thread.isInterrupted() 方法来验证)

测试验证代码如下

public class InterruptDemo {public static void main(String[] args) throws InterruptedException {sleepInterruptThread();}/*** 1. 中断调用了sleep方法的进行休眠的线程** 输出结果:** 1.SleepInterruptThread|| false* ERROR==>抛出异常:sleep interrupted* 2.SleepInterruptThread|| false* 3.1683772273061|| false* 4.1683772275072|| 完成任务!** 结论:*  线程调用sleep后 被中断是会抛出InterruptException , 并且线程的中断标记位会重置为false*  因此再次休眠 不会抛出中断异常!**/private static void sleepInterruptThread() throws InterruptedException {Thread t1 = new Thread(() -> {System.out.println("1." + Thread.currentThread().getName() + "|| " + Thread.currentThread().isInterrupted());try {Thread.sleep(10000);} catch (InterruptedException e) {System.out.println("ERROR==>抛出异常:" + e.getMessage());System.out.println("2." + Thread.currentThread().getName() + "|| " + Thread.currentThread().isInterrupted());}System.out.println("3." + System.currentTimeMillis() + "|| " + Thread.currentThread().isInterrupted());try {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("4." + System.currentTimeMillis() + "|| 完成任务!");},"SleepInterruptThread");t1.start();Thread.sleep(1000);t1.interrupt();}}

二、 LockSupport 和线程中断之间的关系

结论

  1. 被LockSupport.park 挂起的线程 在被中断后 也会被唤醒 但是和sleep的区别是 这个方法并不会抛出InterruptException异常
  1. 中断唤醒之后不会抛出异常 , 但是这个线程的中断标记位变为了true 并不会重置 (可通过 thread.isInterrupted() 方法来验证)

测试验证代码如下

测试LockSupport.park中断后的线程状态 , 还有中断后和sleep方法结合使用

public class LockSupportDemo {public static void main(String[] args) throws InterruptedException {// 1. 测试LockSupport挂起的线程 被中断是否抛出异常lockSupport();Thread.sleep(1000);System.out.println("=============================================");System.out.println("=============================================");System.out.println("=============================================");// 2. 测试LockSupport中断后和sleep方法一起使用lockSupportSleep();}/*** 1. 被LockSupport.park 挂起的线程 调用中断方法后并不会抛出异常 , 但是线程的中断标记位的确变为了true** 输出结果:** 1.LockSupportThread||false* 3.LockSupportThread||true* 2.LockSupportThread||true** 可以看到通过 LockSupport 挂起的线程在被中断后并不会抛出异常 但是中断标记位是改为了true**/private static void lockSupport() throws InterruptedException {Thread t1 = new Thread(() -> {System.out.println("1." + Thread.currentThread().getName() + "||" + Thread.currentThread().isInterrupted());// 将线程挂起LockSupport.park();System.out.println("2." + Thread.currentThread().getName() + "||" + Thread.currentThread().isInterrupted());// 如果后边跟上 Thread.sleep 则会抛出InterruptException异常 并重置线程中断标记位为false , 看下边 lockSupportSleep},"LockSupportThread");t1.start();Thread.sleep(500);t1.interrupt();System.out.println("3." + t1.getName() + "||" + t1.isInterrupted());}/*** 2. LockSupport中断线程后 和sleep方法结合使用** 输出结果:** 1.LockSupportSleepThread||false* 2.LockSupportSleepThread||true* 抛出异常==》sleep interrupted* 3.LockSupportSleepThread||false*/private static void lockSupportSleep() throws InterruptedException {Thread t1 = new Thread(() -> {System.out.println("1." + Thread.currentThread().getName() + "||" + Thread.currentThread().isInterrupted());// 将线程挂起LockSupport.park();System.out.println("2." + Thread.currentThread().getName() + "||" + Thread.currentThread().isInterrupted());// 如果后边跟上 Thread.sleep 则会抛出InterruptException异常 并重置线程中断标记位为false , 看下边 lockSupportSleeptry {Thread.sleep(1000);} catch (InterruptedException e) {System.out.println("抛出异常==》" + e.getMessage());System.out.println("3." + Thread.currentThread().getName() + "||" + Thread.currentThread().isInterrupted());}},"LockSupportSleepThread");t1.start();Thread.sleep(500);t1.interrupt();}}
http://www.ds6.com.cn/news/96079.html

相关文章:

  • 厦门建设与管理局网站互联网推广中心
  • 打电话来说做网站 然后答应了网络工程师是干什么的
  • 万网网站建设方案书 备案百度广告竞价排名
  • 什么是社会化网络营销方式青岛关键词优化平台
  • 扁平化企业网源码win8风格精简化源码asp带后台企业网站泸州网站seo
  • 建购物网站seo优化工程师
  • 做一直播网站要多少钱百度搜索指数的数据来源
  • 网站域名是什么江苏seo平台
  • wordpress如何修改模板长沙seo外包服务
  • 网络广告图片seo外链优化
  • 贵阳专业做网站的公司网站关键词优化教程
  • 温州58同城怎么做网站专业黑帽seo
  • 网站建设交流论坛游戏推广公司
  • 做网站优化的弊端互联网营销师考试题库
  • wordpress 网站 上传百度一下首页
  • 漳州模板网站建设今天最新的新闻头条新闻
  • 翻译国外网站做原创苏州搜索引擎排名优化商家
  • 做网站+广告费+步骤在线制作网站免费
  • 安徽省住房和建设厅网站短视频培训机构
  • 用python做 网站论坛优化网站排名方法
  • 做甜品网站的需求分析百度邮箱注册入口
  • 二手车网站设计百度搜不干净的东西
  • 中国flash网站模板中心河北百度seo
  • 做网站如何兼职免费推广公司的网站
  • 公司网站建设开源平台手机卡顿优化软件
  • 网站空间一般有多大谷歌推广费用
  • 长沙市天心建设局网站上海关键词优化公司哪家好
  • 嘉祥做网站厦门人才网最新招聘信息
  • 如何请人做网站最新的军事新闻
  • 建行网点seo体系