Java 并发 中断线程

@author ixenos

对Runnable.run()方法的三种处置情况


1.在Runnable.run()方法的中间中断它

2.等待该方法到达对cancel标志的测试(用一个if+标志变量)

3.等待该方法到达程序员准备好离开的地方

  第一种情况比其他两种难以处置,因为当打断一个被阻塞的任务时,可能需要清理资源。

  因此,在任务的run()方法中间打断,就像是抛出的异常,在“catch异常处理”中清理资源(否则不放锁),于是Java也为这种打断使用了异常机制。

可中断阻塞和不可中断阻塞


1.SleepBlock 是可中断的阻塞

2.IOBlocked 和 SynchronizedBlocked 是不可中断的阻塞(I/O和synchronized方法都不需要InterruptedException处理器)

示例:

  1 // Interrupting a blocked thread.
2 import java.util.concurrent.*;
3 import java.io.*;
4 import static net.mindview.util.Print.*;
5
6 /*
7 SleepBlock
8
9 */
10 class SleepBlocked implements Runnable {
11 public void run() {
12 try {
13 TimeUnit.SECONDS.sleep(100);
14 } catch(InterruptedException e) {
15 print("InterruptedException");
16 }
17 print("Exiting SleepBlocked.run()");
18 }
19 }
20
21
22 /*
23 IOBlock
24 */
25 class IOBlocked implements Runnable {
26 private InputStream in;
27 public IOBlocked(InputStream is) { in = is; }
28 public void run() {
29 try {
30 print("Waiting for read():");
31 in.read();
32 } catch(IOException e) {
33 if(Thread.currentThread().isInterrupted()) {
34 print("Interrupted from blocked I/O");
35 } else {
36 throw new RuntimeException(e);
37 }
38 }
39 print("Exiting IOBlocked.run()");
40 }
41 }
42
43
44 /*
45 SynchronizedBlock
46
47 */
48 class SynchronizedBlocked implements Runnable {
49 public synchronized void f() {
50 while(true) // Never releases lock
51 Thread.yield();
52 }
53 public SynchronizedBlocked() {
54 new Thread() {
55 public void run() {
56 f(); // Lock acquired by this thread
57 }
58 }.start();
59 }
60 public void run() {
61 print("Trying to call f()");
62 f();
63 print("Exiting SynchronizedBlocked.run()");
64 }
65 }
66
67
68 public class Interrupting {
69 //用java.util.concurrent.*包中的Executor来统一管理多个线程
70 private static ExecutorService exec =
71 Executors.newCachedThreadPool();
72
73 static void test(Runnable r) throws InterruptedException{
74 /*
75 submit() 启动任务将返回Future<?>,
76 可以调用cancel来调用interrupt方法中断某个特定任务;
77 executor() 启动任务,
78 只能调用shutdownNow()来中断所有任务。
79 */
80 Future<?> f = exec.submit(r);
81 TimeUnit.MILLISECONDS.sleep(100);
82 print("Interrupting " + r.getClass().getName());
83
84
85 f.cancel(true); // Interrupts if running
86 print("Interrupt sent to " + r.getClass().getName());
87 }
88
89 //main方法
90 public static void main(String[] args) throws Exception {
91 test(new SleepBlocked());
92 test(new IOBlocked(System.in));
93 test(new SynchronizedBlocked());
94 TimeUnit.SECONDS.sleep(3);
95 print("Aborting with System.exit(0)");
96 System.exit(0); // ... since last 2 interrupts failed
97 }
98 } /* Output: (95% match)
99 Interrupting SleepBlocked
100 InterruptedException
101 Exiting SleepBlocked.run()
102 Interrupt sent to SleepBlocked
103 Waiting for read():
104 Interrupting IOBlocked
105 Interrupt sent to IOBlocked
106 Trying to call f()
107 Interrupting SynchronizedBlocked
108 Interrupt sent to SynchronizedBlocked
109 Aborting with System.exit(0)
110 *///:~

中断线程


1.Thread类包含interrupt()方法,可以终止被阻塞的任务,这个方法将设置线程的中断状态

  1)如果一个线程已被阻塞 或 试图执行一个阻塞操作时,调用interrupt设置这个线程的中断状态,将抛出InterruptedException

  2)当抛出InterruptedException 或 该任务调用Thread.interrupted()时,中断状态将被复位为 false (但实际已中断~)。

      • 可见,使用Thread.interrupted() 既离开run()循环 中断线程,又不抛出InterruptedException

2.中断状态:中断状态是每一个线程都具有的boolean标志,(建议每个线程都应该不时地检查这个标志,以判断线程是否被中断)

  1)t.interrupt(): 当对一个线程调用interrupt()方法时,线程的中断状态将被置位

  2)Thread.interrupted():

最新文章

  1. NV SDK 10 (1) Clipmaps
  2. CSS3 媒体查询移动设备尺寸 Media Queries for Standard Devices (包括 苹果手表 apple watch)
  3. Python学习笔记(1):列表元组结构
  4. 通知(Notification) 、 应用间通信(一)
  5. 【个人笔记】002-PHP基础-01-PHP快速入门-02-PHP语言相关介绍输
  6. applicationContext-mail.xml 模板
  7. jquery 60秒倒计时
  8. Fault Diagnosability Infrastructure Overview
  9. Spark+ECLIPSE+JAVA+MAVEN windows开发环境搭建及入门实例【附详细代码】
  10. 第33篇 js 常用简单的写法
  11. 基于java:读写一个英文的txt文件,记录单词个数,并输出十个出现最多的单词及出现的个数;
  12. gulp 入门指南
  13. 关于一些常用的linux命令
  14. Python简介之输入和输出
  15. python 基础 ------字符串的调用详解(1)
  16. Nio再学习之NIO的buffer缓冲区
  17. sublime c++
  18. 题解 luogu P1144 【最短路计数】
  19. Oracle 和 SQLSERVER 重新获取统计信息的方法
  20. 拟物设计和Angular的实现 - Material Design

热门文章

  1. 使用response实现文件下载功能
  2. Nutch搜索引擎Solr简介及安装
  3. RPC(Remote Procedure Call Protocol)
  4. Python:Module Install Issues
  5. queue,指针求最短路的区别
  6. 用Python实现gmail邮箱服务,实现两个邮箱之间的绑定(上)
  7. [整理]Breakpoint on arbitrary selector
  8. windows 服务器系统日志分析及安全
  9. MongoDB学习(翻译6)
  10. iOS app 发布错误 ERROR ITMS-90167: &quot;No .app bundles found in the package&quot;