/* * 文件名:TestInterrupt.java * 版权:Copyright 2017-2018 liutao. Co. Ltd. All Rights Reserved. * 修改人:Administrator * 修改时间:2020年5月10日 * 修改内容:新建 */ package test20200510; import org.junit.Test; /** * 〈一句话功能简述〉 〈功能详细描述〉 * * @author Administrator * @version 1.0 2020年5月10日 * @see TestInterrupt * @since 1.0 */ public class TestInterrupt { public static void main(String[] args) throws InterruptedException { // testInterrupted(); testInterrupted4(); } /** * thread.interrupt不能终止正在运行的线程 <一句话功能简述> <功能详细描述> 当时会改变线程的状态值false->true * * @throws InterruptedException * @see * @since 1.0 */ @Test public void testInterupt() throws InterruptedException { Thread thread1 = new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub for (int i = 0; i < 1000000; i++) { System.out.println("i:" + i); } } }); thread1.start(); Thread.sleep(10); thread1.interrupt(); } /*** * Thread.interrupted 判断当前线程是否终止 并且会清除当前线程的状态值true->false <一句话功能简述> <功能详细描述> * * @throws InterruptedException * @see * @since 1.0 */ public static void testInterrupted() throws InterruptedException { Thread thread1 = new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub for (int i = 0; i < 10000; i++) { System.out.println("i:" + i); } System.out.println("是否停止 " + Thread.interrupted()); System.out.println("是否停止 " + Thread.interrupted()); } }); thread1.start(); Thread.sleep(10); thread1.interrupt(); // System.out.println("是否停止 " + Thread.interrupted()); // System.out.println("是否停止 " + Thread.interrupted()); } /** * 使用Thread.interruptted+break无法使当前线程立即退出运行 如何解决?使用异常+try catch 又称异常法终止线程 * * 线程状态值默认是false 如果调用interrupt,则变为true 使用Thread.interrupted()则会将true->false * <一句话功能简述> <功能详细描述> * * @throws InterruptedException * @see * @since 1.0 */ public static void testInterrupted2() throws InterruptedException { Thread thread1 = new Thread(new Runnable() { @Override public void run() { try { for (int i = 0; i < 10000; i++) {
System.out.println("i:" + i + Thread.currentThread().isInterrupted()); if (Thread.interrupted()) { System.out.println("线程终止。。。 退出。。。" + Thread.currentThread().isInterrupted()); throw new InterruptedException(); } } System.out.println("end-------"); } catch (Exception e) { // TODO: handle exception System.out.println("Mythread ex:" + e); } // TODO Auto-generated method stub } }); thread1.start(); Thread.sleep(10); thread1.interrupt(); } /** * 如果当前线程正则sleep 这时去interrupt 则会直接抛出InterruptedException 并且将状态值改为false * <一句话功能简述> <功能详细描述> * * @throws InterruptedException * @see * @since 1.0 */ public static void testInterrupted3() throws InterruptedException { Thread thread1 = new Thread(new Runnable() { @Override public void run() { try { for (int i = 0; i < 10000; i++) { System.out.println("i:" + i); } Thread.sleep(10000); System.out.println("end-------"); } catch (Exception e) { System.out.println("当前线程状态:" + Thread.interrupted()); System.out.println("Mythread ex:" + e); } } }); thread1.start(); Thread.sleep(10); thread1.interrupt(); } volatile static boolean flag = true; /** * 终止线程推荐做法 标志位 <一句话功能简述> <功能详细描述> * * @throws InterruptedException * @see * @since 1.0 */ public static void testInterrupted4() throws InterruptedException { Thread thread1 = new Thread(new Runnable() { @Override public void run() { int i = 0; while (flag) { System.out.println(i++); } } }); thread1.start(); Thread.sleep(100); flag = false; } }
总结
- Thread.interrupted()是静态方法,thread.interrupt()是普通方法
- thread.interrupt()不能停止线程,仅仅是将当前线程的标志由true改为false
- Thread.interrupted()返回当前线程的状态,如果为true,将当前线程的标志由true改为false
- 终止线程的推荐做法,循环中使用标志位来终止线程
为什么现在不使用 thread.stop来终止线程? (在线程run方法中Thread.currentThread()==this)
1 此方法已废弃
2 此方法过于暴力,如果当前线程在操作外部资源,调用stop方法,会导致catch finally中代码不执行,资源不释放
3 数据不一致