本文介绍了维基百科的单例模式实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是指,在同一部分链接的文章初始化请求持有人成语中。
See the "How it works", in the article "Initialization on demand holder idiom" linked from the same section.
简而言之,首次调用 getInstance()
时会发生什么:
In a nutshell, here's what happens when you call getInstance()
the first time:
- JVM看到对
SingletonHolder
的引用,它从未见过。 - 执行是暂停,而初始化
SingletonHolder
。这包括运行任何静态初始化,包括单例实例。 - 执行恢复。任何其他线程调用
getInstance()
同时会看到SingletonHolder
已被初始化。 Java规范保证类初始化是线程安全的。
- The JVM sees a reference to
SingletonHolder
it has never seen referenced before. - Execution is paused while it initializes
SingletonHolder
. This includes running any static initialization, which includes the singleton instance. - Execution resumes. Any other threads calling
getInstance()
at the same time will see thatSingletonHolder
is already initialized. The Java spec guarantees that class initialization is thread-safe.
这篇关于维基百科的单例模式实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!