当前位置: 首页 > news >正文

Java 多线程编程 - 线程池任务终止分析(线程池任务终止、中断的本质、检查中断)

一、线程池任务终止

  1. shutdown():等当前任务完成,然后停止
ScheduledExecutorServicescheduler=Executors.newScheduledThreadPool(1);// 启动任务,初始延迟 0 秒,每次执行完后延迟 1 秒再执行下一次ScheduledFuture<?>future=scheduler.scheduleWithFixedDelay(()->{try{System.out.println("任务开始执行");Thread.sleep(5*1000);System.out.println("任务执行完成");}catch(InterruptedExceptione){System.out.println("任务被中断");}},0,1,TimeUnit.SECONDS);try{Thread.sleep(2*1000);}catch(InterruptedExceptione){e.printStackTrace();}scheduler.shutdown();
# 输出结果 任务开始执行 任务执行完成
  1. cancel(true) + shutdown():立即中断当前任务,然后停止
ScheduledExecutorServicescheduler=Executors.newScheduledThreadPool(1);// 启动任务,初始延迟 0 秒,每次执行完后延迟 1 秒再执行下一次ScheduledFuture<?>future=scheduler.scheduleWithFixedDelay(()->{try{System.out.println("任务开始执行");Thread.sleep(5*1000);System.out.println("任务执行完成");}catch(InterruptedExceptione){System.out.println("任务被中断");}},0,1,TimeUnit.SECONDS);try{Thread.sleep(2*1000);}catch(InterruptedExceptione){e.printStackTrace();}future.cancel(true);scheduler.shutdown();
# 输出结果 任务开始执行 任务被中断
  1. shutdownNow():立即中断所有任务,然后停止
ScheduledExecutorServicescheduler=Executors.newScheduledThreadPool(1);// 启动任务,初始延迟 0 秒,每次执行完后延迟 1 秒再执行下一次ScheduledFuture<?>future=scheduler.scheduleWithFixedDelay(()->{try{System.out.println("任务开始执行");Thread.sleep(5*1000);System.out.println("任务执行完成");}catch(InterruptedExceptione){System.out.println("任务被中断");}},0,1,TimeUnit.SECONDS);try{Thread.sleep(2*1000);}catch(InterruptedExceptione){e.printStackTrace();}scheduler.shutdownNow();
# 输出结果 任务开始执行 任务被中断

二、中断的本质

  1. Java 中的中断机制是协作式的,而不是强制性的

  2. 上述案例中的中断是通过触发Thread.sleep()的 InterruptedException 来实现的

  3. 中断只是设置标志位,并不能强制打断任务,如下例

ScheduledExecutorServicescheduler=Executors.newScheduledThreadPool(1);// 启动任务,初始延迟 0 秒,每次执行完后延迟 1 秒再执行下一次ScheduledFuture<?>future=scheduler.scheduleWithFixedDelay(()->{try{System.out.println("任务开始执行");ServerSocketserverSocket=newServerSocket(9999);serverSocket.accept();System.out.println("任务执行完成");}catch(IOExceptione){e.printStackTrace();}},0,1,TimeUnit.SECONDS);try{Thread.sleep(2*1000);}catch(InterruptedExceptione){e.printStackTrace();}future.cancel(true);scheduler.shutdown();
  1. Thread.sleep()在抛出 InterruptedException 时会自动清除中断标志位
ScheduledExecutorServicescheduler=Executors.newScheduledThreadPool(1);// 启动任务,初始延迟 0 秒,每次执行完后延迟 1 秒再执行下一次ScheduledFuture<?>future=scheduler.scheduleWithFixedDelay(()->{try{System.out.println("任务开始执行");System.out.println("中断标志位:"+Thread.currentThread().isInterrupted());Thread.sleep(5*1000);System.out.println("任务执行完成");}catch(InterruptedExceptione){System.out.println("任务被中断");}finally{System.out.println("中断标志位:"+Thread.currentThread().isInterrupted());}},0,1,TimeUnit.SECONDS);try{Thread.sleep(2*1000);}catch(InterruptedExceptione){e.printStackTrace();}future.cancel(true);scheduler.shutdown();
# 输出结果 任务开始执行 中断标志位:false 任务被中断 中断标志位:false

三、检查中断

  1. shutdown():等当前任务完成,然后停止,中断标志位没有变化
ScheduledExecutorServicescheduler=Executors.newScheduledThreadPool(1);// 启动任务,初始延迟 0 秒,每次执行完后延迟 1 秒再执行下一次ScheduledFuture<?>future=scheduler.scheduleWithFixedDelay(()->{System.out.println("任务开始执行");System.out.println("中断标志位:"+Thread.currentThread().isInterrupted());Scannerscanner=newScanner(System.in);System.out.print("请输入一个整数:");intnum=scanner.nextInt();System.out.println("任务执行完成");System.out.println("中断标志位:"+Thread.currentThread().isInterrupted());},0,1,TimeUnit.SECONDS);try{Thread.sleep(2*1000);}catch(InterruptedExceptione){e.printStackTrace();}scheduler.shutdown();
# 输出结果 任务开始执行 中断标志位:false 请输入一个整数:12 任务执行完成 中断标志位:false
  1. cancel(true) + shutdown():立即中断当前任务,中断标志位会被设置为 true,然后停止
ScheduledExecutorServicescheduler=Executors.newScheduledThreadPool(1);// 启动任务,初始延迟 0 秒,每次执行完后延迟 1 秒再执行下一次ScheduledFuture<?>future=scheduler.scheduleWithFixedDelay(()->{System.out.println("任务开始执行");System.out.println("中断标志位:"+Thread.currentThread().isInterrupted());Scannerscanner=newScanner(System.in);System.out.print("请输入一个整数:");intnum=scanner.nextInt();System.out.println("任务执行完成");System.out.println("中断标志位:"+Thread.currentThread().isInterrupted());},0,1,TimeUnit.SECONDS);try{Thread.sleep(2*1000);}catch(InterruptedExceptione){e.printStackTrace();}future.cancel(true);scheduler.shutdown();
# 输出结果 任务开始执行 中断标志位:false 请输入一个整数:12 任务执行完成 中断标志位:true
  1. shutdownNow():立即中断所有任务,中断标志位会被设置为 true,然后停止
ScheduledExecutorServicescheduler=Executors.newScheduledThreadPool(1);// 启动任务,初始延迟 0 秒,每次执行完后延迟 1 秒再执行下一次ScheduledFuture<?>future=scheduler.scheduleWithFixedDelay(()->{System.out.println("任务开始执行");System.out.println("中断标志位:"+Thread.currentThread().isInterrupted());Scannerscanner=newScanner(System.in);System.out.print("请输入一个整数:");intnum=scanner.nextInt();System.out.println("任务执行完成");System.out.println("中断标志位:"+Thread.currentThread().isInterrupted());},0,1,TimeUnit.SECONDS);try{Thread.sleep(2*1000);}catch(InterruptedExceptione){e.printStackTrace();}scheduler.shutdownNow();
# 输出结果 任务开始执行 中断标志位:false 请输入一个整数:12 任务执行完成 中断标志位:true
http://www.cnnetsun.cn/news/151017.html

相关文章:

  • Java 多线程编程 - 线程池 awaitTermination 方法
  • FaceFusion更新日志追踪:每月都有新功能上线
  • (Open-AutoGLM实战白皮书)首次公开:跨平台任务调度的7种高效模式
  • 分布式幂等性:30字讲透核心要点
  • FaceFusion能否对接OneDrive?微软生态无缝衔接
  • 【AI模型部署必读】:Open-AutoGLM云端推理速度提升3倍的秘密路径
  • 为什么顶尖团队开始弃用Monica Manus改用Open-AutoGLM?真相在这里
  • 为什么顶尖大厂开始从Appium转向Open-AutoGLM?这3个关键点你必须知道
  • Open-AutoGLM三大黑科技揭秘:彻底摆脱RPA僵化操作的束缚
  • FaceFusion能否处理带有投影变形的墙面视频?
  • 13、全面掌握 Internet Explorer 配置:个性化与优化指南
  • 14、深入了解Internet Explorer的配置与维护
  • 27、常见连接问题解析与解决指南
  • 28、网络资源安全权限设置与故障排除全解析
  • 29、Windows系统安全与权限管理全解析
  • 34、Windows XP 多用户、多引导和联网计算机故障排除及 SP2 安全增强
  • 视觉识别架构之争,Open-AutoGLM与Mobile-Agent的底层逻辑差异,90%开发者都忽略了
  • Open-AutoGLM与Monica Manus执行效率对比(2024最新 benchmark 数据曝光)
  • 【AI模型选型避坑指南】:Open-AutoGLM与AutoGLM沉思机制的3个致命误区
  • FaceFusion开源项目获得Linux基金会支持
  • Ruoyi-AI技术架构完全重构:从单体到云原生的终极指南
  • 41、Windows PE:功能、使用与定制全解析
  • FaceFusion人脸融合过渡是否平滑?动态视频测试
  • FaceFusion人脸姿态估计精度高达98.7%,行业领先
  • AutoGLM沉思功能被超越?Open-AutoGLM的7大创新点全曝光
  • FaceFusion能否实现自动情绪增强功能?
  • Open-AutoGLM与RPA的5大核心差异(自动化技术跃迁指南)
  • OSPF协议
  • Rust Web开发终极指南:Cot框架快速入门教程
  • 5大核心功能使YashanDB数据库适应多种场景