1. 程式人生 > >子線程指令重排

子線程指令重排

app cep count all rest 一次 com oid error

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

子線程指令重排