Java中断机制(interrupt)
while (!Thread.currentThread().interrupted() && more work to do) {}
interrupted 和 isInterrupted 区别
@Override
public void run() {
while(!Thread.currentThread().isInterrupted() ){
try{
//处理正常的逻辑
Thread.sleep(100);
}catch (InterruptedException e){
//被中断后的进入 //由于抛出异常后会把状态位改变,所以这里应该手动改变状态位
Thread.currentThread().interrupt();
}finally{
// 线程结束前的后续操作
}
}
}
@Override
public void run() {
try{
Thread.sleep(100);
}catch (InterruptedException e){ }
}
void mySubTask() throws InterruptedException {
...
sleep(delay);
...
}
public class Example1 implements Runnable{
private float d;
@Override
public void run() {
while(true){
for(int i=0;i<10000000;i++){
d = (float) (d + (Math.PI + Math.E) / d);
}
System.out.println("I'm counting......");
//转让调度器使用权
Thread.yield();
}
}
public static void main(String[] args) throws InterruptedException {
Example1 example1 = new Example1();
Thread t1 = new Thread(example1);
t1.start();
Thread.sleep(100);
System.out.println("开始中断线程。。。。。。");
t1.interrupt();
}
}
输出:
I'm counting......
开始中断线程。。。。。。
I'm counting......
I'm counting......
I'm counting......
I'm counting......
方法一:信号量法
class Example2 implements Runnable{
public static boolean isLive = true;
float d;
@Override
public void run() {
while(isLive){
for(int i=0;i<10000000;i++){
d = (float) (d + (Math.PI + Math.E) / d);
}
System.out.println("I'm counting......");
//转让调度器使用权
Thread.yield();
}
}
public static void main(String[] args) throws InterruptedException {
Example2 e2 = new Example2();
Thread t1 = new Thread(e2);
t1.start();
Thread.sleep(100);
System.out.println("开始中断线程。。。。。。");
//设置改变信号量
e2.isLive = false;
}
}
输出结果:
I'm counting......
开始中断线程。。。。。。
I'm counting......
方法二:抛出异常法
public class Example1 implements Runnable{
private double d = 0.0;
public void run() {
//死循环执行打印"I am running!" 和做消耗时间的浮点计算
try {
while (true) {
System.out.println("I am running!");
for (int i = 0; i < 900000; i++) {
d = d + (Math.PI + Math.E) / d;
}
//休眠一断时间,中断时会抛出InterruptedException
Thread.sleep(50);
}
} catch (InterruptedException e) {
System.out.println("ATask.run() interrupted!");
}
}
public static void main(String[] args) throws InterruptedException {
Example1 example1 = new Example1();
Thread t1 = new Thread(example1);
t1.start();
Thread.sleep(100);
System.out.println("开始中断线程。。。。。。");
t1.interrupt();
}
}
输出结果
I am running!
I am running!
开始中断线程。。。。。。
ATask.run() interrupted!
方法三:Thread.interrupted()监听
class Example3 implements Runnable {
@Override
public void run() {
while (!Thread.currentThread().interrupted()) {
try {
Thread.sleep(100);
System.out.println("I'm counting......");
} catch (InterruptedException e) {
//设置状态位
Thread.currentThread().interrupt();
}
}
}
public static void main(String[] args) throws InterruptedException {
Example3 e = new Example3();
Thread t1 = new Thread(e);
t1.start();
Thread.sleep(800);
System.out.println("开始中断线程。。。。。。");
t1.interrupt();
}
}
输出为:
I'm counting......
I'm counting......
I'm counting......
I'm counting......
I'm counting......
I'm counting......
开始中断线程。。。。。。
Java中断机制(interrupt)的更多相关文章
- 【转】详细分析Java中断机制
原文地址:http://www.infoq.com/cn/articles/java-interrupt-mechanism 1. 引言 当我们点击某个杀毒软件的取消按钮来停止查杀病毒时,当我们在控制 ...
- 详细分析Java中断机制(转)
1. 引言 当我们点击某个杀毒软件的取消按钮来停止查杀病毒时,当我们在控制台敲入quit命令以结束某个后台服务时……都需要通过一个线程去取消另一个线程正在执行的任务.Java没有提供一种安全直接的方法 ...
- 详细分析Java中断机制-转载
1. 引言 当我们点击某个杀毒软件的取消按钮来停止查杀病毒时,当我们在控制台敲入quit命令以结束某个后台服务时……都需要通过一个线程去取消另一个线程正在执行的任务.Java没有提供一种安全直接的方法 ...
- 详细分析Java中断机制[转]
1. 引言 当我们点击某个杀毒软件的取消按钮来停止查杀病毒时,当我们在控制台敲入quit命令以结束某个后台服务时……都需要通过一个线程去取消另一个线程正在执行的任务.Java没有提供一种安全直接的方法 ...
- Java中断机制
1. 引言 当我们点击某个杀毒软件的取消按钮来停止查杀病毒时,当我们在控制台敲入quit命令以结束某个后台服务时……都需要通过一个线程去取消另一个线程正在执行的任务.Java没有提供一种安全直接的方法 ...
- Java并发(基础知识)—— Java中断机制
上文讲解了Java线程的创建.启动以及停止,在讲到停止线程时说到了Java中断,Java中断是停止线程的一种协作机制,本文打算对Java中断机制进行详细讲解. 在网上搜索Java中断机制,发现两篇好文 ...
- Java Thread.interrupt interrupted
Java Thread.interrupt @(Base)[JDK, 线程, interrupt] 原文地址,转载请注明 下面这个场景你可能很熟悉,我们调用Thread.sleep(),conditi ...
- Java面试-interrupt
我们都知道,Java中停止一个线程不能用stop,因为stop会瞬间强行停止一个线程,且该线程持有的锁并不能释放.大家多习惯于用interrupt,那么使用它又有什么需要注意的呢? interrupt ...
- java中interrupt,interrupted和isInterrupted的区别
文章目录 isInterrupted interrupted interrupt java中interrupt,interrupted和isInterrupted的区别 前面的文章我们讲到了调用int ...
随机推荐
- ocs的沟通平台
Microsoft Office Communications Server 2007 R2 简称:OCS准时准确地联系人员以及管理信息过载根据人员的状态与其联系,然后单击最佳方式与其通信:通过电子邮 ...
- Java面向对象 String 基本数据类型对象包装类
Java面向对象 String 知识概要: (1)String的用法详解 (2)基本数据类型对象包装类 String 顾名思义,该类主要是对字符串 ...
- MySQL(十三)之MySQL事务
前言 这段时间自己会把之前学的东西都总结一遍,希望对自己以后的工作中有帮助.其实现在每天的状态都是很累的,但是我要坚持! 进入我们今天的正题: 为什么MySQL要 有事务呢?事务到底是用来干什么的?我 ...
- ActiveMQ——activemq的详细说明,queue、topic的区别(精选)
JMS中定义了两种消息模型:点对点(point to point, queue)和发布/订阅(publish/subscribe,topic).主要区别就是是否能重复消费. 点对点:Queue,不可重 ...
- 超全面!这可能是最全面的 jQuery 知识总结
个人建议:学习 jQuery 前先掌握基本的 JavaScrpit 语法,特别是对函数要掌握,jQuery 基本上是使用函数. jQuery 简介 jQuery 是一个轻量级 JavaScript 库 ...
- 【转载】CSS3 filter:drop-shadow滤镜与box-shadow区别应用
文章转载自 张鑫旭-鑫空间-鑫生活 http://www.zhangxinxu.com/wordpress/ 原文链接:http://www.zhangxinxu.com/wordpress/?p=5 ...
- SAP问题【转载】
1.A:在公司代码分配折旧表时报错? 在公司代码分配折旧表时报错,提示是"3000 的公司代码分录不完全-参见长文本" 希望各位大侠帮我看看. 3000 的公司代码分录不完全-参见 ...
- 移动端效果之IndexList
写在前面 接着前面的移动端效果讲,这次讲解的的是IndexList的实现原理.效果如下: 代码请看这里:github 移动端效果之swiper 移动端效果之picker 移动端效果之cellSwipe ...
- angular 4 实现的tab栏切换
管理系统 tab 切换页,是一种常见的需求,大概如下: 点击左边菜单,右边显示相应的选项卡,然后不同的选项卡面可以同时编辑,切换时信息不掉失! 用php或.net,java的开发技术,大概是切换显示, ...
- Linux: 查看软件安装路径
一. Which 命令 Shell 的which 命令可以找出相关命令是否已经在搜索路径中. 如: [root@localhost ~]# which gcc /usr/bin/gcc ...