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. HDU 2952 Counting Sheep(DFS)

    题目链接 Problem Description A while ago I had trouble sleeping. I used to lie awake, staring at the cei ...

  2. 此 ObjectContext 实例已释放,不可再用于需要连接的操作

    EF 查询包含导航属性的Model,经常会遇到 此 ObjectContext 实例已释放,不可再用于需要连接的操作 这种错误,解决方法如下: db.T_DailyRecord.Include(&qu ...

  3. chapter8_3 c代码和错误

    1.C代码 Lua提供的所有关于动态链接的功能都集中在一个函数中,即package.loadlib. 该函数有两个字符串参数:动态库的完整路径和一个函数名称: local path = "/ ...

  4. Linux下好用的简单实用命令

    1.你是否为在输入了一大串命令之后发现第一个字符打错了而苦恼?只能删除重来嘛?或者一步步左移光标? NO,一个组合键轻松搞定 Ctrl+A -----到命令行首 Ctrl+E ------到命令行末 ...

  5. 字符串长度截取换行/n

    /// <summary>        /// 格式化字符串长度,超出部分显示省略号,区分汉字跟字母.汉字2个字节,字母数字一个字节        /// </summary> ...

  6. linux 内核协议栈收报流程(二)Netfilter全貌

    ip层分片整理 int ip_local_deliver(struct sk_buff *skb){ /* * Reassemble IP fragments. */ if (ip_is_fragme ...

  7. windows禅道环境搭建

    zentao官网的几个网址 http://www.zentao.net/ http://www.zentao.net/article-view-79863.html   搭建环境需要下载两个文件 1) ...

  8. (译)UEFI 启动:实际工作原理

    本文是我翻译自国外技术博客的一篇文章,其中讲述了 UEFI 的一些基本概念和细节. 本文的原始链接位于: https://www.happyassassin.net/2014/01/25/uefi-b ...

  9. Android中调用系统的相机和图库获取图片

    //--------我的主布局文件------很简单---------------------------------<LinearLayout xmlns:android="http ...

  10. NEU OJ 1644 Median I

    优先级队列 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...