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执行数据的提交和返回 问题:考虑 ...
随机推荐
- 7-15 QQ帐户的申请与登陆
7-15 QQ帐户的申请与登陆(25 分) 实现QQ新帐户申请和老帐户登陆的简化版功能.最大挑战是:据说现在的QQ号码已经有10位数了. 输入格式: 输入首先给出一个正整数N(≤105),随后给 ...
- SFTP文件上传下载
http://www.cnblogs.com/longyg/archive/2012/06/25/2556576.html (转载)
- ES6的let和var声明变量的区别
关于let的描述 let允许你声明一个作用域被限制在块级中的变量.语句或者表达式.与var关键字不同的是,它声明的变量只能是全局或者整个函数块的. 作用域规则 let声明的变量只在其声明的块或子块中可 ...
- hdu 3657最大点权独立集变形(方格取数变形)
/* 分奇偶为二部图,s与奇建图,t与偶建图,权值为当前数的值,如果遇到必取的权值置为inf. 奇偶建边为相邻的权值为2*(x&y):所有数的值-最小点全覆盖. 置为inf意为不能割掉.奇偶边 ...
- hdu 4171 最短路
#include<stdio.h> #include<string.h> #include<queue> #include<iostream> usin ...
- SQLiteOpenHelper/SQLiteDatabase/Cursor源代码解析
转载请注明出处:http://blog.csdn.net/y_zhiwen/article/details/51583188 Github地址.欢迎star和follow 新增android sqli ...
- leetcode第一刷_Minimum Window Substring
好题.字符串.线性时间. 我认为第一次拿到这个题的人应该不会知道该怎么做吧,要么就是我太弱了..先搞清楚这个题要求的是什么.从一个长字符串中找一个字串,这个字串中的字符全然包括了另一个给定目标串中的字 ...
- Oracle批量恢复drop操作删除的表、索引等对象
/**********************************************************************查询Drop操作删除的对象select * from re ...
- linux 搭建https server (apache)
一. 安装准备 1. 安装Openssl 要使Apache支持SSL,须要首先安装Openssl支持.这里使用的是openssl-0.9.8k.tar.gz 下载Openssl:htt ...
- 使用汇编分析c代码的内存分布
arm平台下使用反汇编分析c内存分布: arm:使用arm-linux-objdump命令将编译完毕之后的elf文件,进行反汇编. 之后重定向到tmp.s文件里. 第一步变量例如以下c文件. vim ...