码迷,mamicode.com
首页 > 编程语言 > 详细

子线程指令重排

时间:2018-08-09 14:03:19      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:cep   读取   www   mes   使用   bst   await   end   catch   

public void run() {
        CountDownLatch countDownLatch = new CountDownLatch(1);

        try{
            try{
                if(!GlobalConstants.IB_GATEWAY_LOG_URL.equals(logUrl)) {

                    logger.info("change log url : \n old : {} \n new : {}", logUrl, GlobalConstants.IB_GATEWAY_LOG_URL);

                    // 读取重启前上一次读取后遗留的日志
                    if(logUrl != null && !"".equals(logUrl)) {
                        int res = readLogFile(logUrl);
                        if(res == 0) {
                            logger.info("read last restart success");
                            anotherDay = false;
                        } else if(res == 1) {
                            logger.info("last restart file not found");
                        } else {
                            logger.info("last restart file read fail");
                        }
                    }

                    logUrl = GlobalConstants.IB_GATEWAY_LOG_URL;

                }
            } catch (Exception e){
                logger.error(e.getMessage(), e);
            } finally {
                countDownLatch.countDown();
            }

            countDownLatch.await();

            countDownLatch = new CountDownLatch(1);

            // 判断是否隔天
            Date now = new Date();
            String stnow = dateOnly.format(now);
            String stformer = dateOnly.format(startTime);
            if(!stnow.equals(stformer)) {
                // 说明隔天了,激活隔天读取机制
                anotherDay = true;
                logger.info("another day");
            }


            try{
                if(anotherDay) {
                    String logUrlTemp = logUrl;
                    int index = logUrlTemp.lastIndexOf(".");
                    StringBuilder stringBuilder = new StringBuilder(100);
                    stringBuilder.append(logUrlTemp.substring(0, index+1)).append(dateIbFormate.format(startTime)).append(".log");

                    int res = readLogFile(stringBuilder.toString());
                    if(res == 0) {
                        logger.info("read yesterday success");
                        anotherDay = false;
                    } else if(res == 1) {
                        logger.info("yesterday file not found");
                    } else {
                        logger.info("yestoday file read fail");
                    }
                }
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            } finally {
                countDownLatch.countDown();
            }

            countDownLatch.await();

            readLogFile(logUrl);

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

  

在未使用CountDownLatch前,有出现最后一行

readLogFile

的数据先于前面的代码数据执行插入操作了,故为了确保数据有序性,加入CountDownLatch

性质有点像:https://www.cnblogs.com/silyvin/p/9106641.html

子线程指令重排

标签:cep   读取   www   mes   使用   bst   await   end   catch   

原文地址:https://www.cnblogs.com/silyvin/p/9448012.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!