zoukankan      html  css  js  c++  java
  • 需要恢复中断状态的一个场景

    没有恢复中断状态时,在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)





  • 相关阅读:
    Delphi线程的终止
    Delphi线程简介---Create及其参数、Resume、Suspend
    谈谈Delphi中的类和对象4---类是一种对数据和操作高度的封装机制 && 类是一种代码重用机制
    LeetCode:链表排序
    LeetCode 二叉树的最小深度
    hadoop的集群安装
    java线程池分析和应用
    Java thread中对异常的处理策略
    Thread interrupt方法解析
    如何偷Android的内存-Tricking Android MemoryFile
  • 原文地址:https://www.cnblogs.com/softidea/p/4413374.html
Copyright © 2011-2022 走看看