for improper recycling infinite loop, CPU surge

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/red_sheeps/article/details/78964718

CPU surge

Taiwan recently discovered that some of the service area of ​​the CPU on a surge in production, followed by memory also grew more powerful, they tracked down (too high CPU query method is not described here, on their own Baidu) found that at a certain method in the thread card, as follows :
Write pictures described here

Code

Read the code of this method turned out to be a for loop code, you suspect may be the cycle of death, as follows:

    public static String getGMTNumOfYear(Date yyyyDateTime) throws ParseException {

        String gmtDate = getGMT(yyyyDateTime);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
                "yyyyMMddHHmmss");
        Date date =simpleDateFormat.parse(gmtDate);
        Calendar cd = Calendar.getInstance();
        cd.setTime(date);
        String yearOfNumber = cd.get(Calendar.DAY_OF_YEAR) + "";
        if (yearOfNumber.length() < 3) {
            for (int i = 3 - yearOfNumber.length(); i > 0; i++) {
                yearOfNumber = "0" + yearOfNumber;
            }
        }
        return yearOfNumber;
    }

The purpose of this method is to obtain a certain day in the first few days of the year, if less than three (365 days), then return to the pre-fill 0 out.

the reason

But the cycle of death should not be on the line not be measured, ah, is a function of the tested part of the test.
Later analysis of the next, and she found out why, look at the code submission date is the second half of the development of the code, which means that no matter how the test when the test was returned yearOfNumber are three, initialized for loop int i = 3 - yearOfNumber.length () , 0 i is not executed for this cycle, so there is no time to test out this problem.
However just after New Year's Day, today is January 3, the gorgeous carried out for this cycle, when the code developers are also taken for granted, and that the initialization of the for loop will always execute, cooked I do not know, only once, the final result i have resulted in an increase, for loop condition i> 0 has been set up, resulting in an infinite loop.

Guess you like

Origin blog.csdn.net/red_sheeps/article/details/78964718