Skip to content

Commit 5eb03ca

Browse files
author
chenweijie
committed
添加单例相关的例子
1 parent a2f98d7 commit 5eb03ca

File tree

26 files changed

+536
-0
lines changed

26 files changed

+536
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.chen.api.util.thread.study.chapter6.singleton1;
2+
3+
/**
4+
* 饿汉模式
5+
*
6+
* @author : chen weijie
7+
* @Date: 2018-05-21 23:31
8+
*/
9+
public class MyObject {
10+
11+
private static MyObject myObject = new MyObject();
12+
13+
private MyObject() {
14+
}
15+
16+
public static MyObject getInstance() {
17+
//此代码版本为立即加载,此代码的缺点是不能有其他实例变量
18+
//因为getInstance()没有同步,所以有可能出现线程安全的问题
19+
return myObject;
20+
}
21+
22+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.chen.api.util.thread.study.chapter6.singleton1;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2018-05-21 23:36
6+
*/
7+
public class MyThread extends Thread {
8+
9+
@Override
10+
public void run() {
11+
System.out.println(MyObject.getInstance().hashCode());
12+
}
13+
14+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.chen.api.util.thread.study.chapter6.singleton1;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2018-05-21 23:37
6+
*/
7+
public class Run {
8+
9+
10+
public static void main(String[] args) {
11+
12+
13+
MyThread myThreadA = new MyThread();
14+
MyThread myThreadB = new MyThread();
15+
MyThread myThreadC = new MyThread();
16+
myThreadA.start();
17+
myThreadB.start();
18+
myThreadC.start();
19+
20+
21+
}
22+
23+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.chen.api.util.thread.study.chapter6.singleton2;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2018-05-21 23:38
6+
*/
7+
public class MyObject {
8+
9+
private static MyObject myObject = null;
10+
11+
private MyObject() {
12+
}
13+
14+
//懒汉模式,在多线程的情况下会出现同步问题。
15+
public static MyObject getInstance() {
16+
try {
17+
if (myObject == null) {
18+
Thread.sleep(1000);
19+
myObject = new MyObject();
20+
}
21+
} catch (InterruptedException e) {
22+
e.printStackTrace();
23+
}
24+
return myObject;
25+
}
26+
27+
28+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.chen.api.util.thread.study.chapter6.singleton2;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2018-05-21 23:36
6+
*/
7+
public class MyThread extends Thread {
8+
9+
@Override
10+
public void run() {
11+
System.out.println(MyObject.getInstance().hashCode());
12+
}
13+
14+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.chen.api.util.thread.study.chapter6.singleton2;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2018-05-21 23:41
6+
*/
7+
public class Run {
8+
9+
public static void main(String[] args) {
10+
11+
MyThread myThread1 = new MyThread();
12+
MyThread myThread2 = new MyThread();
13+
MyThread myThread3 = new MyThread();
14+
15+
myThread1.start();
16+
myThread2.start();
17+
myThread3.start();
18+
19+
}
20+
21+
22+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.chen.api.util.thread.study.chapter6.singleton3;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2018-05-21 23:48
6+
*/
7+
public class MyObject {
8+
9+
10+
private static MyObject myObject = null;
11+
12+
private MyObject() {
13+
}
14+
15+
//整个方法上锁,同步方法的效率太低了
16+
synchronized public static MyObject getInstance() {
17+
try {
18+
if (myObject == null) {
19+
Thread.sleep(2000);
20+
myObject = new MyObject();
21+
}
22+
} catch (InterruptedException e) {
23+
e.printStackTrace();
24+
}
25+
return myObject;
26+
}
27+
28+
29+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.chen.api.util.thread.study.chapter6.singleton3;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2018-05-21 23:36
6+
*/
7+
public class MyThread extends Thread {
8+
9+
@Override
10+
public void run() {
11+
System.out.println(MyObject.getInstance().hashCode());
12+
}
13+
14+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.chen.api.util.thread.study.chapter6.singleton3;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2018-05-21 23:41
6+
*/
7+
public class Run {
8+
9+
public static void main(String[] args) {
10+
11+
MyThread myThread11 = new MyThread();
12+
MyThread myThread22 = new MyThread();
13+
MyThread myThread33 = new MyThread();
14+
15+
myThread11.start();
16+
myThread22.start();
17+
myThread33.start();
18+
19+
}
20+
21+
22+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.chen.api.util.thread.study.chapter6.singleton4;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2018-05-21 23:54
6+
*/
7+
public class MyObject {
8+
9+
private static MyObject myObject = null;
10+
11+
private MyObject() {
12+
}
13+
14+
public static MyObject getInstance() {
15+
//同步代码块 getInstance方法中的所有代码都上锁,这样做也会降低运行效率
16+
synchronized (MyObject.class) {
17+
if (myObject == null) {
18+
myObject = new MyObject();
19+
}
20+
}
21+
return myObject;
22+
}
23+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.chen.api.util.thread.study.chapter6.singleton4;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2018-05-21 23:56
6+
*/
7+
public class MyThread extends Thread {
8+
9+
@Override
10+
public void run() {
11+
System.out.println(MyObject.getInstance().hashCode());
12+
}
13+
14+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.chen.api.util.thread.study.chapter6.singleton4;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2018-05-21 23:57
6+
*/
7+
public class Run {
8+
9+
public static void main(String[] args) {
10+
11+
MyThread myThread4 = new MyThread();
12+
MyThread myThread5 = new MyThread();
13+
MyThread myThread6 = new MyThread();
14+
15+
16+
myThread4.start();
17+
myThread5.start();
18+
myThread6.start();
19+
20+
21+
}
22+
23+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.chen.api.util.thread.study.chapter6.singleton5;
2+
3+
4+
/**
5+
* @author : chen weijie
6+
* @Date: 2018-05-22 00:01
7+
*/
8+
public class MyObject {
9+
10+
private static MyObject myObject = null;
11+
12+
private MyObject() {
13+
}
14+
15+
//虽然对实例化对象的关键代码进行了同步,从代码结构上看效率得到了提升,但是无法解决多线程安全的问题。
16+
//同时又多个线程访问if (myObject == null) ,得到的结果可能不一样。
17+
public static MyObject getInstance() {
18+
if (myObject == null) {
19+
synchronized (MyObject.class) {
20+
myObject = new MyObject();
21+
}
22+
}
23+
return myObject;
24+
}
25+
26+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.chen.api.util.thread.study.chapter6.singleton5;
2+
3+
4+
/**
5+
* @author : chen weijie
6+
* @Date: 2018-05-21 23:56
7+
*/
8+
public class MyThread extends Thread {
9+
10+
@Override
11+
public void run() {
12+
System.out.println(MyObject.getInstance().hashCode());
13+
}
14+
15+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.chen.api.util.thread.study.chapter6.singleton5;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2018-05-21 23:57
6+
*/
7+
public class Run {
8+
9+
public static void main(String[] args) {
10+
11+
MyThread myThread4 = new MyThread();
12+
MyThread myThread5 = new MyThread();
13+
MyThread myThread6 = new MyThread();
14+
15+
16+
myThread4.start();
17+
myThread5.start();
18+
myThread6.start();
19+
20+
21+
}
22+
23+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.chen.api.util.thread.study.chapter6.singleton6;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2018-05-22 00:14
6+
*/
7+
public class MyObject {
8+
9+
private volatile static MyObject myObject = null;
10+
11+
private MyObject() {
12+
}
13+
14+
//使用DCL双检查锁机制,既保证了同步代码的异步执行,又保证了单例的效果。
15+
public static MyObject getInstance() {
16+
if (myObject == null) {
17+
synchronized (MyObject.class) {
18+
myObject = new MyObject();
19+
}
20+
}
21+
return myObject;
22+
}
23+
24+
25+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.chen.api.util.thread.study.chapter6.singleton6;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2018-05-22 00:15
6+
*/
7+
public class MyThread extends Thread {
8+
9+
@Override
10+
public void run() {
11+
System.out.println(MyObject.getInstance().hashCode());
12+
}
13+
14+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.chen.api.util.thread.study.chapter6.singleton6;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2018-05-22 00:16
6+
*/
7+
public class Run {
8+
9+
10+
public static void main(String[] args) {
11+
12+
MyThread myThread8 = new MyThread();
13+
MyThread myThread9 = new MyThread();
14+
MyThread myThread0 = new MyThread();
15+
16+
myThread0.start();
17+
myThread8.start();
18+
myThread9.start();
19+
20+
}
21+
22+
}

0 commit comments

Comments
 (0)