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并发编程.当 ...
随机推荐
- c++开源库rapidxm
1.引入头文件: 一般用到的头文件: #include "rapidxml/rapidxml.hpp" #include "rapidxml/rapidxml_utils ...
- 基于科大讯飞AIUI平台自定义语义库的开发
说明:我写这篇文章的主要目的是因为我在做这块的时候遇到过一些坑,也是希望后来者能少走一些弯路. 科大讯飞AIUI开放平台地址 科大讯飞AIUI开放平台后处理地址 AIUI后处理开放平台协议 1. 科大 ...
- duv中内容不换行的解决办法
<div style='width: 100px;display:block;word-break: break-all;word-wrap: break-word;'> 内容超出div宽 ...
- 【代码学习】PYTHON 文件I/O
一.文件的打开和关闭 open(文件名,访问模式) cloese() 模式 描述 r 以只读方式打开文件.文件的指针将会放在文件的开头.这是默认模式. rb 以二进制格式打开一个文件用于只读.文件指针 ...
- Beego Learning Notes
Beego框架学习 1.1软件框架 一个公司是由公司中的各部部门来组成的,每一个部门拥有特定的职能,部门与部门之间通过相互的配合来完成让公司运转起来. 一个软件框架是由其中各个软件模块组成的,每一个模 ...
- spring boot中配置文件中变量的引用
配置文件中 变量的自身引用 ${名称} java文件中引用:非静态变量 之间在变量上面注释@Value("${名称}") 静态变量 在set方法上注释@Value("$ ...
- VUE项目开发中使用WebSocket
初始化WebSocket initWebSocket(){ //初始化weosocket const wsuri = 'ws://10.100.45.8:8888/websocket';//ws地址 ...
- vue.js 第九课
这次讲红色框框. 方法与事件处理器: 方法处理器: 内联语句处理器: 事件修饰符: 按键修饰符: 为什么在HTML中监听事件? 1.v-on绑定事件 带参数 2.事件对象$event: 有时也需要调用 ...
- Vue系列(六)之常用指令v-model
v-model 基本使用 修饰符 .trim .number .lazy 前面讲到的插值,其实都是单向绑定,数据变-->视图变.有些元素可以与用户交互,比如input,select等,那么我们希 ...
- SpringBoot与Mybatis整合,插件生成dao、mapper、pojo
一.创建SpringBoot项目,引入相关依赖包 <?xml version="1.0" encoding="UTF-8"?> <projec ...