In Java, the main process can have several threads at a time, but only a few of them can run concurrently, so it is needed to cancel some thread at times and do the other thread, through interrupt. Interrupt is realized through the InterruptedException, if the other thread interrupt the current running thread, and if the current running thread is running methods like sleep(), which is able to throw InterruptedException, then the method throws an InterruptedException for the try{}catch{InterruptedException e} part of the method. So if a thread is defined to be able to be interrupted, it must be designed so that it invokes methods able to throw InterruptedException periodically or using the following code at times:

 //check if a thread is being trying to interrputed
if( Thread.interrupted() ){
throw new InterruptedException();
}

Note that invoking Thread.interrupt sets the flag Interrupt Status Flag, when Checks for an interrupt by invoking the static method Thread.interrupted, the ISF is reset. Using the non-static isInterrupted method, by one thread to query the interrupt status of another, does not change ISF. By convention, any method that exit by throwing an InterruptedException clears ISF when it does clear the status, however, the ISF might be set up again by another thread invoking interrupt.

Using Joins method to wait for the completion of another. t.join() usage causes the current running process to pause execution and wait till thread t terminates. It has several overloads allowing developer to specify its waiting period. As with sleep. join is dependent on OS for timing, so keep in mind that this method may not wait as long as you specify. The joins() method can also throw InterruptedException.

When Thread.start(), a new thread starts running together with the thread that started it. The accessible thread t stop running when the other thread calls t.interrupt(). This method invokes some certain method running in thread t to throw InterruptedException.

Java Interrupt Related的更多相关文章

  1. Java NIO Related

    A file's status is 3-valued: The file is verified to exist; The file is verified to not exist; The f ...

  2. Java Networking Related (Java Examples in a Nutshell 3rd Edition)

    Examples to: Use URL class to parse URLs and download the network resources specified by a URL Use U ...

  3. 《Java学习笔记(第8版)》学习指导

    <Java学习笔记(第8版)>学习指导 目录 图书简况 学习指导 第一章 Java平台概论 第二章 从JDK到IDE 第三章 基础语法 第四章 认识对象 第五章 对象封装 第六章 继承与多 ...

  4. 学习笔记 | java反序列化漏洞分析

    java反序列化漏洞是与java相关的漏洞中最常见的一种,也是网络安全工作者关注的重点.在cve中搜索关键字serialized共有174条记录,其中83条与java有关:搜索deserialized ...

  5. PatentTips - Enhanced I/O Performance in a Multi-Processor System Via Interrupt Affinity Schemes

    BACKGROUND OF THE INVENTION This relates to Input/Output (I/O) performance in a host system having m ...

  6. 多线程 interrupt()方法

    java interrupt()方法只是设置线程的中断标记,当对处于阻塞状态的线程调用interrupt方法时(处于阻塞状态的线程是调用sleep, wait, join 的线程),会抛出Interr ...

  7. 快手4-5月Java岗面经

    快手面试准备 我的牛客网帖子链接:https://www.nowcoder.com/discuss/429362 一面: 基础知识 1.java基本数据类型(8种) 1.基本数据类型有哪些,各占多少位 ...

  8. 译文《最常见的10种Java异常问题》

    封面:洛小汐 译者:潘潘 知彼知己,方能百战不殆. 前言 本文总结了有关Java异常的十大常见问题. 目录 检查型异常(checked) vs. 非检查型异常(Unchecked) 异常管理的最佳实践 ...

  9. Jersey(1.19.1) - Extracting Request Parameters

    Parameters of a resource method may be annotated with parameter-based annotations to extract informa ...

随机推荐

  1. (转) QImage总结

    嗯,这个QImage的问题研究好久了,有段时间没用,忘了,已经被两次问到了,突然有点解释不清楚,我汗颜,觉得有必要重新总结下了,不然无颜对自己了. 图像的数据是以字节为单位保存的,每一行的字节数必须是 ...

  2. 把嵌入在eclipse中的tomcat日志分离出来

    现象 不知道从哪个版本的tomcat开始,windows版本的tomcat不再包含{tomcat_home}\logs\catalina.out这个文件,eclipse中配置好tomcat服务器之后, ...

  3. Chapter 16_0 面向对象编程

    Lua中的table就是一种对象. 1.table和对象一样拥有状态 2.和对象一样有一个独立的标识符(a self) 3.和对象一样,具有独立于创建者和创建地的生命周期. 对象有他们自己的操作,ta ...

  4. NOIP2010-普及组复赛-第四题-三国游戏

    题目描述 Description 小涵很喜欢电脑游戏,这些天他正在玩一个叫做<三国>的游戏.  在游戏中,小涵和计算机各执一方,组建各自的军队进行对战.游戏中共有 N 位武将(N为偶数且不 ...

  5. asp.net URL DES加密 什在URL中的使用

    DES 加密 有时会有 + = 这些特殊字符串 Server.UrlDecode(Request["UserName"]) //能解决  = 号,但是 +号会变成空格 HttpUt ...

  6. BJFU 1440 fudq的ACM

    矩阵快速幂 #include<cstdio> #include<algorithm> using namespace std; ; const int INF =1e9; ; ...

  7. mac中Eclipse的快捷键

    查看某个类:command + shift +T 快速查看源代码中方法: command + o 选中某个类,command + t:查看此类的父类和子类 如果要导入一个类所在的包名,可以选中这个类, ...

  8. ACE_Event_Handler:事件响应入口

    1:ACE_Event_Handler类 头文件“Event_Handler.h” 在ACE Reactor框架中,ACE_Event_Handler是所有事件处理器的基类.ACE_Event_Han ...

  9. OutputDebugString输出调试信息到debugtrack

    OutPutDebugString()函数的输出则可以用DebugView捕获(DebugView也可以捕获TRACE宏的输出)eg: OutPutDebugString("输出第一调试信息 ...

  10. 在Mac OS X系统下 用dd命令将iso镜像写入u盘

    一. Mac下将ISO写入U盘可使用命令行工具dd,操作如下: 1.找出U盘挂载的路径,使用如下命令:diskutil list2.将U盘unmount(将N替换为挂载路径):diskutil unm ...