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 ...
随机推荐
- 电脑IP地址被占用如何释放?
回车后,关机,等待5分钟左右再开机,就释放掉了.
- 用html+css+js做打地鼠小游戏
html 代码 first.html <!DOCTYPE html> <html lang="en"> <head> <meta char ...
- YYModel学习总结YYClassInfo(1)
OC的run-time 机制,简直像是网络上的猫! 我在开发中很少用到,但是作为iOS开发 人家肯定会问这个东西,所以深入的学习了下. 对于 run-time的入手,YYModel的学习,简直让人美滋 ...
- curl命令用于模拟http浏览器发起动作
1.模拟http浏览器发起访问百度首页的动作 curl http://www.baidu.com 2.也可以模拟http浏览器发起POST动作,这个在测试后端程序时非常常见.
- 1.ElasticSearch介绍及基本概念
一.ElasticSearch介绍 一个采用RESTful API标准的高扩展性的和高可用性的实时性分析的全文搜索工具 基于Lucene[开源的搜索引擎框架]构建 ElasticSearch是一个面向 ...
- win10 UWP Controls by function
Windows的XAML UI框架提供了很多控件,支持用户界面开发库.其中一些有可视化,一些布局. 一些控件例子:https://github.com/Microsoft/Windows-univer ...
- Entity Framework Core 2.0 使用入门
一.前言 Entity Framework(后面简称EF)作为微软家的ORM,自然而然从.NET Framework延续到了.NET Core.以前我也嫌弃EF太重而不去使用它,但是EF Core(E ...
- 【转】C语言中内存分配
原文:C语言中内存分配 在任何程序设计环境及语言中,内存管理都十分重要.在目前的计算机系统或嵌入式系统中,内存资源仍然是有限的.因此在程序设计中,有效地管理内存资源是程序员首先考虑的问题. 第1节主要 ...
- APUE 4 - 线程<2> : 线程同步
当控件的多个线程共享统一内存时,我们需要确定各个线程访问到的数据的一致性.在cpu结构中,修改操作由多个内存读写周期(memory cycle),而在这些内存周期之间, 有可能会发生其他线程的内存读操 ...
- js math对象总结
1: Math 对象用于执行数学任务. 2:Math 对象并不像 Date 和 String 那样是对象的类,因此没有构造函数 Math(), Math.sin() 这样的函数只是函数 3:通过把 ...