news 2026/8/22 2:12:40

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

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
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
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/8/21 0:03:38

Java 多线程编程 - 线程池 awaitTermination 方法

awaitTermination 方法 1、基本介绍 boolean awaitTermination(long timeout, TimeUnit unit)throws InterruptedException;参数类型说明timeoutlong等待时间unitTimeUnit时间单位 返回值说明true线程池在超时前已终止false超时后线程池仍未终止awaitTermination 是 Java 线…

作者头像 李华
网站建设 2026/8/22 3:17:53

FaceFusion更新日志追踪:每月都有新功能上线

AI换脸技术的边界与工程伦理&#xff1a;为何专业分工不可逾越在人工智能技术迅猛发展的今天&#xff0c;我们时常看到各类AI工具以前所未有的速度迭代更新——FaceFusion每月上线新功能、DeepNude类项目引发伦理争议、Stable Diffusion开放模型催生创作革命。这些现象背后&…

作者头像 李华
网站建设 2026/8/20 22:29:12

(Open-AutoGLM实战白皮书)首次公开:跨平台任务调度的7种高效模式

第一章&#xff1a;Open-AutoGLM 跨应用任务处理竞品分析在跨应用自动化任务处理领域&#xff0c;多个框架和平台已展现出不同的技术路径与能力边界。Open-AutoGLM 作为新兴的开源解决方案&#xff0c;其核心优势在于结合大语言模型&#xff08;LLM&#xff09;驱动的任务解析与…

作者头像 李华
网站建设 2026/8/20 22:29:12

分布式幂等性:30字讲透核心要点

幂等性处理是分布式系统和微服务架构中保证数据一致性与系统健壮性的核心概念。我们来系统性地梳理一下。一、什么是幂等性&#xff1f;定义&#xff1a;一个操作&#xff08;或接口&#xff09;被重复执行多次所产生的效果&#xff0c;与仅执行一次所产生的效果完全相同。核心…

作者头像 李华
网站建设 2026/8/22 1:04:34

FaceFusion能否对接OneDrive?微软生态无缝衔接

FaceFusion 与 OneDrive 的无缝集成&#xff1a;打通 AI 生成与办公生态的“最后一公里”在内容创作日益依赖人工智能的今天&#xff0c;一个现实问题摆在开发者和企业面前&#xff1a;我们如何让 AI 工具产出的结果&#xff0c;不再沉睡于本地磁盘&#xff0c;而是自动进入用户…

作者头像 李华
网站建设 2026/8/21 22:05:04

【AI模型部署必读】:Open-AutoGLM云端推理速度提升3倍的秘密路径

第一章&#xff1a;Open-AutoGLM 端侧 vs 云端部署性能权衡在边缘计算与云计算并行发展的背景下&#xff0c;Open-AutoGLM 的部署策略需在端侧与云端之间做出性能与效率的权衡。端侧部署能够显著降低推理延迟、保障数据隐私&#xff0c;并减少对网络带宽的依赖&#xff1b;而云…

作者头像 李华