java并发:interrupt进程终止
interrupt进程终止
interrupt()源码
/**
* Interrupts this thread.
*
* <p> Unless the current thread is interrupting itself, which is
* always permitted, the {@link #checkAccess() checkAccess} method
* of this thread is invoked, which may cause a {@link
* SecurityException} to be thrown.
*
* <p> If this thread is blocked in an invocation of the {@link
* Object#wait() wait()}, {@link Object#wait(long) wait(long)}, or {@link
* Object#wait(long, int) wait(long, int)} methods of the {@link Object}
* class, or of the {@link #join()}, {@link #join(long)}, {@link
* #join(long, int)}, {@link #sleep(long)}, or {@link #sleep(long, int)},
* methods of this class, then its interrupt status will be cleared and it
* will receive an {@link InterruptedException}.
*
* <p> If this thread is blocked in an I/O operation upon an {@link
* java.nio.channels.InterruptibleChannel InterruptibleChannel}
* then the channel will be closed, the thread's interrupt
* status will be set, and the thread will receive a {@link
* java.nio.channels.ClosedByInterruptException}.
*
* <p> If this thread is blocked in a {@link java.nio.channels.Selector}
* then the thread's interrupt status will be set and it will return
* immediately from the selection operation, possibly with a non-zero
* value, just as if the selector's {@link
* java.nio.channels.Selector#wakeup wakeup} method were invoked.
*
* <p> If none of the previous conditions hold then this thread's interrupt
* status will be set. </p>
*
* <p> Interrupting a thread that is not alive need not have any effect.
*
* @throws SecurityException
* if the current thread cannot modify this thread
*
* @revised 6.0
* @spec JSR-51
*/
public void interrupt() {
if (this != Thread.currentThread())
checkAccess();
synchronized (blockerLock) {
Interruptible b = blocker;
if (b != null) {
interrupt0(); // Just to set the interrupt flag
b.interrupt(this);
return;
}
}
interrupt0();
}
我主要强调一点当线程处于阻塞状态的时候,调用interrupt(),interrupt status 状态会被clear,从true再次变为false。所以对于通过InterruptedException异常
来中断需要正确的try catch语句。
正确的阻塞线程中断方式
package com.java.javabase.thread.base;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class InterruptTest2 {
public static void main(String[] args) {
InterruptTest2 test = new InterruptTest2();
Thread t1 = test.new ThreadOne("t1");
t1.start();
try {
Thread.sleep(2000);
t1.interrupt();
log.info("Thread t1 state :{} ", t1.getState());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
class ThreadOne extends Thread {
public ThreadOne(String name) {
super(name);
}
@Override
public void run() {
int i = 0;
try {
while (!interrupted()) {
Thread.sleep(500);
log.info("Thread {} state :{} ,run {} times", Thread.currentThread().getName(),
Thread.currentThread().getState(), i++);
}
} catch (InterruptedException e) {
log.info("Thread {} state :{} ,run {} times", Thread.currentThread().getName(),
Thread.currentThread().getState(), i++);
e.printStackTrace();
}
}
}
}
返回结果
2019-07-30 19:52:40,496 [t1] INFO InterruptTest2 - Thread t1 state :RUNNABLE ,run 0 times
2019-07-30 19:52:41,000 [t1] INFO InterruptTest2 - Thread t1 state :RUNNABLE ,run 1 times
2019-07-30 19:52:41,500 [t1] INFO InterruptTest2 - Thread t1 state :RUNNABLE ,run 2 times
2019-07-30 19:52:41,991 [main] INFO InterruptTest2 - Thread t1 state :TIMED_WAITING
2019-07-30 19:52:41,991 [t1] INFO InterruptTest2 - Thread t1 state :RUNNABLE ,run 3 times
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at com.java.javabase.thread.base.InterruptTest2$ThreadOne.run(InterruptTest2.java:36)
说明
while语句在try catch 捕获到InterruptedException异常,就可以处理。
错误的中断阻塞线程例子
package com.java.javabase.thread.base;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class InterruptTest {
public static void main(String[] args) {
InterruptTest test = new InterruptTest();
Thread t1 = test.new ThreadOne("t1");
t1.start();
try {
Thread.sleep(2000);
t1.interrupt();
log.info("Thread state :{} ", t1.getState());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
class ThreadOne extends Thread {
public ThreadOne(String name) {
super(name);
}
@Override
public void run() {
int i = 0;
while (!interrupted()) {
try {
Thread.sleep(500);
log.info("Thread {} state :{} ,run {} times", Thread.currentThread().getName(),
Thread.currentThread().getState(), i++);
} catch (InterruptedException e) {
log.info("Thread {} state :{} ,run {} times", Thread.currentThread().getName(),
Thread.currentThread().getState(), i++);
e.printStackTrace();
}
}
}
}
}
测试结果
2019-07-30 19:54:40,189 [t1] INFO InterruptTest - Thread t1 state :RUNNABLE ,run 0 times
2019-07-30 19:54:40,691 [t1] INFO InterruptTest - Thread t1 state :RUNNABLE ,run 1 times
2019-07-30 19:54:41,192 [t1] INFO InterruptTest - Thread t1 state :RUNNABLE ,run 2 times
2019-07-30 19:54:41,686 [t1] INFO InterruptTest - Thread t1 state :RUNNABLE ,run 3 times
2019-07-30 19:54:41,686 [main] INFO InterruptTest - Thread state :TIMED_WAITING
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at com.java.javabase.thread.base.InterruptTest$ThreadOne.run(InterruptTest.java:35)
2019-07-30 19:54:42,187 [t1] INFO InterruptTest - Thread t1 state :RUNNABLE ,run 4 times
2019-07-30 19:54:42,687 [t1] INFO InterruptTest - Thread t1 state :RUNNABLE ,run 5 times
2019-07-30 19:54:43,187 [t1] INFO InterruptTest - Thread t1 state :RUNNABLE ,run 6 times
2019-07-30 19:54:43,687 [t1] INFO InterruptTest - Thread t1 state :RUNNABLE ,run 7 times
2019-07-30 19:54:44,187 [t1] INFO InterruptTest - Thread t1 state :RUNNABLE ,run 8 times
2019-07-30 19:54:44,687 [t1] INFO InterruptTest - Thread t1 state :RUNNABLE ,run 9 times
2019-07-30 19:54:45,187 [t1] INFO InterruptTest - Thread t1 state :RUNNABLE ,run 10 times
错误结果说明
try catch在while语句之内, 捕获到InterruptedException异常,while的!interrupted()会再次返回true
运行态的线程的终止
package com.java.javabase.thread.base;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class InterruptTest3 {
private boolean stopFlag = false;
private void stoptask() {
this.stopFlag = true;
}
public static void main(String[] args) {
InterruptTest3 test = new InterruptTest3();
Thread t1 = test.new ThreadOne("t1");
t1.start();
try {
Thread.sleep(5000);
test.stoptask();
log.info("Thread t1 state :{} ", t1.getState());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
class ThreadOne extends Thread {
public ThreadOne(String name) {
super(name);
}
int i = 0;
@Override
public void run() {
while (!stopFlag) {
try {
sleep(1000);
log.info("Thread {} state :{} ,run {} times", Thread.currentThread().getName(),
Thread.currentThread().getState(), i++);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
测试结果
2019-07-30 20:01:19,922 [t1] INFO InterruptTest3 - Thread t1 state :RUNNABLE ,run 0 times
2019-07-30 20:01:20,923 [t1] INFO InterruptTest3 - Thread t1 state :RUNNABLE ,run 1 times
2019-07-30 20:01:21,923 [t1] INFO InterruptTest3 - Thread t1 state :RUNNABLE ,run 2 times
2019-07-30 20:01:22,923 [t1] INFO InterruptTest3 - Thread t1 state :RUNNABLE ,run 3 times
2019-07-30 20:01:23,918 [main] INFO InterruptTest3 - Thread t1 state :TIMED_WAITING
2019-07-30 20:01:23,923 [t1] INFO InterruptTest3 - Thread t1 state :RUNNABLE ,run 4 times
java并发:interrupt进程终止的更多相关文章
- Java并发编程:进程和线程的由来(转)
Java多线程基础:进程和线程之由来 在前面,已经介绍了Java的基础知识,现在我们来讨论一点稍微难一点的问题:Java并发编程.当然,Java并发编程涉及到很多方面的内容,不是一朝一夕就能够融会贯通 ...
- Java 并发:线程中断-interrupt
一直以为执行了interrupt方法就可以让线程结束,并抛出InterruptedException. 今天看了Java并发编程实战的第七章发现并不是这么回事,在这章的开头就提到 要使任务和线程能安全 ...
- Java并发编程:进程和线程之由来
Java多线程基础:进程和线程之由来 在前面,已经介绍了Java的基础知识,现在我们来讨论一点稍微难一点的问题:Java并发编程.当然,Java并发编程涉及到很多方面的内容,不是一朝一夕就能够融会贯通 ...
- java并发编程:进程和线程
java并发编程涉及到很多内容,当然也包括多线程,再次补充点相关概念 原文地址:http://www.cnblogs.com/dolphin0520/p/3910667.html 一.操作系统中为什么 ...
- Java并发编程:进程和线程之由来__进程让操作系统的并发性成为可能,而线程让进程的内部并发成为可能
转载自海子:http://www.cnblogs.com/dolphin0520/p/3910667.html Java多线程基础:进程和线程之由来 在前面,已经介绍了Java的基础知识,现在我们来讨 ...
- Java并发编程:线程和进程的创建(转)
Java并发编程:如何创建线程? 在前面一篇文章中已经讲述了在进程和线程的由来,今天就来讲一下在Java中如何创建线程,让线程去执行一个子任务.下面先讲述一下Java中的应用程序和进程相关的概念知识, ...
- Java并发基础:进程和线程之由来
转载自:http://www.cnblogs.com/dolphin0520/p/3910667.html 在前面,已经介绍了Java的基础知识,现在我们来讨论一点稍微难一点的问题:Java并发编程. ...
- Java并发编程之线程生命周期、守护线程、优先级、关闭和join、sleep、yield、interrupt
Java并发编程中,其中一个难点是对线程生命周期的理解,和多种线程控制方法.线程沟通方法的灵活运用.这些方法和概念之间彼此联系紧密,共同构成了Java并发编程基石之一. Java线程的生命周期 Jav ...
- Java并发编程(一):进程和线程之由来
转自:http://www.cnblogs.com/dolphin0520/p/3910667.html 在前面,已经介绍了Java的基础知识,现在我们来讨论一点稍微难一点的问题:Java并发编程.当 ...
随机推荐
- VMware升级到15版本虚拟机黑屏的解决方法
1.启动VMware15虚拟机,在菜单栏找到:虚拟机→管理→更改硬件兼容性 2.打开该项,弹出更改硬件兼容性向导对话框,点 下一步,接下来把硬件兼容性改为Workstation 12.x 3.根据提 ...
- Codeforces Round #611 (Div. 3) E
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past ...
- Update(stage3):第1节 redis组件:8、主从复制架构;9、Sentinel架构
8.redis的主从复制架构 在Redis中,用户可以通过执行SLAVEOF命令或者设置slaveof选项,让一个服务器去复制(replicate)另一个服务器,我们称呼被复制的服务器为主服务器(ma ...
- 【协作式原创】自己动手写docker之run代码解析
linux预备知识 urfave cli预备知识 准备工作 阿里云抢占式实例:centos7.4 每次实例释放后都要重新安装go wget https://dl.google.com/go/go1.1 ...
- Solr搜索引擎服务器学习笔记
Solr简介 采用Java5开发,基于Lucene的全文搜索服务器.同时对其进行了扩展,提供了比Lucene更为丰富的查询语言,同时实现了可配置.可扩展并对查询性能进行了优化,并且提供了一个完善的功能 ...
- 用Struts2框架报错:The Struts dispatcher cannot be found
报错信息 The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the ...
- if,while,for循环
目录 if条件 while循环 for循环 拓展知识点 if条件 if 条件: code elif 条件: code else: code # 三元运算符 x = 10 y = 20 print(y ...
- 【剑指Offer面试编程题】题目1361:翻转单词顺序--九度OJ
题目描述: JOBDU最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上.同事Cat对Fish写的内容颇感兴趣,有一天他向Fish借来翻看,但却读不懂它的意思.例如,&quo ...
- 【转载】手把手教你使用Git(简单,实用)
手把手教你使用Git(简单,实用) 标签: git 2016年04月21日 20:51:45 1328人阅读 评论(0) 收藏 举报 一:Git是什么? Git是目前世界上最先进的分布式版本控制系统. ...
- Chrome 浏览器新功能:共享剪贴板
导读 Chrome 79 在桌面版和 Android 版浏览器中添加了一项新的功能,名为“共享剪贴板”(shared clipboard). 简单来说,就是可以实现在电脑端复制,手机端粘贴.有了这项功 ...