比尔皮尤单例,为什么线程安全?
2022-09-04 22:59:19
为什么Bill Pugh的单例设计模式线程是安全的?
public class Logger {
private Logger() {
// private constructor
}
// static inner class - inner classes are not loaded until they are
// referenced.
private static class LoggerHolder {
private static Logger logger = new Logger();
}
// global access point
public static Logger getInstance() {
return LoggerHolder.logger;
}
//Other methods
}