上周五和周末,工作忙里偷闲,在看java cocurrent中也顺便再温故了一下Thread.interrupt和java 5之后的LockSupport的实现。

在介绍之前,先抛几个问题。

  1. Thread.interrupt()方法和InterruptedException异常的关系?是由interrupt触发产生了InterruptedException异常?
  2. Thread.interrupt()会中断线程什么状态的工作? RUNNING or BLOCKING?
  3. 一般Thread编程需要关注interrupt中断不?一般怎么处理?可以用来做什么?
  4. LockSupport.park()和unpark(),与object.wait()和notify()的区别?
  5. LockSupport.park(Object blocker)传递的blocker对象做什么用?
  6. LockSupport能响应Thread.interrupt()事件不?会抛出InterruptedException异常?
  7. Thread.interrupt()处理是否有对应的回调函数?类似于钩子调用?
如果你都都能很明确的答上来了,说明你已经完全懂Thread.interrupt,可以不用往下看那了。
那如果不清楚的,带着这几个问题,一起来梳理下。
Thread的interrupt处理的几个方法:
  • public void interrupt() :  执行线程interrupt事件
  • public boolean isInterrupted() : 检查当前线程是否处于interrupt
  • public static boolean interrupted() : check当前线程是否处于interrupt,并重置interrupt信息。类似于resetAndGet()
理解:
1. 每个线程都有一个interrupt status标志位,用于表明当前线程是否处于中断状态
2. 一般调用Thread.interrupt()会有两种处理方式
  • 遇到一个低优先级的block状态时,比如object.wait(),object.sleep(),object.join()。它会立马触发一个unblock解除阻塞,并throw一个InterruptedException。
  • 其他情况,Thread.interrupt()仅仅只是更新了status标志位。然后你的工作线程通过Thread.isInterrrupted()进行检查,可以做相应的处理,比如也throw InterruptedException或者是清理状态,取消task等。
在interrupt javadoc中描述:

最佳实践
IBM上有篇文章写的挺不错。Java theory and practice: Dealing with InterruptedException , 里面提到了Interrupt处理的几条最佳实践。
  1. Don't swallow interrupts (别吃掉Interrupt,一般是两种处理:  继续throw InterruptedException异常。  另一种就是继续设置Thread.interupt()异常标志位,让更上一层去进行相应处理。

    Java代码  
    1. public class TaskRunner implements Runnable {
    2. private BlockingQueue<Task> queue;
    3. public TaskRunner(BlockingQueue<Task> queue) {
    4. this.queue = queue;
    5. }
    6. public void run() {
    7. try {
    8. while (true) {
    9. Task task = queue.take(10, TimeUnit.SECONDS);
    10. task.execute();
    11. }
    12. }
    13. catch (InterruptedException e) {
    14. // Restore the interrupted status
    15. Thread.currentThread().interrupt();
    16. }
    17. }
    18. }
  2. Implementing cancelable tasks with Interrupt (使用Thread.interrupt()来设计和支持可被cancel的task)
    Java代码  
    1. public class PrimeProducer extends Thread {
    2. private final BlockingQueue<BigInteger> queue;
    3. PrimeProducer(BlockingQueue<BigInteger> queue) {
    4. this.queue = queue;
    5. }
    6. public void run() {
    7. try {
    8. BigInteger p = BigInteger.ONE;
    9. while (!Thread.currentThread().isInterrupted())
    10. queue.put(p = p.nextProbablePrime());
    11. } catch (InterruptedException consumed) {
    12. /* Allow thread to exit */
    13. }
    14. }
    15. public void cancel() { interrupt(); } // 发起中断
    16. }<span style="white-space: normal;"> </span>

注册Interrupt处理事件(非正常用法)

一般正常的task设计用来处理cancel,都是采用主动轮询的方式检查Thread.isInterrupt(),对业务本身存在一定的嵌入性,还有就是存在延迟,你得等到下一个检查点(谁知道下一个检查点是在什么时候,特别是进行一个socket.read时,遇到过一个HttpClient超时的问题)。

来看一下,主动抛出InterruptedException异常的实现,借鉴于InterruptibleChannel的设计,比较取巧。

Java代码  
  1. interface InterruptAble { // 定义可中断的接口
  2. public void interrupt() throws InterruptedException;
  3. }
  4. abstract class InterruptSupport implements InterruptAble {
  5. private volatile boolean interrupted = false;
  6. private Interruptible    interruptor = new Interruptible() {
  7. public void interrupt() {
  8. interrupted = true;
  9. InterruptSupport.this.interrupt(); // 位置3
  10. }
  11. };
  12. public final boolean execute() throws InterruptedException {
  13. try {
  14. blockedOn(interruptor); // 位置1
  15. if (Thread.currentThread().isInterrupted()) { // 立马被interrupted
  16. interruptor.interrupt();
  17. }
  18. // 执行业务代码
  19. bussiness();
  20. } finally {
  21. blockedOn(null);   // 位置2
  22. }
  23. return interrupted;
  24. }
  25. public abstract void bussiness() ;
  26. public abstract void interrupt();
  27. // -- sun.misc.SharedSecrets --
  28. static void blockedOn(Interruptible intr) { // package-private
  29. sun.misc.SharedSecrets.getJavaLangAccess().blockedOn(Thread.currentThread(), intr);
  30. }
  31. }

代码说明,几个取巧的点:

位置1:利用sun提供的blockedOn方法,绑定对应的Interruptible事件处理钩子到指定的Thread上。

位置2:执行完代码后,清空钩子。避免使用连接池时,对下一个Thread处理事件的影响。

位置3:定义了Interruptible事件钩子的处理方法,回调InterruptSupport.this.interrupt()方法,子类可以集成实现自己的业务逻辑,比如sock流关闭等等。

使用:

Java代码  
  1. class InterruptRead extends InterruptSupport {
  2. private FileInputStream in;
  3. @Override
  4. public void bussiness() {
  5. File file = new File("/dev/urandom"); // 读取linux黑洞,永远读不完
  6. try {
  7. in = new FileInputStream(file);
  8. byte[] bytes = new byte[1024];
  9. while (in.read(bytes, 0, 1024) > 0) {
  10. // Thread.sleep(100);
  11. // if (Thread.interrupted()) {// 以前的Interrupt检查方式
  12. // throw new InterruptedException("");
  13. // }
  14. }
  15. } catch (Exception e) {
  16. throw new RuntimeException(e);
  17. }
  18. }
  19. public FileInputStream getIn() {
  20. return in;
  21. }
  22. @Override
  23. public void interrupt() {
  24. try {
  25. in.getChannel().close();
  26. } catch (IOException e) {
  27. e.printStackTrace();
  28. }
  29. }
  30. }
  31. public static void main(String args[]) throws Exception {
  32. final InterruptRead test = new InterruptRead();
  33. Thread t = new Thread() {
  34. @Override
  35. public void run() {
  36. long start = System.currentTimeMillis();
  37. try {
  38. System.out.println("InterruptRead start!");
  39. test.execute();
  40. } catch (InterruptedException e) {
  41. System.out.println("InterruptRead end! cost time : " + (System.currentTimeMillis() - start));
  42. e.printStackTrace();
  43. }
  44. }
  45. };
  46. t.start();
  47. // 先让Read执行3秒
  48. Thread.sleep(3000);
  49. // 发出interrupt中断
  50. t.interrupt();
  51. }

jdk源码介绍:

1. sun提供的钩子可以查看System的相关代码, line : 1125

System代码  
  1. sun.misc.SharedSecrets.setJavaLangAccess(new sun.misc.JavaLangAccess(){
  2. public sun.reflect.ConstantPool getConstantPool(Class klass) {
  3. return klass.getConstantPool();
  4. }
  5. public void setAnnotationType(Class klass, AnnotationType type) {
  6. klass.setAnnotationType(type);
  7. }
  8. public AnnotationType getAnnotationType(Class klass) {
  9. return klass.getAnnotationType();
  10. }
  11. public <E extends Enum<E>>
  12. E[] getEnumConstantsShared(Class<E> klass) {
  13. return klass.getEnumConstantsShared();
  14. }
  15. public void blockedOn(Thread t, Interruptible b) {
  16. t.blockedOn(b);
  17. }
  18. });

2. Thread.interrupt()

Java代码  
  1. public void interrupt() {
  2. if (this != Thread.currentThread())
  3. checkAccess();
  4. synchronized (blockerLock) {
  5. Interruptible b = blocker;
  6. if (b != null) {
  7. interrupt0();       // Just to set the interrupt flag
  8. b.interrupt(); //回调钩子
  9. return;
  10. }
  11. }
  12. interrupt0();
  13. }

更多

更多关于Thread.stop,suspend,resume,interrupt的使用注意点,可以看一下sun的文档,比如http://download.oracle.com/javase/6/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html

最后来解答一下之前的几个问题:

问题1: Thread.interrupt()方法和InterruptedException异常的关系?是由interrupt触发产生了InterruptedException异常?

答: Thread.interrupt()只是在Object.wait() .Object.join(), Object.sleep()几个方法会主动抛出InterruptedException异常。而在其他的的block常见,只是通过设置了Thread的一个标志位信息,需要程序自我进行处理。

Java代码  
  1. if (Thread.interrupted())  // Clears interrupted status!
  2. throw new InterruptedException();

问题2:Thread.interrupt()会中断线程什么状态的工作? RUNNING or BLOCKING?

答:Thread.interrupt设计的目的主要是用于处理线程处于block状态,比如wait(),sleep()状态就是个例子。但可以在程序设计时为支持task cancel,同样可以支持RUNNING状态。比如Object.join()和一些支持interrupt的一些nio channel设计。

问题3: 一般Thread编程需要关注interrupt中断不?一般怎么处理?可以用来做什么?

答: interrupt用途: unBlock操作,支持任务cancel, 数据清理等。

问题4: LockSupport.park()和unpark(),与object.wait()和notify()的区别?

答:

1.  面向的主体不一样。LockSuport主要是针对Thread进进行阻塞处理,可以指定阻塞队列的目标对象,每次可以指定具体的线程唤醒。Object.wait()是以对象为纬度,阻塞当前的线程和唤醒单个(随机)或者所有线程。

2.  实现机制不同。虽然LockSuport可以指定monitor的object对象,但和object.wait(),两者的阻塞队列并不交叉。可以看下测试例子。object.notifyAll()不能唤醒LockSupport的阻塞Thread.

问题5: LockSupport.park(Object blocker)传递的blocker对象做什么用?

答: 对应的blcoker会记录在Thread的一个parkBlocker属性中,通过jstack命令可以非常方便的监控具体的阻塞对象.

Java代码  
  1. public static void park(Object blocker) {
  2. Thread t = Thread.currentThread();
  3. setBlocker(t, blocker); // 设置Thread.parkBlocker属性的值
  4. unsafe.park(false, 0L);
  5. setBlocker(t, null);  // 清除Thread.parkBlocker属性的值
  6. }

具体LockSupport的javadoc描述也比较清楚,可以看下:

问题6: LockSupport能响应Thread.interrupt()事件不?会抛出InterruptedException异常?

答:能响应interrupt事件,但不会抛出InterruptedException异常。针对LockSupport对Thread.interrupte支持,也先看一下javadoc中的描述:

相关测试代码

Java代码  
  1. package com.agapple.cocurrent;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.lang.reflect.Field;
  5. import java.util.concurrent.TimeUnit;
  6. import java.util.concurrent.locks.LockSupport;
  7. public class LockSupportTest {
  8. private static LockSupportTest blocker = new LockSupportTest();
  9. public static void main(String args[]) throws Exception {
  10. lockSupportTest();
  11. parkTest();
  12. interruptParkTest();
  13. interruptSleepTest();
  14. interruptWaitTest();
  15. }
  16. /**
  17. * LockSupport.park对象后,尝试获取Thread.blocker对象,调用其single唤醒
  18. *
  19. * @throws Exception
  20. */
  21. private static void lockSupportTest() throws Exception {
  22. Thread t = doTest(new TestCallBack() {
  23. @Override
  24. public void callback() throws Exception {
  25. // 尝试sleep 5s
  26. System.out.println("blocker");
  27. LockSupport.park(blocker);
  28. System.out.println("wakeup now!");
  29. }
  30. @Override
  31. public String getName() {
  32. return "lockSupportTest";
  33. }
  34. });
  35. t.start(); // 启动读取线程
  36. Thread.sleep(150);
  37. synchronized (blocker) {
  38. Field field = Thread.class.getDeclaredField("parkBlocker");
  39. field.setAccessible(true);
  40. Object fBlocker = field.get(t);
  41. System.out.println(blocker == fBlocker);
  42. Thread.sleep(100);
  43. System.out.println("notifyAll");
  44. blocker.notifyAll();
  45. }
  46. }
  47. /**
  48. * 尝试去中断一个object.wait(),会抛出对应的InterruptedException异常
  49. *
  50. * @throws InterruptedException
  51. */
  52. private static void interruptWaitTest() throws InterruptedException {
  53. final Object obj = new Object();
  54. Thread t = doTest(new TestCallBack() {
  55. @Override
  56. public void callback() throws Exception {
  57. // 尝试sleep 5s
  58. obj.wait();
  59. System.out.println("wakeup now!");
  60. }
  61. @Override
  62. public String getName() {
  63. return "interruptWaitTest";
  64. }
  65. });
  66. t.start(); // 启动读取线程
  67. Thread.sleep(2000);
  68. t.interrupt(); // 检查下在park时,是否响应中断
  69. }
  70. /**
  71. * 尝试去中断一个Thread.sleep(),会抛出对应的InterruptedException异常
  72. *
  73. * @throws InterruptedException
  74. */
  75. private static void interruptSleepTest() throws InterruptedException {
  76. Thread t = doTest(new TestCallBack() {
  77. @Override
  78. public void callback() throws Exception {
  79. // 尝试sleep 5s
  80. Thread.sleep(5000);
  81. System.out.println("wakeup now!");
  82. }
  83. @Override
  84. public String getName() {
  85. return "interruptSleepTest";
  86. }
  87. });
  88. t.start(); // 启动读取线程
  89. Thread.sleep(2000);
  90. t.interrupt(); // 检查下在park时,是否响应中断
  91. }
  92. /**
  93. * 尝试去中断一个LockSupport.park(),会有响应但不会抛出InterruptedException异常
  94. *
  95. * @throws InterruptedException
  96. */
  97. private static void interruptParkTest() throws InterruptedException {
  98. Thread t = doTest(new TestCallBack() {
  99. @Override
  100. public void callback() {
  101. // 尝试去park 自己线程
  102. LockSupport.parkNanos(blocker, TimeUnit.SECONDS.toNanos(5));
  103. System.out.println("wakeup now!");
  104. }
  105. @Override
  106. public String getName() {
  107. return "interruptParkTest";
  108. }
  109. });
  110. t.start(); // 启动读取线程
  111. Thread.sleep(2000);
  112. t.interrupt(); // 检查下在park时,是否响应中断
  113. }
  114. /**
  115. * 尝试去中断一个LockSupport.unPark(),会有响应
  116. *
  117. * @throws InterruptedException
  118. */
  119. private static void parkTest() throws InterruptedException {
  120. Thread t = doTest(new TestCallBack() {
  121. @Override
  122. public void callback() {
  123. // 尝试去park 自己线程
  124. LockSupport.park(blocker);
  125. System.out.println("wakeup now!");
  126. }
  127. @Override
  128. public String getName() {
  129. return "parkTest";
  130. }
  131. });
  132. t.start(); // 启动读取线程
  133. Thread.sleep(2000);
  134. LockSupport.unpark(t);
  135. t.interrupt();
  136. }
  137. public static Thread doTest(final TestCallBack call) {
  138. return new Thread() {
  139. @Override
  140. public void run() {
  141. File file = new File("/dev/urandom"); // 读取linux黑洞
  142. try {
  143. FileInputStream in = new FileInputStream(file);
  144. byte[] bytes = new byte[1024];
  145. while (in.read(bytes, 0, 1024) > 0) {
  146. if (Thread.interrupted()) {
  147. throw new InterruptedException("");
  148. }
  149. System.out.println(bytes[0]);
  150. Thread.sleep(100);
  151. long start = System.currentTimeMillis();
  152. call.callback();
  153. System.out.println(call.getName() + " callback finish cost : "
  154. + (System.currentTimeMillis() - start));
  155. }
  156. } catch (Exception e) {
  157. e.printStackTrace();
  158. }
  159. }
  160. };
  161. }
  162. }
  163. interface TestCallBack {
  164. public void callback() throws Exception;
  165. public String getName();
  166. }

lock interupt相关推荐

  1. 理解notify()/notifyall()/interupt()对于线程里面wait方法的影响

    本文将通过三个程序例子帮助读者理解其中的原理: import java.text.SimpleDateFormat; import java.util.Date; import java.util.c ...

  2. 【java线程】锁机制:synchronized、Lock、Condition

    [Java线程]锁机制:synchronized.Lock.Condition 原创 2013年08月14日 17:15:55 标签:Java /多线程 74967 http://www.infoq. ...

  3. c# lock (obj) 与 lock (this) 区别

    lock(obj) 锁定 obj 对象 lock(this) 锁定 当前实例对象,如果有多个类实例的话,lock锁定的只是当前类实例,对其它类实例无影响. 直接上代码. 主窗体代码如下: delega ...

  4. java连接mysql执行ddl_Mysql 执行DDL导致Waiting for table metadata lock

    MySQL在进行alter table等DDL操作时,有时会出现Waiting for table metadata lock的等待场景.而且,一旦alter table TableA的操作停滞在Wa ...

  5. Go 分布式学习利器(18)-- Go并发编程之lock+WaitGroup实现线程安全

    Go语言中通过Groutine 启动一个Go协程,不同协程之间是并发执行的,就像C++/Java中线程之间线程安全是一个常见的问题. 如下Go 语言代码: func TestConcurrent(t ...

  6. apt Could not get lock /var/lib/dpkg/lock 解决方案

    apt Could not get lock /var/lib/dpkg/lock 解决方案 删除锁定文件 sudo rm /var/lib/dpkg/lock

  7. hive lock命令的使用

    1.hive锁表命令 hive> lock table t1 exclusive;锁表后不能对表进行操作 2.hive表解锁: hive> unlock table t1; 3.查看被锁的 ...

  8. SQL Server Lock Escalation - 锁升级

    Articles Locking in Microsoft SQL Server (Part 12 – Lock Escalation) http://dba.stackexchange.com/qu ...

  9. java并发vol_java 并发中 volitile、synchronized和lock的比较(一)

    1.volitile和(synchronnized.lock) 首先比较volitile和synchronnized,volitile线程不安全,但是synchronized则是线程安全的. voli ...

最新文章

  1. 如何使用jdbc连接数据库
  2. hdu 3046(最小割)
  3. 各厂商服务器存储设备默认密码大全
  4. 【基础】ABAP不同变量类型之间数值大小比较
  5. 2022跨年代码(HTML·资源都是网上的可以直接使用)
  6. EntityFramework进阶——继承
  7. Python 装饰器理解
  8. 免费馅饼(HDU-1176)
  9. NPM酷库:cheerio,服务端jQuery接口实现
  10. java提示单个cass怎么办,求助解决hibernate报错,java.lang.casscastexception
  11. linux下隐藏tomcat版本号
  12. 计算机考试用户注册,全国计算机等级考试报名系统账号注册和登录
  13. 如何构建你的认知体系?查理芒格的100个思维模型
  14. Problem 1 : Multiples of 3 and 5
  15. Character Swap (Hard Version)
  16. python dfs
  17. RHEL 7.0下载
  18. 【转自知乎】软件实施工程师技能要求
  19. 自定义Group,解决Group setVisibility后,子View再次设置setVisibility无效的问题
  20. create-react-dom脚手架中图片的路径问题

热门文章

  1. Linux查看java进程
  2. 5分钟读懂c语言编译步骤
  3. 公约数(也叫公因数)|公倍数 |小知识|Golang
  4. 爱心跳动效果 CSS实现
  5. 没有兴趣爱好,该怎么回答面试官?
  6. Linux安装及基础命令了解
  7. maven本地仓库有依赖包,还会远程下载的问题
  8. sitemesh 了解
  9. vue项目全局引入jquery
  10. macos13发热严重(CMFSynAgent和sharingd两个进程占用cpu太高)