标签:
1.线程中断的结果专业术语
isInterrupted interrupted interrupt
// 测试当前线程是否已经中断,同时会将线程的中断状态取消
Thread.interrupted();
// 在当前线程加上一个打断标记 ** 并不会真的立即停止线程
thread.interrupt();
// 测试线程是否已经中断
thread.isInterrupted();
2.知识点:
直接调用interrupt 不会中断线程
package org.test; /* * 线程中断演示 * * @author patzheng 597439394@163.com * */ public class Yak { public static void main(String[] args) { Thread countThread = new Thread(new CountRunnable(10)); countThread.start(); countThread.interrupt(); System.err.println("子线程被主线程给终止"); } } class CountRunnable implements Runnable { private int maxValue; public CountRunnable(int maxValue) { this.maxValue = maxValue; } @Override public void run() { int sum = 0; for (int i = 0; i < maxValue; i++) { sum += i; Long now = System.currentTimeMillis(); while (System.currentTimeMillis() - now < 200) { } } System.err.println(sum); } }
换个方式终止线程:使用break 语句结束循环,但是循环之外的语句还是会执行。使用的是
countThread.interrupt();可以看到终止之后线程的isInterupted返回的都是true;如果换成interupted的话就会输出
false 意味着线程的终止状态被清除
package org.test; import java.text.BreakIterator; /* * 线程中断演示 * * @author patzheng 597439394@163.com * */ public class Yak { public static void main(String[] args) { Thread countThread = new Thread(new CountRunnable(10)); countThread.start(); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } countThread.interrupt(); System.err.println("子线程被主线程给终止"); } } /** * 再runnable 里面直接用Thread.currentThread 就可以使用Thread的一些方法了 * * @author patzheng 597439394@163.com * */ class CountRunnable implements Runnable { private int maxValue; public CountRunnable(int maxValue) { this.maxValue = maxValue; } @Override public void run() { int sum = 0; for (int i = 0; i < maxValue; i++) { if (!Thread.currentThread().isInterrupted()) { sum += i; } else { System.err.println(Thread.currentThread().isInterrupted() + "status"); System.err.println(Thread.currentThread().isInterrupted() + "status"); System.err.println("break"); break; } Long now = System.currentTimeMillis(); while (System.currentTimeMillis() - now < 1000) { } } System.err.println(sum); } }
接下来通过异常来处理:
package org.test; /* * 线程中断演示 * * @author patzheng 597439394@163.com * */ public class Yak { public static void main(String[] args) { Thread countThread = new Thread(new CountRunnable(10)); countThread.start(); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } countThread.interrupt(); System.err.println("子线程被主线程给终止"); } } /** * 再runnable 里面直接用Thread.currentThread 就可以使用Thread的一些方法了 * * @author patzheng 597439394@163.com * */ class CountRunnable implements Runnable { private int maxValue; public CountRunnable(int maxValue) { this.maxValue = maxValue; } @Override public void run() { try { int sum = 0; for (int i = 0; i < maxValue; i++) { if (!Thread.interrupted()) { sum += i; } else { throw new InterruptedException(" thread interrupted"); } Long now = System.currentTimeMillis(); while (System.currentTimeMillis() - now < 1000) { } } System.err.println(sum); } catch (Exception e) { e.printStackTrace(); } } }
线程在sleep,wait,join 的情况下呗interrupt回发生什么?都直接抛出异常 这也是为什么使用这些方法的时候需要捕获
InterruptedException异常 而且中断状态被清除
并非所有的阻塞方法都抛出 InterruptedException
。输入和输出流类会阻塞等待 I/O 完成,但是它们不抛出 InterruptedException
,而且在被中断的情况下也不会提前返回。然而,对于套接字 I/O,如果一个线程关闭套接字,则那个套接字上的阻塞 I/O 操作将提前结束,并抛出一个 SocketException
。java.nio
中的非阻塞 I/O 类也不支持可中断 I/O,但是同样可以通过关闭通道或者请求 Selector
上的唤醒来取消阻塞操作。类似地,尝试获取一个内部锁的操作(进入一个 synchronized
块)是不能被中断的,但是 ReentrantLock
支持可中断的获取模式。
最佳实践 对于这个异常最好的处理办法是在异常处理中将中断复位方便接下来的处理
标签:
原文地址:http://my.oschina.net/payzheng/blog/513274