对Java中interrupt、interrupted和isInterrupted的理解

interrupt()

interrupt方法用于中断线程。调用该方法的线程的状态为将被置为”中断”状态。

注意:线程中断仅仅是设置线程的中断状态位,不会停止线程

支持线程中断的方法(也就是线程中断后会抛出interruptedException的方法)就是在监视线程的中断状态,一旦线程的中断状态被置为“中断状态”,就会抛出中断异常。

当处于阻塞状态的线程调用interrupt()时,会抛出InterruptedException,此时线程会退出阻塞,并会清除中断标记。也就是我们调用sleep、wait等此类可中断(throw InterruptedException)方法时,一旦方法抛出InterruptedException,当前调用该方法的线程的中断状态就会被jvm自动清除了,就是说我们调用该线程的isInterrupted 方法时是返回false。

如果你想保持中断状态,可以再次调用interrupt方法设置中断状态。这样做的原因是,java的中断并不是真正的中断线程,而只设置标志位(中断位)来通知用户。如果你捕获到中断异常,说明当前线程已经被中断,不需要继续保持中断位。

下面给出例子来看看:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class Interrupt {
public static void main(String[] args) throws Exception {
Thread t = new Thread(new Worker());
t.start();

Thread.sleep(200);
t.interrupt();

System.out.println("Main thread stopped.");
}

public static class Worker implements Runnable {
public void run() {
System.out.println("Worker started.");

try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println("Worker IsInterrupted: " +
Thread.currentThread().isInterrupted());
}

System.out.println("Worker stopped.");
}
}
}

内容很简答:主线程main启动了一个子线程Worker,然后让worker睡500ms,而main睡200ms,之后main调用worker线程的interrupt方法去中断worker,worker被中断后打印中断的状态。

下面是执行结果:

1
2
3
4
Worker started.
Main thread stopped.
Worker IsInterrupted: false
Worker stopped.

打印的结果充分地印证了当可中断的方法抛出异常时,线程中断的标记也会跟着被jvm所清除,因此调用isInterrupted()方法返回的是false。

interrupted()和isInterrupted()

我们先看看这两个方法的源码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
	public static boolean interrupted() {
return currentThread().isInterrupted(true);
}

public boolean isInterrupted() {
return isInterrupted(false);
}

/**
2 * Tests if some Thread has been interrupted. The interrupted state
3 * is reset or not based on the value of ClearInterrupted that is
4 * passed.
5 */
6 private native boolean isInterrupted(boolean ClearInterrupted);

原来这是一个本地方法,看不到源码。不过没关系,通过参数名我们就能知道,这个参数代表是否要清除状态位。

如果这个参数为true,说明返回线程的状态位后,要清掉原来的状态位(恢复成原来情况)。这个参数为false,就是直接返回线程的状态位。

这两个方法有两个主要区别:

  1. interrupted()是作用于当前线程,静态方法isInterrupted()是作用于调用该方法的线程对象所对应的线程;(线程对象对应的线程不一定是当前运行的线程。例如我们可以在A线程中去调用B线程对象的isInterrupted()方法)
  2. 这两个方法最终都会调用同一个方法,只不过参数一个是true,一个是false;
  3. 这两个方法很好区分,只有当前线程才能清除自己的中断位。(对应interrupted()方法)

interrupted()是静态方法,返回的是当前线程的中断状态。例如,如果当前线程被中断(没有抛出中断异常,否则中断状态就会被清除),你调用interrupted()方法,第一次会返回true。然后,当前线程的中断状态被方法内部清除了。第二次调用时就会返回false。如果你刚开始一直调用isInterrupted(),则会一直返回true,除非中间线程的中断状态被其他操作清除了。

下面我们看看例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class Interrupt  {
public static void main(String[] args) throws Exception {
Thread t = new Thread(new Worker());
t.start();

Thread.sleep(200);
t.interrupt();

System.out.println("Main thread stopped.");
}

public static class Worker implements Runnable {
public void run() {
System.out.println("Worker started.");

try {
Thread.sleep(500);
} catch (InterruptedException e) {
Thread curr = Thread.currentThread();
//再次调用interrupt方法中断自己,将中断状态设置为“中断”
curr.interrupt();
System.out.println("Worker IsInterrupted: " + curr.isInterrupted());
System.out.println("Worker IsInterrupted: " + curr.isInterrupted());
System.out.println("Static Call: " + Thread.interrupted());//clear status
System.out.println("---------After Interrupt Status Cleared----------");
System.out.println("Static Call: " + Thread.interrupted());
System.out.println("Worker IsInterrupted: " + curr.isInterrupted());
System.out.println("Worker IsInterrupted: " + curr.isInterrupted());
}

System.out.println("Worker stopped.");
}
}
}

执行结果:

1
2
3
4
5
6
7
8
9
10
Worker started.
Main thread stopped.
Worker IsInterrupted: true
Worker IsInterrupted: true
Static Call: true
---------After Interrupt Status Cleared----------
Static Call: false
Worker IsInterrupted: false
Worker IsInterrupted: false
Worker stopped.

从执行结果也可以看到,前两次调用isInterrupted方法都返回true,说明isInterrupted方法不会改变线程的中断状态,而接下来调用静态的interrupted()方法,第一次返回了true,表示线程被中断,第二次则返回了false,因为第一次调用的时候已经清除了中断状态。最后两次调用isInterrupted()方法就肯定返回false了。

参考:

https://www.cnblogs.com/zhuhongchang/p/8467803.html

https://my.oschina.net/itblog/blog/787024

秉持初心,继续向前。
显示 Gitment 评论