File tree 25 files changed +590
-0
lines changed
p2_singleton_impl_hunger_ThreadSafe_ThreadUnsafe
p2_singleton_impl_lazy_ThreadSafe_DoubleCheckLock
p2_singleton_impl_lazy_ThreadSafe_InnerClass
p2_singleton_impl_lazy_ThreadSafe_Synchronized
p2_singleton_impl_lazy_ThreadSafe_SynchronizedBlock
p2_singleton_impl_lazy_ThreadSafe_ThreadLocal
p2_singleton_impl_lazy_ThreadUnsafe
25 files changed +590
-0
lines changed Original file line number Diff line number Diff line change
1
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2
+ <classpath >
3
+ <classpathentry kind =" src" path =" src" />
4
+ <classpathentry kind =" con" path =" org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7" />
5
+ <classpathentry kind =" output" path =" bin" />
6
+ </classpath >
Original file line number Diff line number Diff line change
1
+ /bin /
Original file line number Diff line number Diff line change
1
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2
+ <projectDescription >
3
+ <name >SingletonPattern</name >
4
+ <comment ></comment >
5
+ <projects >
6
+ </projects >
7
+ <buildSpec >
8
+ <buildCommand >
9
+ <name >org.eclipse.jdt.core.javabuilder</name >
10
+ <arguments >
11
+ </arguments >
12
+ </buildCommand >
13
+ </buildSpec >
14
+ <natures >
15
+ <nature >org.eclipse.jdt.core.javanature</nature >
16
+ </natures >
17
+ </projectDescription >
Original file line number Diff line number Diff line change
1
+ eclipse.preferences.version =1
2
+ org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode =enabled
3
+ org.eclipse.jdt.core.compiler.codegen.targetPlatform =1.7
4
+ org.eclipse.jdt.core.compiler.codegen.unusedLocal =preserve
5
+ org.eclipse.jdt.core.compiler.compliance =1.7
6
+ org.eclipse.jdt.core.compiler.debug.lineNumber =generate
7
+ org.eclipse.jdt.core.compiler.debug.localVariable =generate
8
+ org.eclipse.jdt.core.compiler.debug.sourceFile =generate
9
+ org.eclipse.jdt.core.compiler.problem.assertIdentifier =error
10
+ org.eclipse.jdt.core.compiler.problem.enumIdentifier =error
11
+ org.eclipse.jdt.core.compiler.source =1.7
Original file line number Diff line number Diff line change
1
+ package p2_singleton_impl_hunger_ThreadSafe_ThreadUnsafe ;
2
+
3
+ /**
4
+ * Title: ¶àÏ̲߳âÊÔ
5
+ * Description:
6
+ * @author rico
7
+ * @created 2017Äê5ÔÂ7ÈÕ ÏÂÎç4:41:44
8
+ */
9
+ public class MultiThreadTest {
10
+
11
+ public static void main (String [] args ) {
12
+
13
+ MyThread [] threads = new MyThread [20 ];
14
+ for (int i = 0 ; i < threads .length ; i ++) {
15
+ threads [i ] = new MyThread ();
16
+ }
17
+
18
+ for (int i = 0 ; i < threads .length ; i ++) {
19
+ threads [i ].start ();
20
+ }
21
+ }
22
+ }
23
+
24
+ class MyThread extends Thread {
25
+
26
+ @ Override
27
+ public void run () {
28
+ System .out .println (Singleton .getInstance ());
29
+ }
30
+ }
Original file line number Diff line number Diff line change
1
+ package p2_singleton_impl_hunger_ThreadSafe_ThreadUnsafe ;
2
+
3
+ /**
4
+ * Title: 单线程测试
5
+ * Description:
6
+ * @author rico
7
+ * @created 2017年5月7日 下午4:42:08
8
+ */
9
+ public class SingleThreadTest {
10
+ public static void main (String [] args ) {
11
+
12
+ Singleton instance1 = Singleton .getInstance ();
13
+
14
+ Singleton instance2 = Singleton .getInstance ();
15
+
16
+ //判断是否是同一个对象
17
+ System .out .println (instance1 == instance2 );
18
+
19
+ }
20
+ }
21
+
Original file line number Diff line number Diff line change
1
+ package p2_singleton_impl_hunger_ThreadSafe_ThreadUnsafe ;
2
+
3
+ /**
4
+ * Title: 饿汉式单例模式
5
+ * Description: 饿汉式单例模式本身就是线程安全的
6
+ * @author rico
7
+ * @created 2017年5月7日 下午4:22:29
8
+ */
9
+ public class Singleton {
10
+
11
+ private static Singleton instance = new Singleton ();
12
+
13
+ private Singleton (){
14
+
15
+ }
16
+
17
+ public static Singleton getInstance (){
18
+ return instance ;
19
+ }
20
+ }
Original file line number Diff line number Diff line change
1
+ package p2_singleton_impl_lazy_ThreadSafe_DoubleCheckLock ;
2
+
3
+ /**
4
+ * Title: ¶àÏ̲߳âÊÔ
5
+ * Description:
6
+ * @author rico
7
+ * @created 2017Äê5ÔÂ7ÈÕ ÏÂÎç4:44:10
8
+ */
9
+ public class MultiThreadTest {
10
+
11
+ public static void main (String [] args ) {
12
+
13
+ MyThread [] threads = new MyThread [20 ];
14
+ for (int i = 0 ; i < threads .length ; i ++) {
15
+ threads [i ] = new MyThread ();
16
+ }
17
+
18
+ for (int i = 0 ; i < threads .length ; i ++) {
19
+ threads [i ].start ();
20
+ }
21
+ }
22
+ }
23
+
24
+ class MyThread extends Thread {
25
+
26
+ @ Override
27
+ public void run () {
28
+ Singleton instance = Singleton .getInstance ();
29
+ System .out .println (instance );
30
+ }
31
+ }
Original file line number Diff line number Diff line change
1
+ package p2_singleton_impl_lazy_ThreadSafe_DoubleCheckLock ;
2
+
3
+
4
+ /**
5
+ * Title: 线程安全的懒汉式单例
6
+ * Description: 双重检查锁定
7
+ * @author rico
8
+ * @created 2017年5月7日 下午4:44:57
9
+ */
10
+ public class SingleThreadTest {
11
+
12
+ public static void main (String [] args ) {
13
+
14
+ Singleton instance1 = Singleton .getInstance ();
15
+
16
+ Singleton instance2 = Singleton .getInstance ();
17
+
18
+ //判断是否是同一个对象
19
+ System .out .println (instance1 == instance2 );
20
+
21
+ }
22
+ }
23
+
Original file line number Diff line number Diff line change
1
+ package p2_singleton_impl_lazy_ThreadSafe_DoubleCheckLock ;
2
+
3
+ /**
4
+ * Title: 线程安全的懒汉式单例模式
5
+ * Description: 双重检查锁定,效率较高
6
+ *
7
+ * @author rico
8
+ * @created 2017年5月7日 下午4:22:29
9
+ */
10
+ public class Singleton {
11
+
12
+ // volatile: 防止指令重排序
13
+ private volatile static Singleton instance ;
14
+
15
+ private Singleton () {
16
+
17
+ }
18
+
19
+ public static Singleton getInstance () {
20
+ // 第一次检查
21
+ if (instance == null ){
22
+ // 只在最初几次会进入该同步块,提高效率
23
+ synchronized (Singleton .class ){
24
+ // 第二次检查
25
+ if (instance == null ){
26
+ instance = new Singleton ();
27
+ }
28
+ }
29
+ }
30
+ return instance ;
31
+ }
32
+ }
Original file line number Diff line number Diff line change
1
+ package p2_singleton_impl_lazy_ThreadSafe_InnerClass ;
2
+
3
+ /**
4
+ * Title: ¶àÏ̲߳âÊÔ
5
+ * Description:
6
+ * @author rico
7
+ * @created 2017Äê5ÔÂ7ÈÕ ÏÂÎç4:44:10
8
+ */
9
+ public class MultiThreadTest {
10
+
11
+ public static void main (String [] args ) {
12
+
13
+ MyThread [] threads = new MyThread [20 ];
14
+ for (int i = 0 ; i < threads .length ; i ++) {
15
+ threads [i ] = new MyThread ();
16
+ }
17
+
18
+ for (int i = 0 ; i < threads .length ; i ++) {
19
+ threads [i ].start ();
20
+ }
21
+ }
22
+ }
23
+
24
+ class MyThread extends Thread {
25
+
26
+ @ Override
27
+ public void run () {
28
+ System .out .println (Singleton .getInstance ());
29
+ }
30
+ }
Original file line number Diff line number Diff line change
1
+ package p2_singleton_impl_lazy_ThreadSafe_InnerClass ;
2
+
3
+
4
+ /**
5
+ * Title: 单线程测试
6
+ * Description:
7
+ * @author rico
8
+ * @created 2017年5月7日 下午4:44:57
9
+ */
10
+ public class SingleThreadTest {
11
+
12
+ public static void main (String [] args ) {
13
+
14
+ Singleton instance1 = Singleton .getInstance ();
15
+
16
+ Singleton instance2 = Singleton .getInstance ();
17
+
18
+ //判断是否是同一个对象
19
+ System .out .println (instance1 == instance2 );
20
+
21
+ }
22
+ }
23
+
Original file line number Diff line number Diff line change
1
+ package p2_singleton_impl_lazy_ThreadSafe_InnerClass ;
2
+
3
+ /**
4
+ * Title: 线程安全的懒汉式单例模式
5
+ * Description: 内部类实现
6
+ * @author rico
7
+ * @created 2017年5月7日 下午4:22:29
8
+ */
9
+ public class Singleton {
10
+
11
+ //静态私有内部类
12
+ private static class InnerClass {
13
+ private final static Singleton instance = new Singleton ();
14
+ }
15
+
16
+ private Singleton (){
17
+
18
+ }
19
+
20
+ public static Singleton getInstance (){
21
+ return InnerClass .instance ;
22
+ }
23
+ }
Original file line number Diff line number Diff line change
1
+ package p2_singleton_impl_lazy_ThreadSafe_Synchronized ;
2
+
3
+ /**
4
+ * Title: ¶àÏ̲߳âÊÔ
5
+ * Description:
6
+ * @author rico
7
+ * @created 2017Äê5ÔÂ7ÈÕ ÏÂÎç4:44:10
8
+ */
9
+ public class MultiThreadTest {
10
+
11
+ public static void main (String [] args ) {
12
+
13
+ MyThread [] threads = new MyThread [20 ];
14
+ for (int i = 0 ; i < threads .length ; i ++) {
15
+ threads [i ] = new MyThread ();
16
+ }
17
+
18
+ for (int i = 0 ; i < threads .length ; i ++) {
19
+ threads [i ].start ();
20
+ }
21
+ }
22
+ }
23
+
24
+ class MyThread extends Thread {
25
+
26
+ @ Override
27
+ public void run () {
28
+ Singleton instance = Singleton .getInstance ();
29
+ System .out .println (instance );
30
+ }
31
+ }
Original file line number Diff line number Diff line change
1
+ package p2_singleton_impl_lazy_ThreadSafe_Synchronized ;
2
+
3
+
4
+ /**
5
+ * Title: 单线程测试
6
+ * Description:
7
+ * @author rico
8
+ * @created 2017年5月7日 下午4:44:57
9
+ */
10
+ public class SingleThreadTest {
11
+
12
+ public static void main (String [] args ) {
13
+
14
+ Singleton instance1 = Singleton .getInstance ();
15
+
16
+ Singleton instance2 = Singleton .getInstance ();
17
+
18
+ //判断是否是同一个对象
19
+ System .out .println (instance1 == instance2 );
20
+
21
+ }
22
+ }
23
+
Original file line number Diff line number Diff line change
1
+ package p2_singleton_impl_lazy_ThreadSafe_Synchronized ;
2
+
3
+ /**
4
+ * Title: 线程安全的懒汉式单例模式
5
+ * Description: 同步方法实现,相比双重检查锁定,效率较低
6
+ *
7
+ * @author rico
8
+ * @created 2017年5月7日 下午4:22:29
9
+ */
10
+ public class Singleton {
11
+
12
+ private static Singleton instance ;
13
+
14
+ private Singleton () {
15
+
16
+ }
17
+
18
+ // 同步方法
19
+ public synchronized static Singleton getInstance () {
20
+ if (instance == null ){
21
+ instance = new Singleton ();
22
+ }
23
+ return instance ;
24
+ }
25
+ }
Original file line number Diff line number Diff line change
1
+ package p2_singleton_impl_lazy_ThreadSafe_SynchronizedBlock ;
2
+
3
+ /**
4
+ * Title: ¶àÏ̲߳âÊÔ
5
+ * Description:
6
+ * @author rico
7
+ * @created 2017Äê5ÔÂ7ÈÕ ÏÂÎç4:44:10
8
+ */
9
+ public class MultiThreadTest {
10
+
11
+ public static void main (String [] args ) {
12
+
13
+ MyThread [] threads = new MyThread [20 ];
14
+ for (int i = 0 ; i < threads .length ; i ++) {
15
+ threads [i ] = new MyThread ();
16
+ }
17
+
18
+ for (int i = 0 ; i < threads .length ; i ++) {
19
+ threads [i ].start ();
20
+ }
21
+ }
22
+ }
23
+
24
+ class MyThread extends Thread {
25
+
26
+ @ Override
27
+ public void run () {
28
+ Singleton instance = Singleton .getInstance ();
29
+ System .out .println (instance );
30
+ }
31
+ }
You can’t perform that action at this time.
0 commit comments