基于JDK8总结java中的interrupt

2025-05-27 0 29

1. interrupt知识点

以下总结基于JDK8

本文不会完整说明interrupt,只会罗列一些比较重要的点。完整了解Thread.interrupt可以看参考资料。

以下的一些理解新的有助于理解参考资料的文章:

interrupt方法调用后,针对BLOCKED状态的线程,只是设定中断标志位为true。是否响应中断(感知这个标志位的变化)取决于API的设计。JDK的阻塞IO API、Synchronized同步块、还有Lock中的很多方法(不包括lockInterruptibly)都是不响应中断的。当然调用线程可以利用标志位判断来使得自己设计的API是可响应中断的。

interrupt方法调用后,针对WAITING/TIMED_WAITING状态的线程,会上抛interruptedException**并且设置中断标志位false**。例如线程调用Thread.sleep,Object.wait()之后。

如果线程尚未启动(NEW),或者已经结束(TERMINATED),则调用interrupt()对它没有任何效果,中断标志位也不会被设置。

最佳实践:有时候一些方法设计上不允许被中断或者取消,但是当别的线程发来中断请求的时候,也需要进行标记的保留,方便其他调用方“了解情况”

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18
public Task getNextTask(BlockingQueue<Task> queue) {

boolean interrupted = false;

try {

while (true) {

try {

return queue.take();

} catch (InterruptedException e) {

//fianlly中依赖的状态标记

interrupted = true;

// fall through and retry

}

}

} finally {

if (interrupted)

//在fianlly中重新标记,确保没有丢失中断通知

Thread.currentThread().interrupt();

}

}

利用中断可以实现一些cancel的操作。例如:

?

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

35

36

37

38

39

40

41

42

43

44

45

46

47
package concurrent;

import java.util.concurrent.BlockingQueue;

import java.util.concurrent.Callable;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

/**

* Created by wanshao

* Date: 2017/12/18

* Time: 下午3:42

**/

public class InterruptExample {

public static void main(String[] args) throws InterruptedException {

InterruptTask interruptTask = new InterruptTask();

ExecutorService executorService = Executors.newSingleThreadExecutor();

executorService.submit(interruptTask);

Thread.sleep(100);

interruptTask.cancel();

executorService.shutdown();

}

}

/**

* 一个响应中断的任务

*/

class InterruptTask implements Callable<Integer> {

private BlockingQueue<Task> queue;

//保存要被interrupt的线程

Thread t;

@Override

public Integer call() throws InterruptedException {

System.out.println("start a blocked task");

try {

t = Thread.currentThread();

Thread.currentThread().sleep(50000);

} catch (InterruptedException e) {

System.out.println("be interrupted");

e.printStackTrace();

}

return 0;

}

public void cancel() {

System.out.println("cacel a task....");

//这里直接调用Thread.currentThread()会获取到main线程,而不是线程池里面的线程

if (!t.isInterrupted()) {

t.interrupt();

}

}

}

总结

以上所述是小编给大家介绍的基于JDK8总结java中的interrupt,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对快网idc网站的支持!

原文链接:http://www.kaimingwan.com/post/java/javabing-fa-yu-suo/guan-yu-javazhong-de-interrupt?utm_source=tuicool&utm_medium=referral

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 基于JDK8总结java中的interrupt https://www.kuaiidc.com/76846.html

相关文章

发表评论
暂无评论