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 ...
随机推荐
- zoj3961(区间问题)
点击打开zoj1961Let's Chat Time Limit: 1 Second Memory Limit:65536 KB ACM (ACMers' Chatting Messenge ...
- ZOJ2150 Raising Modulo Numbers 快速幂
ZOJ2150 快速幂,但是用递归式的好像会栈溢出. #include<cstdio> #include<cstdlib> #include<iostream> # ...
- ZOJ 2042 Divisibility (DP)
Divisibility Time Limit: 2 Seconds Memory Limit:65536 KB Consider an arbitrary sequence of inte ...
- 干了这杯Java之ArrayList
List存储一个有序元素合集 List接口的实现类有: ArrayList,LinkedList,Vector,Stack ArrayList一个数组型的List 默认容量为10 private st ...
- angular添加,查找与全部删除
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- 【转载】quickLayout.css-快速构建结构兼容的web页面
文章转载自 张鑫旭-鑫空间-鑫生活 http://www.zhangxinxu.com/wordpress/ 原文链接:http://www.zhangxinxu.com/wordpress/?p=4 ...
- JavaScript享元模式
通过两个例子的对比来凸显享元模式的特点:享元模式是一个为了提高性能(空间复杂度)的设计模式,享元模式可以避免大量非常相似类的开销. 第一实例,没有使用享元模式,计算所花费的时间和空间使用程度. 要求为 ...
- event模拟数据库链接
from threading import Thread,Event,currentThread import time e = Event() def conn_mysql(): count = 1 ...
- Linux入门(14)——Ubuntu常用快捷键
打开终端:ctrl + alt + T 左右分屏:ctrl + win + 箭头左或者箭头右 显示桌面:Ctrl + win + D 切换工作区:ctrl + alt + 箭头左或者箭头右 新建文件夹 ...
- MVC-HtmlHelper简单总结
Asp.Net MVC - Htmlhelper 总结 HtmlHelper是一个返回Html字符串的方法.返回的字符串可以是任意类型.例如你可以使用HtmlHelper方法返回一个标准的html标签 ...