美文网首页
Java---中断机制

Java---中断机制

作者: 懒眉 | 来源:发表于2019-01-28 15:04 被阅读6次

    Thread中断机制涉及的方法或者属性有三个:

    public boolean isInterrupted();
    public static boolean interrupted();
    public void interrupt();
    
    isInterrupted

    首先看第一个,它很简单,用来判断这个线程是否被中断,true标识中断,此方法不影响线程的状态

    interrupted

    此方法和isInterrupted的区别在于,它会清除线程的中断状态,当一个线程中断之后,连续调用两次该方法,第一次返回结果为true,第二次返回结果为false

    interrupt

    先看API注释,我大概提取了几个意思,下面就所理解的做说明(建议去看原文):

    • 中断调用线程,并不中断流程,只是修改中断状态为true,流程由开发者控制。
    public class MainTest {
        
        public static void main(String[] args) {
            TestThread thread = new TestThread();
            thread.start();
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            thread.interrupt();
            System.out.println("中断之后:" +thread.isInterrupted() );
        }
        
    }
    
    class  TestThread  extends Thread{
        @Override
        public void run() {
            // TODO Auto-generated method stub
            super.run();
            for (int j = 0; j < 300; j++) {
                System.out.println("输出:" + j + "--中断状态:" +this.isInterrupted() );
            }
            System.out.println("线程执行完成");
        }
    }
    
    

    运行代码,我们可以看到,状态值已经改变,但中断之后的操作,开发者自己设计。

    输出:2--中断状态:false
    输出:3--中断状态:false
    输出:4--中断状态:true
    中断之后:true
    输出:5--中断状态:true
    输出:6--中断状态:true
    
    • 调用者线程如果被如wait(),join(),sleep()方法阻塞时,将抛出InterruptedException异常,并且重置中断状态
    public class MainTest {
        
        public static void main(String[] args) {
            TestThread thread = new TestThread();
            thread.start();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            thread.interrupt();
            System.out.println("中断之后:" +thread.isInterrupted() );
        }
        
    }
    
    class  TestThread  extends Thread{
        @Override
        public void run() {
            // TODO Auto-generated method stub
            super.run();
            for (int j = 0; j < 20; j++) {
                System.out.println("输出:" + j + "--中断状态:" +this.isInterrupted() );
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            System.out.println("线程执行完成");
        }
    }
    
    输出:4--中断状态:false
    输出:5--中断状态:false
    中断之后:true
    java.lang.InterruptedException: sleep interrupted
    输出:6--中断状态:false
        at java.lang.Thread.sleep(Native Method)
        at com.cs.多线程.中断.TestThread.run(MainTest.java:35)
    输出:7--中断状态:false
    输出:8--中断状态:false
    

    这个时候可以看到,调用中断方法之后,中断状态变成true,然后由于sleep()抛出InterruptedException异常,状态被重置所以马上恢复为false。捕获异常可以根据需要进行处理,一般是上抛或者其他逻辑,此处需要注意,System.out.println("中断之后:" +thread.isInterrupted() );该处打印并不一定结果为true,这取决于主,子线程的执行速度以及状态修改的时机。

    • 线程中出现I/O阻塞,此时在线程中被等待连接的状态中阻塞,进行中断并不能退出线程,此时需要关闭socketIOException异常捕获时退出。
    public class SocketThread extends Thread {
        private ServerSocket socket;  
        
        public static void main( String args[] ) throws Exception {  
           SocketThread thread = new SocketThread();  
           System.out.println("启动线程" ); 
           thread.start();
           //这里假设3秒没有客户端连接就需要结束线程
           Thread.sleep(3000);  
           System.out.println("中断线程" ); 
           thread.interrupt();
           System.out.println("关闭socket通道");
           thread.socket.close();  
         }  
          
        @Override
        public void run() {  
            try {  
              socket = new ServerSocket(12345);  
            } catch ( IOException e ) {  
              return;  
            }  
            while (! Thread.currentThread().isInterrupted()) {  
              try {  
                  System.out.println("等待客户端连接...");  
                  Socket sock = socket.accept();  
              } catch ( IOException e ) {  
                  System.out.println("I/O异常");  
                  break;
              }  
            }  
            System.out.println("线程执行结束" );  
         }  
    }
    
    • 线程中出现java.nio.channels.Selector阻塞,待续。
    • 中断非活动线程没有效果
    • 当前线程不能修改该线程将抛出SecurityException异常

    参考:
    https://blog.csdn.net/meiliangdeng1990/article/details/80559012
    https://www.cnblogs.com/jenkov/p/juc_interrupt.html
    https://www.ibm.com/developerworks/cn/java/j-jtp05236.html

    相关文章

      网友评论

          本文标题:Java---中断机制

          本文链接:https://www.haomeiwen.com/subject/wclajqtx.html