zoukankan      html  css  js  c++  java
  • java线程中的interrupt,isInterrupt,interrupted方法

    在java的线程Thread类中有三个方法,比较容易混淆,在这里解释一下 (1)interrupt:置线程的中断状态 (2)isInterrupt:线程是否中断 (3)interrupted:返回线程的上次的中断状态,并清除中断状态
    举个例子:

    1. 用法:  
    2. class MyThread extends Thread {  
    3.     ......  
    4.     ......  
    5.     public void run() {  
    6.         try {  
    7.             while(!Thread.currentThread().isInterrupted()) {  
    8.                 //当达到队列容量时,在这里会阻塞  
    9.                 //put的内部会调用LockSupport.park()这个是用来阻塞线程的方法  
    10.                 //当其他线程,调用此线程的interrupt()方法时,会设置一个中断标志  
    11.                 //LockSupport.part()中检测到这个中断标志,会抛出InterruptedException,并清除线程的中断标志  
    12.                 //因此在异常段调用Thread.currentThread().isInterrupted()返回为false  
    13.                 ArrayBlockingQueue.put(somevalue);   
    14.             }  
    15.         } catch (InterruptedException e) {  
    16.             //由于阻塞库函数,如:Object.wait,Thread.sleep除了抛出异常外,还会清除线程中断状态,因此可能在这里要保留线程的中断状态  
    17.             Thread.currentThread().interrupt();  
    18.         }  
    19.     }  
    20.     public void cancel() {  
    21.         interrupt();  
    22.     }  
    23. }  
    24. 外部调用  
    25. MyThread thread = new MyThread();  
    26. thread.start();  
    27. ......  
    28. thread.cancel();  
    29. thread.isInterrupted();  

     
    一般来说,阻塞函数,如:Thread.sleep、Thread.join、Object.wait、LockSupport.park等在检查到线程的中断状态时,会抛出InterruptedException,同时会清除线程的中断状态
    对于InterruptedException的处理,可以有两种情况: (1)外层代码可以处理这个异常,直接抛出这个异常即可 (2)如果不能抛出这个异常,比如在run()方法内,因为在得到这个异常的同时,线程的中断状态已经被清除了,需要保留线程的中断状态,则需要调用Thread.currentThread().interrupt()
    另外,Thread.interrupted()在jdk库的源代码中比较常用,因为它既可以得到上一次线程的中断标志值,又可以同时清除线程的中断标志,一举两得,但同时也有坏处,就是这个函数有清除中断状态的副作用,不容易理解

  • 相关阅读:
    20200902
    20200808
    20200801
    20191017
    LeetCode #974. Subarray Sums Divisible by K 数组
    LeetCode #532. K-diff Pairs in an Array 数组 哈希 双指针
    LeetCode #234. Palindrome Linked List 链表 栈 链表逆置
    LeetCode #307. Range Sum Query
    LeetCode #45. Jump Game II 数组 贪心
    LeetCode #55. Jump Game 数组 贪心 线性DP 回溯
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/3440323.html
Copyright © 2011-2022 走看看