?????????LogUtil?????????л?????????????new???????????LogUtil?????????????????sLogUtil??о???????????????????????????е?getInstance??????????LogUtil?????????????????????ж????sLogUtil????new??????μ?LogUtil???????????????sLogUtil??????????????浱???????????LogUtil?????????????????????????????????????????·????
????LogUtil.getInstance().debug("Hello World");
??????????汾???????leader?????????Ц??Ц?????“????????????????????????????????bug?????
?????????????????????????????????????????????bug???
???????leader?????????????????????????????????????????????????????п???????????д??????????????????????????
????public static LogUtil getInstance() {
????if (sLogUtil == null) {
????sLogUtil = new LogUtil();
????}
????return sLogUtil;
????}
???????????????????????????getInstance?????????????????????2?У??????е?3?У????????????????е????2?У???????sLogUtil????null???????????if?ж????檔?????????????????????????????????????????
?????????????????????????????????????????????????????????????????????????£?
????public synchronized static LogUtil getInstance() {
????if (sLogUtil == null) {
????sLogUtil = new LogUtil();
????}
????return sLogUtil;
????}
?????????????????????????????????getInstance??????????????Ч???????????????????????????
???????leader????????′???????“???????????????????п??????????????????????????δ?????????????”
????????????????????????????????
???????leaderЦЦ??“???y??????β???bug??????????????????Щ??????£????????getInstance????????????synchronized??????????????getInstace???????????????????????????????е?Ч??????????????????δ???LogUtil??????????????????????????????????????????????á?”
?????????synchronized?????????????????????????????????嵱?У?
public static LogUtil getInstance() {
synchronized (LogUtil.class) {
if (sLogUtil == null) {
sLogUtil = new LogUtil();
}
return sLogUtil;
}
}
????????Ч????????????????synchronized?????μ???????synchronized?????????????ж???????????
public static LogUtil getInstance() {
if (sLogUtil == null) {
synchronized (LogUtil.class) {
if (sLogUtil == null) {
sLogUtil = new LogUtil();
}
}
}
return sLogUtil;
}
??????????????????????sLogUtil??????????????????????3?У????????????????sLogUtil????????????????????????3????????????getInstance????????????????????????Ч??????????????????
???????鯔??????????????????????????ó????????????????
???????leader?????????????“??????????????????(Double-Check Locking)????????????????????????????????????????顣”
???????????????????????????????????????????????????