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 ...
随机推荐
- Hibernate Mapping Exception:-9
if("true".equals(map.get("isAudited"))){ isAudited="=";//已审核 }else{ is ...
- 启用事务操作,解决批量插入或更新sqlite,mssql等数据库耗时问题
private void button1_Click(object sender, EventArgs e) { //Sqlite使用事务批量操作 极大的提高速度 DateTime starttime ...
- Python通过future处理并发
future初识 通过下面脚本来对future进行一个初步了解:例子1:普通通过循环的方式 import os import time import sys import requests POP20 ...
- TableView的性能优化
现在市场上的iOS应用程序界面中使用最多的UI控件是什么? 答案肯定是UITableView,几乎每一款App都有很多的界面是由UITableView实现的,所以为了做出一款优秀的App,让用户有更好 ...
- $http设置headers来实现IE不缓存url请求的资源
var getOrders = function(){ var deferred = $q.defer(); $http({ method:'get', url:'/getOr ...
- C#类的学习
①类的定义是以关键字 class 开始,后跟类的名称.类的主体,包含在一对花括号内.下面是类定义的一般形式: 类的修饰符 class 类名 :继承的类{ //类的成员 } 请注意: 如果要访问类的成员 ...
- SGU 223 Little Kings(状压DP)
Description 用字符矩阵来表示一个8x8的棋盘,'.'表示是空格,'P'表示人质,'K'表示骑士.每一步,骑士可以移动到他周围的8个方格中的任意一格.如果你移动到的格子中有人质(即'P'), ...
- Linux中的各种软件安装
Linux下的软件形式 Linux上的软件有几种常见的方式 二进制发布包 软件包已经针对具体平台完成了编译和打包,解压后即可以使用,最多去改改配置文件,也是Linux上最通用和常见的软件包发布形式 例 ...
- LeetCode 35. Search Insert Position (搜索嵌入的位置)
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) 解决方案
1.命令行用maven编译项目失败,提示 Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compi ...