Interrupt中断线程
package com.wistron.swpc.ecs.util;
public class WrongWayStopThread extends Thread{
public static void main(String[] args) {
WrongWayStopThread thread = new WrongWayStopThread();
System.out.println("Starting thread...");
thread.start();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Interrupting thread... ");
thread.interrupt();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Stopping application...");
}
public void run(){
while(!this.isInterrupted()){
System.out.println("Thread is running...");
//下面几行代码是模拟Thread.sleep(1000),至于为啥不用sleep是因为用了sleep无法正确中断线程
long time = System.currentTimeMillis();
while(System.currentTimeMillis()-time < 1000){
//减少屏幕输出的空循环
}
}
}
}
运行结果:
Starting thread...
Thread is running...
Thread is running...
Thread is running...
Thread is running...
Interrupting thread...
Stopping application...
上面模拟的sleep的方法换成sleep
package com.wistron.swpc.ecs.util;
public class WrongWayStopThread extends Thread{
public static void main(String[] args) {
WrongWayStopThread thread = new WrongWayStopThread();
System.out.println("Starting thread...");
thread.start();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Interrupting thread... ");
thread.interrupt();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Stopping application...");
}
public void run(){
while(!this.isInterrupted()){
System.out.println("Thread is running...");
//下面几行代码是模拟Thread.sleep(1000),至于为啥不用sleep是因为用了sleep无法正确中断线程
/*long time = System.currentTimeMillis();
while(System.currentTimeMillis()-time < 1000){
//减少屏幕输出的空循环
}*/
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
运行结果:
Starting thread...
Thread is running...
Thread is running...
Thread is running...
Interrupting thread...
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at com.wistron.swpc.ecs.util.WrongWayStopThread.run(WrongWayStopThread.java:39)
Thread is running...
Thread is running...
Thread is running...
Thread is running...
Stopping application...
Thread is running...
Thread is running...
Thread is running...
Thread is running...
Thread is running...
换成sleep之后不能结束线程
Interrupt中断线程的更多相关文章
- Interrupt中断线程注意点
首先我们要明确,线程中断并不会使线程立即退出,而是发送一个通知,告知目标线程你该退出了,但是后面如何处理,则完全有目标线程自行决定. 这就是和stop()不一样的地方,stop执行后线程会立即终止,这 ...
- 注意Thread.interrupt()方法的真正作用并不是用来中断线程
程序是很简易的.然而,在编程人员面前,多线程呈现出了一组新的难题,如果没有被恰当的解决,将导致意外的行为以及细微的.难以发现的错误. 在本篇文章中,我们针对这些难题之一:如何中断一个正在 ...
- 用interrupt()中断Java线程
最近在学习Java线程相关的东西,和大家分享一下,有错误之处欢迎大家指正. 假如我们有一个任务如下,交给一个Java线程来执行,如何才能保证调用interrupt()来中断它呢? class ATas ...
- 中断线程Interrupt()
以下是参考<<Java多线程模式>>的 1. sleep() & interrupt() 线程A正在使用sleep()暂停着: Thread.sleep(100000) ...
- 并发和多线程(二)--启动和中断线程(Interrupt)的正确姿势
启动线程: 从一个最基本的面试题开始,启动线程到底是start()还是run()? Runnable runnable = () -> System.out.println(Thread.cur ...
- Java多线程系列--“基础篇”09之 interrupt()和线程终止方式
概要 本章,会对线程的interrupt()中断和终止方式进行介绍.涉及到的内容包括:1. interrupt()说明2. 终止线程的方式2.1 终止处于“阻塞状态”的线程2.2 终止处于“运行状态” ...
- java多线程系类:基础篇:09之interrupt()和线程终止方式
概要 本章,会对线程的interrupt()中断和终止方式进行介绍.涉及到的内容包括:1. interrupt()说明2. 终止线程的方式2.1 终止处于"阻塞状态"的线程2.2 ...
- 如何实现Android 中断线程的处理
我现在对一个用户注册的功能1.用ProgressDialog将当前页面设成不可操作(保留返回键 退出ProgressDialog)2.用一个线程clientThread执行数据的提交和返回 问题:考虑 ...
- Android 中断线程的处理
我现在对一个用户注册的功能1.用ProgressDialog将当前页面设成不可操作(保留返回键 退出ProgressDialog)2.用一个线程clientThread执行数据的提交和返回 问题:考虑 ...
随机推荐
- SSL/TLS 协议介绍
SSL/TLS 协议(RFC2246 RFC4346)处于 TCP/IP 协议与各种应用层协议之间,为数据通讯提供安全支持. 从协议内部的功能层面上来看,SSL/TLS 协议可分为两层: 1. SSL ...
- java中的redis工具类
1.redis基础类 package com.qlchat.component.redis.template; import javax.annotation.PostConstruct; impor ...
- cxf 和 httpclient 客户端调用 webservice 接口
一.cxf 生成 webservice 客户端 1.接口路径 http://ws.webxml.com.cn/WebServices/WeatherWS.asmx 2.进入你需要放置 webservi ...
- VScode使用简介
1.1 VSCode简介 VSCode官网:https://code.visualstudio.com/ 支持语音: 速度较快,对超大文件读写速度飞快(打开10M代码不到1s,Subline原生会卡近 ...
- Spring Boot-Starter(九)
说明 在使用非spring boot项目我们集成spring mvc mybatis等框架往往需要大量xml配置, spring 的推出是为了解决项目的复杂度,随着项目的增长,xml配置会越来越臃肿, ...
- Clojure:两步发送iOS推送通知(apns)
首先在project.clj中,添加对notnoop 类库的引用:[com.notnoop.apns/apns "0.2.3"] 然后使用如下方法就可以发送推送消息了: (ns d ...
- Java单元測试工具JUnit 5新特性一览
Java单元測试工具JUnit 5新特性一览 作者:chszs,未经博主同意不得转载. 经许可的转载需注明作者和博客主页:http://blog.csdn.net/chszs JUnit是最流行的开源 ...
- 《从零開始学Swift》学习笔记(Day60)——Core Foundation框架
创文章,欢迎转载.转载请注明:关东升的博客 Core Foundation框架是苹果公司提供一套概念来源于Foundation框架,编程接口面向C语言风格的API.尽管在Swift中调用这样的C语 ...
- [Cypress] Test XHR Failure Conditions with Cypress
Testing your application’s behavior when an XHR call results in an error can be difficult. The use o ...
- Sahara中的数据模型
声明: 本博客欢迎转载.但请保留原作者信息,并请注明出处! 作者:郭德清 团队:华为杭州OpenStack团队 本文主要是介绍下Sahara中一些常见的数据模型. 1.Config 用于描写叙述配置信 ...