interrupt 停止线程
该方法只是给线程设置了一个停止的标记 并不是真正的立即停止线程
interrupted() 测试当前线程是否已经中断
isInterrupted() 测试线程是否已经中断
停止线程的方法:
.异常法 (相当于return出去)
package entity.thread; public class Mythread extends Thread{ @Override
public void run() {
super.run();
try {
for(int i=;i<;i++){
if(this.interrupted()){
System.out.println("是停止状态了。。。");
throw new InterruptedException();
}
System.out.println("i=" + (i+));
}
System.out.println("我在for下面");
} catch (InterruptedException e) {
System.out.println("进入异常方法了线程终止");
e.printStackTrace();
}
} public static void main(String[] args) throws InterruptedException {
try {
Mythread mh = new Mythread();
mh.start();
mh.sleep();
mh.interrupt();
} catch (Exception e) {
System.out.println("main catch");
e.printStackTrace();
}
System.out.println("end"); }
} 打印结果:
i=
i=
end
是停止状态了。。。
进入异常方法了线程终止
java.lang.InterruptedException
at entity.thread.Mythread.run(Mythread.java:) .在沉睡中被停止 (程序会直接抛异常)
package entity.thread; public class Mythread2 extends Thread{ @Override
public void run() {
super.run();
try {
System.out.println("run begin");
Thread.sleep();
System.out.println("run end");
} catch (InterruptedException e) {
System.out.println("在沉睡中被终止! 进入catch!"+ this.isInterrupted());
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
Mythread2 mythread2 = new Mythread2();
mythread2.start();
mythread2.sleep();
mythread2.interrupt();
} catch (InterruptedException e) {
System.out.println("main catch");
e.printStackTrace();
}
System.out.println("end");
}
}
执行结果
run begin
end
在沉睡中被终止! 进入catch!false
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at entity.thread.Mythread2.run(Mythread2.java:)
3.暴力停止 stop 方法 已经过时不建议使用并且会存在问题
interrupt 停止线程的更多相关文章
- Java多线程(五)停止线程 interrupt
调用interrupt方法仅仅是在当前线程中打了一个停止的标记,并不是真正停止线程. this.interrupted() :测试当前线程是否已经中断,执行后具有将状态标志清除为false的功能 is ...
- 多线程-停止线程方式-Interrupt
1 package multithread4; 2 /* 3 * 停止线程: 4 * 1,stop方法. 5 * 6 * 2,run方法结束. 7 * 8 * 怎么控制线程的任务结束呢? 9 * 任务 ...
- [改善Java代码]不使用stop方法停止线程
线程启动完毕后,在运行可能需要终止,Java提供的终止方法只有一个stop,但是不建议使用此方法,因为它有以下三个问题: (1)stop方法是过时的 从Java编码规则来说,已经过时的方式不建议采用. ...
- java多线程之停止线程
/*1.让各个对象或类相互灵活交流2.两个线程都冻结了,就不能唤醒了,因为根据代码要一个线程活着才能执行唤醒操作,就像玩木游戏3.中断状态就是冻结状态4.当主线程退出的时候,里面的两个线程都处于冻结状 ...
- java线程之停止线程
在Java中有以下3种方法可以终止一个正在运行的线程: 1.使用退出标志,是线程正常退出,也就是run方法完成后线程终止. 2.使用stop方法强制终止线程,但不推荐使用 ...
- 多线程---其他方法 停止线程、守护线程、join方法
第三方停止线程: 原来是stop(),因为该方法有些问题,所以被interrupt()方法取代,它的用途跟机制是 当没有指定的方式让冻结的线程恢复到运行状态时,这时需要对冻结进行清除,强制让线程恢复到 ...
- Thread之十:停止线程方法汇总
在上篇文章<多线程的使用——Thread类和Runnable接口>中提到中断线程的问题.在JAVA中,曾经使用stop方法来停止线程,然而,该方法具有固有的不安全性,因而已经被抛弃(Dep ...
- 深入理解Java中停止线程
一.停止线程会带来什么? 对于单线程中,停止单线程就是直接使用关键字return或者break,但是在停止多线程时是让线程在完成任务前去开启另外一条线程,必须放弃当前任务,而这个过程是不可预测,所以必 ...
- 正确停止线程的方式三 使用Thread类中的内置的中断标记位-----------不熟悉
package charpter10; public class Processor implements Runnable { @Override public void run() { for ( ...
随机推荐
- ftp限制
/etc/hosts.deny /etc/vsftpd/user_list 从2.3.5之后,vsftpd增强了安全检查,如果用户被限定在了其主目录下,则该用户的主目录不能再具有写权限了!如果检查发现 ...
- 移动端— position: fixed;固定定位解决方案
这里有个关键的东西叫做viewport,你经常在页面的头部里可以见到它: <meta name="viewport" content="width=device-w ...
- cuda addressMode解析
cudaAddressModeClamp:超出范围就用边界值代替,示意: AA | ABCDE | EE cudaAddressModeBorder:超出范围就用零代替,示意: 00 | ABCDE ...
- Iptables的规则语法
Iptables的规则语法 分类: 防火墙2012-04-19 17:09 1228人阅读 评论(0) 收藏 举报 inputtcpfilter防火墙output网络 (一) 基本语法 iptable ...
- vue中报错Props with type Object/Array must use a factory function to return the default value
Invalid default value for prop "value": Props with type Object/Array must use a factory fu ...
- 自定义的listbox支持拖放
unit unit2; interface uses Classes, Controls, StdCtrls; type TListBox2 = class(TCustomListBox) prote ...
- Python3中bytes和HexStr之间的转换
1 Python3中bytes和HexStr之间的转换 ByteToHex的转换 def ByteToHex( bins ): """ Convert a byte st ...
- 对于centos的运用ssh远程连接
1,首先安装ssh服务器 $yum install openssh-server 2,记录你当前centos的ip地址 $ifconfig 3,再在windows里面安装putty 4安装完成后, 在 ...
- cJSON api的使用教程
背景说明:由于和后台通信,为了统一数据格式,选择使用json格式,客户端开发语言使用的是c,故需要借助第三方库来实现json格式的转化,cJSON是一个很好的第三方库, 下载链接1:https://g ...
- 实用 | PyCharm常用快捷键整理
PyCharm是一款非常受欢迎的Python IDE,用Python高效处理web和数据科学,带有一整套可以帮助用户在使用Python语言开发时提高其效率的工具,比如调试.语法高亮.Project管理 ...