Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
QZero_0
java-design-patterns
提交
6413c4d2
J
java-design-patterns
项目概览
QZero_0
/
java-design-patterns
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
java-design-patterns
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
6413c4d2
编写于
10月 02, 2015
作者:
I
Ilkka Seppala
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Added more Singleton documentation #188
上级
11cdd20f
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
30 addition
and
16 deletion
+30
-16
singleton/src/main/java/com/iluwatar/singleton/App.java
singleton/src/main/java/com/iluwatar/singleton/App.java
+29
-15
singleton/src/main/java/com/iluwatar/singleton/IvoryTower.java
...eton/src/main/java/com/iluwatar/singleton/IvoryTower.java
+1
-1
未找到文件。
singleton/src/main/java/com/iluwatar/singleton/App.java
浏览文件 @
6413c4d2
/**
* Singleton pattern.
*/
package
com.iluwatar.singleton
;
/**
* Singleton pattern ensures that the class
({@link IvoryTower}) can have only one
*
existing instance per Java classloader instance
and provides global access to it.
* Singleton pattern ensures that the class
can have only one existing instance per Java classloader instance
* and provides global access to it.
* <p/>
* http://stackoverflow.com/questions/70689/what-is-an-efficient-way-to-implement-a-singleton-pattern-in-java
* <p/>
* The risk of this pattern is that bugs resulting from setting a singleton up in
* One of the risks of this pattern is that bugs resulting from setting a singleton up in
* a distributed environment can be tricky to debug, since it will work fine if you
* debug with a single classloader. Additionally, these problems can crop up a while
* after the implementation of a singleton, since they may start out synchronous and
* only become async with time, so you it may not be clear why you are seeing certain
* changes in behaviour.
* <p/>
* http://stackoverflow.com/questions/17721263/singleton-across-jvm-or-application-instance-or-tomcat-instance
* There are many ways to implement the Singleton. The first one is the eagerly initialized instance in
* {@link IvoryTower}. Eager initialization implies that the implementation is thread safe. If you can
* afford giving up control of the instantiation moment, then this implementation will suit you fine.
* <p/>
* The other option to implement eagerly initialized Singleton is enum based Singleton. The example is
* found in {@link EnumIvoryTower}. At first glance the code looks short and simple. However, you should
* be aware of the downsides including committing to implementation strategy, extending the enum class,
* serializability and restrictions to coding. These are extensively discussed in Stack Overflow:
* http://programmers.stackexchange.com/questions/179386/what-are-the-downsides-of-implementing-a-singleton-with-javas-enum
* <p/>
* {@link ThreadSafeLazyLoadedIvoryTower} is a Singleton implementation that is initialized on demand.
* The downside is that it is very slow to access since the whole access method is synchronized.
* <p/>
* Another Singleton implementation that is initialized on demand is found in {@link ThreadSafeDoubleCheckLocking}. It
* is somewhat faster than {@link ThreadSafeLazyLoadedIvoryTower} since it doesn't synchronize the whole access method
* but only the method internals on specific conditions.
* <p/>
* Yet another way to implement thread safe lazily initialized Singleton can be found in {@link InitializingOnDemandHolderIdiom}.
* However, this implementation requires at least Java 8 API level to work.
*/
public
class
App
{
...
...
@@ -48,16 +60,18 @@ public class App {
System
.
out
.
println
(
"enumIvoryTower1="
+
enumIvoryTower1
);
System
.
out
.
println
(
"enumIvoryTower2="
+
enumIvoryTower2
);
// double checked locking
ThreadSafeDoubleCheckLocking
dcl1
=
ThreadSafeDoubleCheckLocking
.
getInstance
();
System
.
out
.
println
(
dcl1
);
ThreadSafeDoubleCheckLocking
dcl2
=
ThreadSafeDoubleCheckLocking
.
getInstance
();
System
.
out
.
println
(
dcl2
);
// initialize on demand holder idiom
InitializingOnDemandHolderIdiom
demandHolderIdiom
=
InitializingOnDemandHolderIdiom
.
getInstance
();
System
.
out
.
println
(
demandHolderIdiom
);
InitializingOnDemandHolderIdiom
demandHolderIdiom2
=
InitializingOnDemandHolderIdiom
.
getInstance
();
System
.
out
.
println
(
demandHolderIdiom2
);
ThreadSafeDoubleCheckLocking
dcl1
=
ThreadSafeDoubleCheckLocking
.
getInstance
();
System
.
out
.
println
(
dcl1
);
ThreadSafeDoubleCheckLocking
dcl2
=
ThreadSafeDoubleCheckLocking
.
getInstance
();
System
.
out
.
println
(
dcl2
);
}
}
singleton/src/main/java/com/iluwatar/singleton/IvoryTower.java
浏览文件 @
6413c4d2
...
...
@@ -10,7 +10,7 @@ public final class IvoryTower {
/**
* Static to class instance of the class.
*/
private
static
IvoryTower
instance
=
new
IvoryTower
();
private
static
final
IvoryTower
instance
=
new
IvoryTower
();
/**
* Private constructor so nobody can instantiate the class.
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录