需要恢复中断状态的一个场景
没有恢复中断状态时,在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)