范文健康探索娱乐情感热点
投稿投诉
热点动态
科技财经
情感日志
励志美文
娱乐时尚
游戏搞笑
探索旅游
历史星座
健康养生
美丽育儿
范文作文
教案论文

腾讯面试官如何停止一个正在运行的线程?我一脸懵逼

  目录1. 停止不了的线程  2. 判断线程是否停止状态  3. 能停止的线程--异常法  4. 在沉睡中停止  5. 能停止的线程---暴力停止  6.方法stop()与java.lang.ThreadDeath异常  7. 释放锁的不良后果  8. 使用return停止线程
  停止一个线程意味着在任务处理完任务之前停掉正在做的操作,也就是放弃当前的操作。停止一个线程可以用Thread.stop()方法,但最好不要用它。虽然它确实可以停止一个正在运行的线程,但是这个方法是不安全的,而且是已被废弃的方法。在java中有以下3种方法可以终止正在运行的线程:  使用退出标志,使线程正常退出,也就是当run方法完成后线程终止。  使用stop方法强行终止,但是不推荐这个方法,因为stop和suspend及resume一样都是过期作废的方法。  使用interrupt方法中断线程。  1. 停止不了的线程
  interrupt()方法的使用效果并不像for+break语句那样,马上就停止循环。调用interrupt方法是在当前线程中打了一个停止标志,并不是真的停止线程。  public class MyThread extends Thread {     public void run(){         super.run();         for(int i=0; i<500000; i++){             System.out.println("i="+(i+1));         }     } }  public class Run {     public static void main(String args[]){         Thread thread = new MyThread();         thread.start();         try {             Thread.sleep(2000);             thread.interrupt();         } catch (InterruptedException e) {             e.printStackTrace();         }     } }
  输出结果:  ... i=499994 i=499995 i=499996 i=499997 i=499998 i=499999 i=5000002. 判断线程是否停止状态
  Thread.java类中提供了两种方法:  this.interrupted(): 测试当前线程是否已经中断;  this.isInterrupted(): 测试线程是否已经中断;
  那么这两个方法有什么图区别呢?我们先来看看this.interrupted()方法的解释:测试当前线程是否已经中断,当前线程是指运行this.interrupted()方法的线程。  public class MyThread extends Thread {     public void run(){         super.run();         for(int i=0; i<500000; i++){             i++; //            System.out.println("i="+(i+1));         }     } }  public class Run {     public static void main(String args[]){         Thread thread = new MyThread();         thread.start();         try {             Thread.sleep(2000);             thread.interrupt();              System.out.println("stop 1??" + thread.interrupted());             System.out.println("stop 2??" + thread.interrupted());         } catch (InterruptedException e) {             e.printStackTrace();         }     } }
  运行结果:  stop 1??false stop 2??false
  类Run.java中虽然是在thread对象上调用以下代码:thread.interrupt(), 后面又使用  System.out.println("stop 1??" + thread.interrupted()); System.out.println("stop 2??" + thread.interrupted());
  来判断thread对象所代表的线程是否停止,但从控制台打印的结果来看,线程并未停止,这也证明了interrupted()方法的解释,测试当前线程是否已经中断。这个当前线程是main,它从未中断过,所以打印的结果是两个false.
  如何使main线程产生中断效果呢?  public class Run2 {     public static void main(String args[]){         Thread.currentThread().interrupt();         System.out.println("stop 1??" + Thread.interrupted());         System.out.println("stop 2??" + Thread.interrupted());          System.out.println("End");     } }
  运行效果为:  stop 1??true stop 2??false End
  方法interrupted()的确判断出当前线程是否是停止状态。但为什么第2个布尔值是false呢?官方帮助文档中对interrupted方法的解释: 测试当前线程是否已经中断。线程的中断状态由该方法清除。 换句话说,如果连续两次调用该方法,则第二次调用返回false。
  下面来看一下inInterrupted()方法。  public class Run3 {     public static void main(String args[]){         Thread thread = new MyThread();         thread.start();         thread.interrupt();         System.out.println("stop 1??" + thread.isInterrupted());         System.out.println("stop 2??" + thread.isInterrupted());     } }
  运行结果:  stop 1??true stop 2??true
  isInterrupted()并未清除状态,所以打印了两个true。  3. 能停止的线程--异常法
  有了前面学习过的知识点,就可以在线程中用for语句来判断一下线程是否是停止状态,如果是停止状态,则后面的代码不再运行即可:  public class MyThread extends Thread {     public void run(){         super.run();         for(int i=0; i<500000; i++){             if(this.interrupted()) {                 System.out.println("线程已经终止, for循环不再执行");                 break;             }             System.out.println("i="+(i+1));         }     } }  public class Run {     public static void main(String args[]){         Thread thread = new MyThread();         thread.start();         try {             Thread.sleep(2000);             thread.interrupt();         } catch (InterruptedException e) {             e.printStackTrace();         }     } }
  运行结果:  ... i=202053 i=202054 i=202055 i=202056 线程已经终止, for循环不再执行
  上面的示例虽然停止了线程,但如果for语句下面还有语句,还是会继续运行的。看下面的例子:  public class MyThread extends Thread {     public void run(){         super.run();         for(int i=0; i<500000; i++){             if(this.interrupted()) {                 System.out.println("线程已经终止, for循环不再执行");                 break;             }             System.out.println("i="+(i+1));         }          System.out.println("这是for循环外面的语句,也会被执行");     } }
  使用Run.java执行的结果是:  ... i=180136 i=180137 i=180138 i=180139 线程已经终止, for循环不再执行 这是for循环外面的语句,也会被执行
  如何解决语句继续运行的问题呢?看一下更新后的代码:  public class MyThread extends Thread {     public void run(){         super.run();         try {             for(int i=0; i<500000; i++){                 if(this.interrupted()) {                     System.out.println("线程已经终止, for循环不再执行");                         throw new InterruptedException();                 }                 System.out.println("i="+(i+1));             }              System.out.println("这是for循环外面的语句,也会被执行");         } catch (InterruptedException e) {             System.out.println("进入MyThread.java类中的catch了…");             e.printStackTrace();         }     } }
  使用Run.java运行的结果如下:  ... i=203798 i=203799 i=203800 线程已经终止, for循环不再执行 进入MyThread.java类中的catch了… java.lang.InterruptedException  at thread.MyThread.run(MyThread.java:13)4. 在沉睡中停止
  如果线程在sleep()状态下停止线程,会是什么效果呢?  public class MyThread extends Thread {     public void run(){         super.run();          try {             System.out.println("线程开始…");             Thread.sleep(200000);             System.out.println("线程结束。");         } catch (InterruptedException e) {             System.out.println("在沉睡中被停止, 进入catch, 调用isInterrupted()方法的结果是:" + this.isInterrupted());             e.printStackTrace();         }      } }
  使用Run.java运行的结果是:  线程开始… 在沉睡中被停止, 进入catch, 调用isInterrupted()方法的结果是:false java.lang.InterruptedException: sleep interrupted  at java.lang.Thread.sleep(Native Method)  at thread.MyThread.run(MyThread.java:12)
  从打印的结果来看, 如果在sleep状态下停止某一线程,会进入catch语句,并且清除停止状态值,使之变为false。
  前一个实验是先sleep然后再用interrupt()停止,与之相反的操作在学习过程中也要注意:  public class MyThread extends Thread {     public void run(){         super.run();         try {             System.out.println("线程开始…");             for(int i=0; i<10000; i++){                 System.out.println("i=" + i);             }             Thread.sleep(200000);             System.out.println("线程结束。");         } catch (InterruptedException e) {              System.out.println("先停止,再遇到sleep,进入catch异常");             e.printStackTrace();         }      } }  public class Run {     public static void main(String args[]){         Thread thread = new MyThread();         thread.start();         thread.interrupt();     } }
  运行结果:  i=9998 i=9999 先停止,再遇到sleep,进入catch异常 java.lang.InterruptedException: sleep interrupted  at java.lang.Thread.sleep(Native Method)  at thread.MyThread.run(MyThread.java:15)5. 能停止的线程---暴力停止
  使用stop()方法停止线程则是非常暴力的。  public class MyThread extends Thread {     private int i = 0;     public void run(){         super.run();         try {             while (true){                 System.out.println("i=" + i);                 i++;                 Thread.sleep(200);             }         } catch (InterruptedException e) {             e.printStackTrace();         }     } }  public class Run {     public static void main(String args[]) throws InterruptedException {         Thread thread = new MyThread();         thread.start();         Thread.sleep(2000);         thread.stop();     } }
  运行结果:  i=0 i=1 i=2 i=3 i=4 i=5 i=6 i=7 i=8 i=9  Process finished with exit code 06.方法stop()与java.lang.ThreadDeath异常
  调用stop()方法时会抛出java.lang.ThreadDeath异常,但是通常情况下,此异常不需要显示地捕捉。  public class MyThread extends Thread {     private int i = 0;     public void run(){         super.run();         try {             this.stop();         } catch (ThreadDeath e) {             System.out.println("进入异常catch");             e.printStackTrace();         }     } }  public class Run {     public static void main(String args[]) throws InterruptedException {         Thread thread = new MyThread();         thread.start();     } }
  stop()方法以及作废,因为如果强制让线程停止有可能使一些清理性的工作得不到完成。另外一个情况就是对锁定的对象进行了解锁,导致数据得不到同步的处理,出现数据不一致的问题。  7. 释放锁的不良后果
  使用stop()释放锁将会给数据造成不一致性的结果。如果出现这样的情况,程序处理的数据就有可能遭到破坏,最终导致程序执行的流程错误,一定要特别注意:  public class SynchronizedObject {     private String name = "a";     private String password = "aa";      public synchronized void printString(String name, String password){         try {             this.name = name;             Thread.sleep(100000);             this.password = password;         } catch (InterruptedException e) {             e.printStackTrace();         }     }      public String getName() {         return name;     }      public void setName(String name) {         this.name = name;     }      public String getPassword() {         return password;     }      public void setPassword(String password) {         this.password = password;     } }  public class MyThread extends Thread {     private SynchronizedObject synchronizedObject;     public MyThread(SynchronizedObject synchronizedObject){         this.synchronizedObject = synchronizedObject;     }      public void run(){         synchronizedObject.printString("b", "bb");     } }  public class Run {     public static void main(String args[]) throws InterruptedException {         SynchronizedObject synchronizedObject = new SynchronizedObject();         Thread thread = new MyThread(synchronizedObject);         thread.start();         Thread.sleep(500);         thread.stop();         System.out.println(synchronizedObject.getName() + "  " + synchronizedObject.getPassword());     } }
  输出结果:  b  aa
  由于stop()方法以及在JDK中被标明为"过期/作废"的方法,显然它在功能上具有缺陷,所以不建议在程序张使用stop()方法。  8. 使用return停止线程
  将方法interrupt()与return结合使用也能实现停止线程的效果:  public class MyThread extends Thread {     public void run(){         while (true){             if(this.isInterrupted()){                 System.out.println("线程被停止了!");                 return;             }             System.out.println("Time: " + System.currentTimeMillis());         }     } }  public class Run {     public static void main(String args[]) throws InterruptedException {         Thread thread = new MyThread();         thread.start();         Thread.sleep(2000);         thread.interrupt();     } }
  输出结果:  ... Time: 1467072288503 Time: 1467072288503 Time: 1467072288503 线程被停止了!
  不过还是建议使用"抛异常"的方法来实现线程的停止,因为在catch块中还可以将异常向上抛,使线程停止事件得以传播。  最后
  在这里我再分享一份由多位大佬亲自收录整理的Android学习PDF+架构视频+面试文档+源码笔记,高级架构技术进阶脑图、Android开发面试专题资料,高级进阶架构资料
  这些都是我现在闲暇时还会反复翻阅的精品资料。里面对近几年的大厂面试高频知识点都有详细的讲解。相信可以有效地帮助大家掌握知识、理解原理,帮助大家在未来面试取得一份不错的答卷。
  当然,你也可以拿去查漏补缺,提升自身的竞争力。
  如果你有需要的话,只需私信我【进阶】即可获取

你知道吗?电视清洁也要讲究小技巧现代社会,即使手机与电脑更受欢迎,电视机也依旧是家家户户的必备电子产品。不管你家的是全新的4K电视机还是老旧电视机,有一个问题都是不可避免的屏幕上总是沾满灰尘。家里有孩子的更是会有粉丝自制PS1版血源诅咒明年1月免费登陆血源PSX是一款由粉丝玩家自制的PS1版本的血源诅咒游戏,将会在明年1月31日正式免费上线PC,该作的开发者LilithWalther发布了游戏的最新预告,对游戏的画面和内容等做了充电1小时,排队4小时,为啥一次国庆就让电动汽车痛点暴露无遗?按照惯例,国庆节和过年,我国高速都会堵车,没有例外。但今年国庆却与往年有些许不同。不同点有2个一是今年高速拥堵情况远甚于往年二是今年高速上电动汽车的数量远多于往年。于是今年国庆节期11月XGP新增游戏公布,双人成行等作包含在内微软在昨天正式公布了备受玩家期待的GamePass(XGP)订阅服务最新的11月游戏阵容,可以看到其中包含双人成行和GTA圣安地列斯最终版等非常受玩家欢迎的游戏,一起来看看详情吧!汽车没电就是电瓶坏了?别被卖电瓶的骗了!留个心眼前几天晚上大虎悠正准备休息,一位车友喊住了我。说他的车遇到了怪问题,请我帮忙分析分析。话说他的那台老马自达3(第二代大笑脸马三)仪表上的电瓶故障灯亮了,于是猜测可能是电瓶故障,因此免费游戏螃蟹游戏Steam好评率达到92,Twitch观众飙升至21万免费第一人称多人联机游戏螃蟹游戏在10月29日正式上线Steam平台,这款看起来非常简单又有趣,看起来非常像网页小游戏的新作品却俘获了很多玩家的心,目前Steam平台共有将近九千位真相广告宣传里很高级的全合成机油,究竟高级不高级?近来依然有车友纠结于机油在德国如何如何好吧,我们来复习一下我们知道,成品机油的成分是基础油添加剂。如果一款机油,基础油部分,仅仅有4类或者5类,或者45类构成,那么按照所谓的德标它电动网约车司机的赚钱神器?近几年是新能源电动汽车蓬勃发展的几年,同时也是网约车行业在祖国遍地开花的几年。于是两者之间产生了一些化学反应,而反应的产物就是网约车电动化。对于网约车的营运者而言,节约营运成本是每谷歌推出防作弊神器每月四美元起众所周知,类似于苹果以及Google这些科技公司在教育方面都会下不小的本钱。而最近有消息曝出,Google凭借其强大的搜寻功能研发出新款工具,能够帮助老师们来检查学生是否有抄作业的新厨引领新生活方式未来厨房消费者与企业皆期待我国疫情已经不在颠覆消费者的日常生活,但是疫情带来生活上的改变却依旧影响我们。宅家民间大厨各显神通,电饭煲煲蛋糕全社会爆红自制小凉皮肠粉裹上面包糠油炸一切,不知馋哭了多少隔壁小孩。集安全品质智能续航于一身,宋PLUSEV找不到拒绝的理由比亚迪作为新能源领域的佼佼者,已经成为不少朋友的购车首选。宋PLUSDMi之后,带刀上阵的宋PLUSEV同样备受追捧。今天我就带大家到附近的比亚迪e网4S店,实地感受这款车的魅力。
新品第二期4G工业路由器R9607系列在工业作业现场里,路由器的使用现状经常都是使用路由器基本功能串口转网络数据透传场景。如农业气象监测中的监控摄像头(网口设备)和各类环境传感器(RS485串口设备),为满足此类场景联市值暴跌6200亿!一月被罚6次,马化腾和腾讯还扛得住吗?马化腾的腾讯拥有qq以及微信这两款价值最高的产品,这些年来,腾讯发展的非常不错,如今也已经与百度以及阿里并称是国内互联网三大巨头。腾讯的发展史1999年,马化腾研发了一个新的软件,智慧农贸人们眼中的菜市场是什么样的?为了满足人们多元化的消费需求,很多传统农贸市场纷纷开始升级改造,通过软硬件升级,摇身一变成为有智慧有颜值的智慧农贸智慧农贸菜市场系统改造建设智慧农贸解决方案服务商广州睿途士电子科技手机一碰开锁OTG反向充电,华为智选徳施曼智能门锁入手记朋友想换指纹智能锁已经很久了,正好之前入手了一款华为智选德施曼智能锁,作为德施曼的老用户(之前机械的自动的都是用的德施曼)用了大概三个月的时间体验还是不错的,所以之前618的时候我超苹果三星!泰国Q2季度智能手机出货量排名OPPO位居榜首随着8月份即将划上尾声,全球知名调研机构Counterpoint也适时发布了2021年Q2季度泰国的智能手机出货量。据Counterpoint表示,与去年相比,今年Q2季度泰国的智半年股价蒸发近万亿,拼多多的路还能走多久?有这样一些公司,它们一直在亏损,想要用补贴的方式来获得用户的规模。但最终他们还是要想办法让市场看到,他们可以通过创造价值来实现盈利。阿里巴巴集团执行副主席蔡崇信说道。其实这段话说得百度发布Apollo汽车机器人和无人车出行服务平台萝卜快跑8月18日,在百度世界大会上,百度创始人李彦宏首次提出了汽车机器人的概念,并发布了具有跨时代意义的Apollo汽车机器人。据介绍,百度汽车机器人在外观上,自动鸥翼门全玻璃车顶与外部长视频平台之战优酷跌落前三,B站日活用户6500万成功上位看剧看电影看综艺,你习惯上哪个平台?大多数人的回答一定是爱奇艺腾讯视频优酷。爱腾优三家独大,已霸占国内长视频平台前三甲许久,广告频出版权争夺会员限播成为常态。然而,就在这场长视频平I饭整合平台怎么装?这套配置收藏好在当下显卡市场行情波动较大,价格居高不下的时期,部分玩家决定先装一台集显主机用着,玩玩电竞类网游是肯定没有问题的,等以后显卡价格下来了再加装显卡也不迟。在最新的第11代酷睿上,InwatchOS8beta6要来了,正式版9月发布会上亮相在iOS15iPadOS15和tvOS15测试版于周二发布数小时后,Apple第六个watchOS8开发者测试版现已可供测试。最新版本可以通过Apple开发人员中心为注册测试程序的又翻车?升级iOS14。7。1正式版手机无信号,果粉没升级的先别升iOS系统和安卓系统相比,在安全性上因为是独立的,所以一直被果粉称赞,但是这几年iOS系统频频受到漏洞和bug的困扰,更新速度也是越来越快了。关于iPhone手机iOS系统升级通常