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 ...
随机推荐
- 浙江大学 pat 题解---58
1058. A+B in Hogwarts (20) 时间限制 50 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue If you ...
- 让 idea webstorm phpstorm 能够 识别 thinkphp 的方法(自动提示功能)
1.在/ThinkPHP/Library/Think 目录下 新建一个文件,名为: BaseController.class.php 2.BaseController.class.php 内容为 n ...
- poj1256(全排列stl)
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;bool cmp ...
- Spring的Resource
通过Spring Resource接口获取资源(取自http://haohaoxuexi.iteye.com/blog/2016305)目录1 Resource简介2 通过ResourceLoader ...
- Sitemesh 3
Sitemesh 3 的使用及配置(收藏自:http://www.cnblogs.com/luotaoyeah/p/3776879.html) 1 . Sitemesh 3 简介 Sitemesh 是 ...
- mac中Eclipse的快捷键
查看某个类:command + shift +T 快速查看源代码中方法: command + o 选中某个类,command + t:查看此类的父类和子类 如果要导入一个类所在的包名,可以选中这个类, ...
- Servlet、Filter、Listener、Interceptor基础
第一:Servlet Servlet是个接口,全限定名是javax.servlet.Servlet,在javax.servlet包中,在servlet-api.jar(在tomcat自带的lib文件夹 ...
- List循环与Map循环的总结
做了一下list和map的总结,没有什么技术含量,就全当复习了一下api. 测试环境是在junit4下,如果没有自己写一个main方法也是一样的. 首先是List的三种循环: @Test public ...
- [转载]Linux 环境下编译 0.11版本内核 kernel
最近在看<.如果Clobber/Modify 为空,则其前面的冒号(:)必须省略. 2.如果Output,Input,Clobber/Modify都为空,Output,Input之前的冒号(:) ...
- 图片(img标签)大小自适应
$(function(){ var myimg,oldwidth,oldheight; var maxwidth=249; var maxheight=187; var imgs = document ...