Java线程中断的正确姿势( 二 )


先对比下2函数的源码:
public static boolean interrupted() { return currentThread().isInterrupted(true); } public boolean isInterrupted() { return isInterrupted(false); } /** * Tests if some Thread has been interrupted. The interrupted state * is reset or not based on the value of ClearInterrupted that is * passed. */ private native boolean isInterrupted(boolean ClearInterrupted);从源码中可以看出,2函数都是调用了Native函数private native boolean isInterrupted(boolean ClearInterrupted);,前者调用传的参数为true,所以,调用interrupted函数,会在检测线程中断状态标志是否为true后,还会将中断状态标志重置为false 。而isInterrupted函数只是检测线程中断状态标志 。




推荐阅读