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并发编程.当 ...
随机推荐
- EF中 GroupJoin 与 Join
数据: GroupJoin: 返回左表所有数据 using (tempdbEntities context = new tempdbEntities()) { var query = context. ...
- 带你了解MyBatis一二级缓存
在对数据库进行噼里啪啦的查询时,可能存在多次使用相同的SQL语句去查询数据库,并且结果可能还一样,这时,如果不采取一些措施,每次都从数据库查询,会造成一定资源的浪费,所以Mybatis中提供了一级缓存 ...
- Myeclipse下PHP开发环境搭建及运行
外接CSDN链接 http://blog.csdn.net/yuxiangaaaaa/article/details/54948426 这是php初始设置,后面进行重新设置
- 14.浏览器屏幕缩放bug修复
问题:浏览器缩放时,轮播图显示不全,滚动水平滚动条,发现图片缺失 解决:隐藏水平滚动条,页面都只提供垂直滚动条的需求 global.css /* 水平超出部分默认隐藏 */ #app { overfl ...
- 后端——框架——持久层框架——Mybatis——《Mybatis从入门到精通》读书笔记——初篇
1.Mybatis知识点 框架的知识点大致可以分为三个部分 基础: 介绍编写增,删,改,查: 动态标签: config配置文件 Mapper配置文件 插件:常见的插件有三个 pageHelper:分页 ...
- php海量架构
架构 Varnish+nginx+php(FastCGI)+MYSQL5+MenCache+MenCachedb 说明:我在设计系统架构时,进行了大胆的尝试,只用6台Web服务器,达到了可承受4000 ...
- leetCode练题——21. Merge Two Sorted Lists(照搬大神做法)
1.题目 21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new l ...
- iOS中常用的手势
--前言 智能手机问世后的很长一段时间,各大手机厂商都在思考着智能手机应该怎么玩?也都在尝试着制定自己的一套操作方式.直到2007年乔布斯发布了iPhone手机,人们才认识到智能手机就应该这样玩. 真 ...
- Plastic Sprayer Supplier - Sprayer: How Can The Product Be Sprayed?
In many end products, especially in cosmetics, the first thing that appeals to consumers is the form ...
- js学习:函数
概述 函数的声明 JavaScript 有三种声明函数的方法 function 命令 function命令声明的代码区块,就是一个函数.function命令后面是函数名,函数名后面是一对圆括号,里面是 ...