需要恢复中断状态的一个场景
没有恢复中断状态时,在Step1执行期间发生中断,Step2操作还会继续,这就存在让数据出现不一致的风险:
import java.util.concurrent.TimeUnit; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /*2015-4-9*/ public class InterruptedDemo { private static final Logger LOGGER=LoggerFactory.getLogger(InterruptedDemo.class); public static void main(String[] args) throws InterruptedException { Thread playgame=new Thread(new TaskRunner(), "do a work"); playgame.start(); int waitTime=5; LOGGER.info("tips: task will be cannel ,after {}s",waitTime); TimeUnit.SECONDS.sleep(waitTime); playgame.interrupt(); } } class TaskRunner implements Runnable{ private static final Logger LOGGER=LoggerFactory.getLogger(TaskRunner.class); @Override public void run() { Step step1=new Step("step1",20); try { step1.process(); } catch (InterruptedException e) { LOGGER.info("clean 、 backup or other business ............."); LOGGER.error("step1 Fail",e); // Thread.currentThread().interrupt(); } Step step2=new Step("step2",10); try { step2.process(); } catch (InterruptedException e) { LOGGER.info("clean 、backup or other business ............. "); LOGGER.error("step2 Fail",e); // Thread.currentThread().interrupt(); } } } class Step{ private static final Logger LOGGER=LoggerFactory.getLogger(Step.class); private String step; private int costTime; public Step(String step,int costTime) { this.step=step; this.costTime=costTime; } void process() throws InterruptedException{ LOGGER.info("Step:{}",this.step); LOGGER.info("Need {}s",this.costTime); TimeUnit.SECONDS.sleep(this.costTime); LOGGER.info("end "); } }
Output:
[2015-04-10 01:37:57,634] [main] INFO - tips: task will be cannel ,after 5s [2015-04-10 01:37:57,634] [do a work] INFO - Step:step1 [2015-04-10 01:37:57,635] [do a work] INFO - Need 20s [2015-04-10 01:38:02,637] [do a work] INFO - clean 、 backup or other business ............. [2015-04-10 01:38:02,638] [do a work] ERROR - step1 Fail java.lang.InterruptedException: sleep interrupted at java.lang.Thread.sleep(Native Method) at java.lang.Thread.sleep(Thread.java:340) at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386) at thread.Step.process(InterruptedDemo.java:63) at thread.TaskRunner.run(InterruptedDemo.java:30) at java.lang.Thread.run(Thread.java:745) [2015-04-10 01:38:02,641] [do a work] INFO - Step:step2 [2015-04-10 01:38:02,641] [do a work] INFO - Need 10s [2015-04-10 01:38:12,641] [do a work] INFO - end
传递中断状态的场景:
去掉上面代码中Thread.currentThread().interrupt();这句的注释
Output:
[2015-04-10 01:41:32,349] [do a work] INFO - Step:step1 [2015-04-10 01:41:32,350] [do a work] INFO - Need 20s [2015-04-10 01:41:32,349] [main] INFO - tips: task will be cannel ,after 5s [2015-04-10 01:41:37,351] [do a work] INFO - clean 、 backup or other business ............. [2015-04-10 01:41:37,352] [do a work] ERROR - step1 Fail java.lang.InterruptedException: sleep interrupted at java.lang.Thread.sleep(Native Method) at java.lang.Thread.sleep(Thread.java:340) at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386) at thread.Step.process(InterruptedDemo.java:63) at thread.TaskRunner.run(InterruptedDemo.java:30) at java.lang.Thread.run(Thread.java:745) [2015-04-10 01:41:37,355] [do a work] INFO - Step:step2 [2015-04-10 01:41:37,355] [do a work] INFO - Need 10s [2015-04-10 01:41:37,355] [do a work] INFO - clean 、backup or other business ............. [2015-04-10 01:41:37,355] [do a work] ERROR - step2 Fail java.lang.InterruptedException: sleep interrupted at java.lang.Thread.sleep(Native Method) at java.lang.Thread.sleep(Thread.java:340) at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386) at thread.Step.process(InterruptedDemo.java:63) at thread.TaskRunner.run(InterruptedDemo.java:39) at java.lang.Thread.run(Thread.java:745)
标签:
interrupt
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Browser-use 详细介绍&使用文档
· 软件产品开发中常见的10个问题及处理方法
· Vite CVE-2025-30208 安全漏洞