Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b30e544

Browse files
🎉 readme & singleton
1 parent 898a2cf commit b30e544

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

‎README.md‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
![](http://upload-images.jianshu.io/upload_images/7802425-9eb1bcd006e34aa6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
1+
<p align=center>
2+
<a href="https://github.com/Jasonandy/Java-Core-Advanced">
3+
<img src="http://upload-images.jianshu.io/upload_images/7802425-9eb1bcd006e34aa6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="Java-Core-Advanced" >
4+
</a>
5+
</p>
6+
7+
<p align=center>
8+
You are what you want to be.
9+
</p>
10+
11+
<p align="center">
12+
<a href="#"><img src="https://img.shields.io/wercker/ci/wercker/docs.svg?style=for-the-badge" alt=""></a>
13+
<a href="#"><img src="https://img.shields.io/badge/Author-Jason-orange.svg?style=for-the-badge" alt=""></a>
14+
</p>
15+
216

317
# Ucaner
418
> You are what you want to be. - w.b

‎src/cn/ucaner/pattern/create/singleton/Singleton.java‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,30 @@
2323
*/
2424
public class Singleton {
2525

26-
//防止外部创建实例 私有
26+
/**
27+
* 防止外部创建实例 私有
28+
* Singleton.
29+
*/
2730
private Singleton() {
2831

2932
}
3033

31-
//唯一实例
34+
/**
35+
* 唯一实例
36+
* 内存可见性:通俗来说就是,线程A对一个volatile变量的修改,对于其它线程来说是可见的,即线程每次获取volatile变量的值都是最新的。
37+
* https://www.cnblogs.com/chengxiao/p/6528109.html
38+
*/
3239
private static volatile Singleton mInstance;
3340

3441
public static Singleton getInstance() {
35-
//第一个锁,如果没有实例
36-
if (mInstance == null) {
37-
synchronized (Singleton.class) {
38-
//第二个锁,如果没有任何线程创建Singleton实例
42+
43+
if (mInstance == null) {//第一个锁,如果没有实例
44+
45+
/**
46+
* 第二个锁,如果没有任何线程创建Singleton实例 对象锁 - 若多个线程拥有同一个MyObject类的对象,则这些方法只能以同步的方式执行
47+
* https://www.cnblogs.com/hapjin/p/5452663.html
48+
*/
49+
synchronized (Singleton.class) {
3950
if (mInstance == null) {
4051
mInstance = new Singleton();
4152
}
@@ -44,7 +55,6 @@ public static Singleton getInstance() {
4455
return mInstance;
4556
}
4657

47-
4858
public static void main(String[] args) {
4959
System.out.println(mInstance);
5060
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /