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中断线程的更多相关文章

  1. Interrupt中断线程注意点

    首先我们要明确,线程中断并不会使线程立即退出,而是发送一个通知,告知目标线程你该退出了,但是后面如何处理,则完全有目标线程自行决定. 这就是和stop()不一样的地方,stop执行后线程会立即终止,这 ...

  2. 注意Thread.interrupt()方法的真正作用并不是用来中断线程

      程序是很简易的.然而,在编程人员面前,多线程呈现出了一组新的难题,如果没有被恰当的解决,将导致意外的行为以及细微的.难以发现的错误.      在本篇文章中,我们针对这些难题之一:如何中断一个正在 ...

  3. 用interrupt()中断Java线程

    最近在学习Java线程相关的东西,和大家分享一下,有错误之处欢迎大家指正. 假如我们有一个任务如下,交给一个Java线程来执行,如何才能保证调用interrupt()来中断它呢? class ATas ...

  4. 中断线程Interrupt()

    以下是参考<<Java多线程模式>>的 1. sleep() & interrupt() 线程A正在使用sleep()暂停着: Thread.sleep(100000) ...

  5. 并发和多线程(二)--启动和中断线程(Interrupt)的正确姿势

    启动线程: 从一个最基本的面试题开始,启动线程到底是start()还是run()? Runnable runnable = () -> System.out.println(Thread.cur ...

  6. Java多线程系列--“基础篇”09之 interrupt()和线程终止方式

    概要 本章,会对线程的interrupt()中断和终止方式进行介绍.涉及到的内容包括:1. interrupt()说明2. 终止线程的方式2.1 终止处于“阻塞状态”的线程2.2 终止处于“运行状态” ...

  7. java多线程系类:基础篇:09之interrupt()和线程终止方式

    概要 本章,会对线程的interrupt()中断和终止方式进行介绍.涉及到的内容包括:1. interrupt()说明2. 终止线程的方式2.1 终止处于"阻塞状态"的线程2.2 ...

  8. 如何实现Android 中断线程的处理

    我现在对一个用户注册的功能1.用ProgressDialog将当前页面设成不可操作(保留返回键 退出ProgressDialog)2.用一个线程clientThread执行数据的提交和返回 问题:考虑 ...

  9. Android 中断线程的处理

    我现在对一个用户注册的功能1.用ProgressDialog将当前页面设成不可操作(保留返回键 退出ProgressDialog)2.用一个线程clientThread执行数据的提交和返回 问题:考虑 ...

随机推荐

  1. GeoTrust 企业(OV)型 通配符(Wildcard) SSL证书

      GeoTrust 企业(OV)型 通配符(Wildcard)SSL证书(GeoTrust True BusinessID Wildcard SSL Certificates),支持通配符(Wild ...

  2. [Bzoj3940] [AC自动机,USACO 2015 February Gold] Censor [AC自动机模板题]

    AC自动机模板题(膜jcvb代码) #include <iostream> #include <algorithm> #include <cstdio> #incl ...

  3. Spring MVC 入门(二)

    前言 上一篇讲到了 Spring MVC 的基本请求处理流程,本篇主要说一说如何使用 Spring MVC 来构建一个简单例子. 大致步骤如下 1.新建项目 2.导入相应 jar 包 (Spring ...

  4. Clojure:两步发送iOS推送通知(apns)

    首先在project.clj中,添加对notnoop 类库的引用:[com.notnoop.apns/apns "0.2.3"] 然后使用如下方法就可以发送推送消息了: (ns d ...

  5. 一起talk C栗子吧(第七十七回:C语言实例--DIY ls命令续)

    各位看官们,大家好.上一回中咱们说的是DIY cat命令的样例.这一回咱们说的样例是:DIY ls命令续. 闲话休提,言归正转.让我们一起talk C栗子吧! 我们在前面的章回中DIY过ls命令.时间 ...

  6. Oracle 11g OEM登录后提示“出现内部错误”

    使用oem登录时提示:“出现内部错误.有关详细信息, 请查看日志文件”. 具体原因未知,发现使用SQL Plus登录一次之后,再次登录即可.

  7. 通过telent、php深入了解http协议

    HTTP协议:简单点就是client怎么问.server如何答. 重要性:webservice 还是rest做大型架构都离不开对http协议的认识,甚至能够简化的说webservice =  http ...

  8. vbs setkeys 特殊符号

    set keys={~}!^@^#^${%%}{^&^}{^^}{*}{(}{)}{_}{-}{=}{+}.;:'"

  9. CF 447A(DZY Loves Hash-简单判重)

    A. DZY Loves Hash time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  10. phoenixframe自己主动化測试平台对div弹出框(如弹出的div登陆框)的处理

    package org.phoenix.cases; import java.util.LinkedList; import org.phoenix.action.WebElementActionPr ...