07.interrupt
/**
*isInterrupted
*/
public class InterruptDemo {
public static void main(String[] args) throws InterruptedException{
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
while (!Thread.currentThread().isInterrupted()){
System.out.println(Thread.currentThread()+"没被中断");
}
}
});
thread.start();
Thread.sleep(1000);
System.out.println("main thread interrupt thread");
thread.interrupt();
thread.join();
System.out.println("main is over");
//...
//Thread[Thread-0,5,main]没被中断
//Thread[Thread-0,5,main]没被中断
//Thread[Thread-0,5,main]没被中断
//Thread[Thread-0,5,main]没被中断
//Thread[Thread-0,5,main]没被中断
//main thread interrupt thread
//Thread[Thread-0,5,main]没被中断
//main is over
}
}
/**
* interrupt()方法打断线程的休眠
*/
public class InterruptDemo2 {
public static void main(String[] args) throws InterruptedException{
Thread threadOne = new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println("threadOne begin sleep for 200 s");
Thread.sleep(200000);
System.out.println("threadOne waking");
} catch (InterruptedException e) {
System.out.println("threadOne is interrupted while sleeping");
return;
}
System.out.println("threadOne-leaving normally");
}
});
threadOne.start();
//确保子线程进入休眠状态
Thread.sleep(1000);
//打断子线程的休眠,让子线程从sleep函数返回
threadOne.interrupt();
//等待子线程执行完毕
threadOne.join();
System.out.println("main thread is over");
//threadOne begin sleep for 200 s
//threadOne is interrupted while sleeping
//main thread is over
// threadOne 线程休眠了200s,在正常情况下该线程需要等到 200s 后才会被唤醒,
// 但是通过调用 threadOne.interrupt()方法打断了该线程的休眠,
// 该线程会在调用 sleep 方法处抛出 InterruptedException 异常后返回
}
}
/**
* interrupted() 与 isInterrupted()
*/
public class InterruptDemo3 {
public static void main(String[] args) throws InterruptedException{
Thread threadOne = new Thread(new Runnable() {
@Override
public void run() {
while (true){}
}
});
threadOne.start();
threadOne.interrupt();
System.out.println("isInterrupted:"+threadOne.isInterrupted());
System.out.println("isInterrupted:"+threadOne.interrupted());
System.out.println("isInterrupted:"+Thread.interrupted());
System.out.println("isInterrupted:"+threadOne.isInterrupted());
threadOne.join();
System.out.println("main thread is over");
//isInterrupted:true
//isInterrupted:false
//isInterrupted:false
//isInterrupted:true
// 在 interrupted()内部是获取当前调用线程的中断标志而不是调用 interrupted()方法的实例对象的中断标志。
// public static boolean interrupted() {
// return currentThread().isInterrupted(true);
// }
//在 interrupted()方法内部是获取当前线程的中断状态,这里虽然调用了 threadOne 的 interrupted() 方法,
//但是获取的是主线程的中断标志,因为主线程是 当前线程。 threadOne.interrupted()和 Thread.interrupted()方法的作用是一样的,
// 目的都是 获取当前线程的中断标志
}
}
/**
* interrupted()
*/
public class InterruptDemo4 {
public static void main(String[] args) throws InterruptedException{
Thread threadOne = new Thread(new Runnable(){
@Override
public void run() {
//中断标志为true时会退出循环,并清除中断标志
while (!Thread.currentThread().interrupted()){}
System.out.println("threadOne interrupted:"+Thread.currentThread().isInterrupted());
}
});
threadOne.start();
threadOne.interrupt();
threadOne.join();
System.out.println("main thread is over");
//threadOne interrupted:false
//main thread is over
}
}
补充案例:
public class InterruptTest01 {
public static void main(String[] args) throws InterruptedException{
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
while (true){
System.out.println("hello");
Thread.yield();
}
}
});
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
while (true){
System.out.println("hello");
if (Thread.currentThread().isInterrupted()){
System.out.println("中断...");
break;
}
Thread.yield();
}
}
});
// thread.start();
// Thread.sleep(2000);
// thread.interrupt();
//hello
//hello
//hello
//hello
//hello
//hello
//hello
//...
//虽然对thread进行了中断,但在thread中没有中断处理逻辑,因此,即使thread被置上了中断状态,也不会发生任何作用
// thread2.start();
// Thread.sleep(1000);
// thread2.interrupt();
//hello
//hello
//hello
//hello
//hello
//hello
//中断...
//在循环体中,出现类似于wait(),sleep()这样的操作,则只能通过中断来识别了
//sleep()会抛出InterruptedException,当线程在sleep时被中断,就会产生这个异常
Thread thread3 = new Thread(new Runnable() {
@Override
public void run() {
while (true){
System.out.println("hello");
if (Thread.currentThread().isInterrupted()){
System.out.println("中断...");
break;
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// e.printStackTrace();
//加处理
System.out.println("处理异常");
Thread.currentThread().interrupt(); //再次中断自己,置上中断标记位
}
Thread.yield();
}
}
});
thread3.start();
Thread.sleep(1000);
thread3.interrupt();
//hello
//hello
//hello
//hello
//hello
//hello
//java.lang.InterruptedException: sleep interrupted
// at java.lang.Thread.sleep(Native Method)
// at com.combat.InterruptTest01$3.run(InterruptTest01.java:63)
// at java.lang.Thread.run(Thread.java:748)
//hello
//hello
//hello
//Thread.sleep()方法由于中断而抛出异常,她会清除中断标记,不加处理,那么下次循环就无法捕获这个中断
//处理后结果
//hello
//hello
//hello
//hello
//hello
//hello
//hello
//hello
//hello
//处理异常
//hello
//中断...
}
}
07.interrupt的更多相关文章
- Samsung_tiny4412(驱动笔记07)----spinlock,semaphore,atomic,mutex,completion,interrupt
/*********************************************************************************** * * spinlock,se ...
- Java多线程系列--“基础篇”09之 interrupt()和线程终止方式
概要 本章,会对线程的interrupt()中断和终止方式进行介绍.涉及到的内容包括:1. interrupt()说明2. 终止线程的方式2.1 终止处于“阻塞状态”的线程2.2 终止处于“运行状态” ...
- u-boot-2016.07 README文档结构
Author:AP0904225版权声明:本文为博主原创文章,转载请标明出处. 阅读u-boot的README文档,可以获取很多有用的信息,例如从哪里可以获得帮助,帮助:u-boot版本命名规则,目录 ...
- Reentrant protected mode kernel using virtual 8086 mode interrupt service routines
A method for allowing a protected mode kernel to service, in virtual 8086 mode, hardware interrupts ...
- IDT系列:(一)初探IDT,Interrupt Descriptor Table,中断描述符表
原文: IDT系列:(一)初探IDT,Interrupt Descriptor Table,中断描述符表 IDT,Interrupt Descriptor Table,中断描述符表是CPU用来处理中 ...
- java中interrupt,interrupted和isInterrupted的区别
文章目录 isInterrupted interrupted interrupt java中interrupt,interrupted和isInterrupted的区别 前面的文章我们讲到了调用int ...
- 第07课:GDB 常用命令详解(下)
本课的核心内容: disassemble 命令 set args 和 show args 命令 tbreak 命令 watch 命令 display 命令 disassemble 命令 当进行一些高级 ...
- Windows内核中的CPU架构-6-中断门(32-Bit Interrupt Gate)
Windows内核中的CPU架构-6-中断门(32-Bit Interrupt Gate) 中断门和调用门类似,也是一种系统段.同样的它也可以用来提权. 中断门: 虽然中断门的段描述符如下: 但是中断 ...
- iOS系列 基础篇 07 Action动作和输出口
iOS系列 基础篇 07 Action动作和输出口 目录: 1. 前言及案例说明 2. 什么是动作? 3. 什么是输出口? 4. 实战 5. 结尾 1. 前言及案例说明 上篇内容我们学习了标签和按钮 ...
随机推荐
- Redis Redis-Cell
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11632679.html 漏斗限流 漏斗限流是最常用的限流方法之一,另一个是令牌桶(比如:Guava R ...
- 【锁】synchronized的实现(偏向锁、轻量级锁、重量级锁)
synchronized的三种应用方式 一. 修饰实例方法,作用于当前实例加锁,进入同步代码前要获得当前实例的锁. 二. 修饰静态方法,作用于当前类对象加锁,进入同步代码前要获得当前类对象的锁. 三. ...
- 如何在MaxCompute上处理存储在OSS上的开源格式数据
0. 前言 MaxCompute作为使用最广泛的大数据平台,内部存储的数据以EB量级计算.巨大的数据存储量以及大规模计算下高性能数据读写的需求,对于MaxCompute提出了各种高要求及挑战.处在大数 ...
- Eternal Victory
题目链接 题意:给出n个点,再给出n-1条路,想一口气从1走完n个点的最小距离. 思路:好像它不构成环!md没看清题目,所以说每次遍历完全部的点后,最短的路就是每条边的距离*2减去最长路的距离. 所以 ...
- 汇编 “error A2031”
assume cs:code code segment main: mov ax,0020h mov ds,ax ;指定段地址0200h mov dx,0h mov cx,003fh s: mov [ ...
- python中的_ElementUnicodeResult是什么
_ElementUnicodeResult在python中是字符串的一种,因为在python3中,字符串就是指以unicode编码规则存储的数据,而以其他方式如utf-8,ASCII编码方式存储的数据 ...
- Nuget-Doc:NuGet 介绍
ylbtech-Nuget-Doc:NuGet 介绍 NuGet 是适用于 .NET 的包管理器. 它使开发人员能够创建.共享和使用有用的 .NET 库. NuGet 客户端工具可生成这些库并将其作为 ...
- Python 进阶_函数式编程
目录 目录 函数式编程 Python 函数式编程的特点 高阶函数 匿名函数 lambda 函数式编程相关的内置函数 filter 序列对象过滤器 map reduce 折叠 自定义的排序函数 最后 函 ...
- 测开之路五十:monggodb安装与初步使用
mongodb下载地址:https://www.mongodb.com/download-center Robo3T下载地址:https://robomongo.org/ 安装mongodb 双击无脑 ...
- webapi返回json格式优化 转载https://www.cnblogs.com/GarsonZhang/p/5322747.html
一.设置webapi返回json格式 在App_Start下的WebApiConfig的注册函数Register中添加下面这代码 1 config.Formatters.Remove(config.F ...