需要恢復中斷狀態的一個場景

developerguy發表於2015-04-10

沒有恢復中斷狀態時,在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)

 

 


相關文章