Java Interrupt Related
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的更多相关文章
- 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 ...
- 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 ...
- 《Java学习笔记(第8版)》学习指导
<Java学习笔记(第8版)>学习指导 目录 图书简况 学习指导 第一章 Java平台概论 第二章 从JDK到IDE 第三章 基础语法 第四章 认识对象 第五章 对象封装 第六章 继承与多 ...
- 学习笔记 | java反序列化漏洞分析
java反序列化漏洞是与java相关的漏洞中最常见的一种,也是网络安全工作者关注的重点.在cve中搜索关键字serialized共有174条记录,其中83条与java有关:搜索deserialized ...
- 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 ...
- 多线程 interrupt()方法
java interrupt()方法只是设置线程的中断标记,当对处于阻塞状态的线程调用interrupt方法时(处于阻塞状态的线程是调用sleep, wait, join 的线程),会抛出Interr ...
- 快手4-5月Java岗面经
快手面试准备 我的牛客网帖子链接:https://www.nowcoder.com/discuss/429362 一面: 基础知识 1.java基本数据类型(8种) 1.基本数据类型有哪些,各占多少位 ...
- 译文《最常见的10种Java异常问题》
封面:洛小汐 译者:潘潘 知彼知己,方能百战不殆. 前言 本文总结了有关Java异常的十大常见问题. 目录 检查型异常(checked) vs. 非检查型异常(Unchecked) 异常管理的最佳实践 ...
- Jersey(1.19.1) - Extracting Request Parameters
Parameters of a resource method may be annotated with parameter-based annotations to extract informa ...
随机推荐
- 支付顺序-->微信支付到公司账户-->待出票
支付顺序-->微信支付到公司账户-->待出票-->查询所有待出票订单 -->遍历提交订单-->火车票接口放回订单id-->存入order订单表中 -->读取订 ...
- HTTP Request GET, HEAD, POST, PUT, DELETE, OPTIONS, TRACE Methods
注:本文为个人学习摘录,原文地址为:http://javaeedevelop.iteye.com/blog/1725299 An HTTP request is a class consisting ...
- linux上安装mono发布.net网站步骤
在linux上部署mono 1.自己安装好linux 2.使用桥接方式,让虚拟机和本机在一个局域网内 3.安装apache服务器 4.安装libgdiplug 5.安装mono 6.安装xsp 7.安 ...
- MVC5的控制器,使用HttpPost方式时,接收的参数为null的原因
1.问题现象 POST提交时,控制的Action接收到的参数为null, 但Request.Form.Request.Params等集合其实是包含提交的所有数据的 .如下截图: 2.该问题很诡异,重新 ...
- javascript显式类型的转换
显式类型转换目的:为了使代码变得清晰易读,而做显示类型的转换常使用的函数:Boolean(),String(),Number()或Object()如:Nunber(5) //5String(true) ...
- 模拟SPI协议时序
SPI是串行外设接口总线,摩托罗拉公司开发的一种全双工,同步通信总线,有四线制和三线制. 在单片机系统应用中,单片机常常是被用来当做主机(MASTER),外围器件被当做从机(SLAVE). 所以,在以 ...
- CSS居中方法
css居中方法非常多,根据工作的实际情况采用恰当方法,可取到事半功倍的效果. 就常见的一些居中方法整理如下: 代码如下: <div class="con"> <d ...
- php编译错误:Cannot find OpenSSL's <evp.h>
yum install openssl openssl-devel ln -s /usr/lib64/libssl.so /usr/lib/
- IntelliJ Idea 常用快捷键列表(转)
IntelliJ Idea 常用快捷键列表 Alt+回车 导入包,自动修正Ctrl+N 查找类Ctrl+Shift+N 查找文件Ctrl+Alt+L 格式化代码 Ctrl+Alt+O 优化导 ...
- HttpClient的get和post方式提交数据的使用
/** * Http工具类 */ public class HttpUtil { // 创建HttpClient对象 public static HttpClient httpClient = new ...