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. Android一键换肤功能:一种简单的实现

     Android一键换肤功能:一种简单的实现 现在的APP开发,通常会提供APP的换肤功能,网上流传的换肤代码和实现手段过于复杂,这里有一个开源实现,我找了一大堆,发现这个项目相对较为简洁:htt ...

  2. VNC Server Installation on CentOS 6.5

    In my case I have a fresh installed CentOS6.5 Server on which I will be installing the VNC-server so ...

  3. memcached集群

    借鉴:http://www.cnblogs.com/happyday56/p/3461113.html 首先说明下memcached存在如下问题 本身没有内置分布式功能,无法实现使用多台Memcach ...

  4. 一次MySQL(INNODB存储引擎) 死锁捉虫记

    前言 任何系统不管在什么阶段都需要关注生产环境错误日志,最近几个月内,发现偶尔会出现数据库死锁情况.以前碰到的数据库类错误大部分是SQL语法造成的错误,来到新东家之后才第一次碰到死锁情况,以前是搞游戏 ...

  5. [转]十五天精通WCF——第十天 学会用SvcConfigEditor来简化配置

    我们在玩wcf项目的时候,都是自己手工编写system.serviceModel下面的配置,虽然在webconfig中做wcf的服务配置的时候,vs提供大多 数的代码提示,但对于不太熟悉服务配置的小鸟 ...

  6. java 执行可执行文件时提示“could not find or load main class ”的问题

  7. [自己动手改wordpress.1]wordpress的插件User-Access-Manager在新的php版本号里面无法执行的bug.

    近期同事在玩wp, 就顺带一起看了下. 她说插件有个不能用. 是一个叫User Access Manager 的插件 详细表现就是在后台填好相应的roles角色的时候, 点提交就会跳到一个错误的页面 ...

  8. UI层自动化测试框架(一)-简介和环境搭建

    http://blog.csdn.net/ToBeTheEnder/article/details/52302777

  9. 大规模的I/O流中有效识别大数据并增强时间局部性

    一篇热数据识别存储外文翻译,本文主要在讲思想 原文题目:  HDCat: Effectively Identifying Hot Data in    Large-scale I/O Streams ...

  10. 学习笔记——WCF

    学了一下WCF,发现怎么跟Web Service这么像! 这个WCF究竟干嘛的? 一查,原来: "Windows Communication Foundation (WCF) 是由微软发展的 ...