Skip to content

Commit 1f7286a

Browse files
author
sgr
committed
提交单例模式代码
1 parent c8e6e88 commit 1f7286a

File tree

5 files changed

+132
-0
lines changed

5 files changed

+132
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Created by .ignore support plugin (hsz.mobi)
2+
.idea
3+
*.iml

pom.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>os.dt.design.patterns</groupId>
7+
<artifactId>java-design-patterns</artifactId>
8+
<packaging>pom</packaging>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<modules>
12+
<module>singleton</module>
13+
</modules>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>junit</groupId>
18+
<artifactId>junit</artifactId>
19+
<version>4.12</version>
20+
</dependency>
21+
</dependencies>
22+
23+
24+
</project>

singleton/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
5+
<parent>
6+
<artifactId>java-design-patterns</artifactId>
7+
<groupId>os.dt.design.patterns</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
11+
<modelVersion>4.0.0</modelVersion>
12+
<artifactId>singleton</artifactId>
13+
14+
15+
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package os.dt.design.patterns.singleton;
2+
3+
/**
4+
* 饿汉式写法
5+
* Created by songgr on 2019/10/17.
6+
*/
7+
public class HungryPresident {
8+
9+
private static final HungryPresident INSTANCE = new HungryPresident();
10+
11+
// 私有化构造方法
12+
private HungryPresident(){
13+
14+
}
15+
16+
public static HungryPresident getInstance() {
17+
System.out.println("getInstance");
18+
return INSTANCE;
19+
}
20+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package os.dt.design.patterns.singleton;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
import java.util.concurrent.ExecutorService;
10+
import java.util.concurrent.Executors;
11+
import java.util.concurrent.TimeUnit;
12+
import java.util.function.Function;
13+
import java.util.function.Supplier;
14+
15+
/**
16+
* 测试
17+
* Created by songgr on 2019/10/17.
18+
*/
19+
20+
public class UnitTest {
21+
22+
private List<Object> objs = null;
23+
24+
class CreateObj implements Runnable {
25+
private Object obj = null;
26+
27+
private Supplier function;
28+
public CreateObj(Supplier function) {
29+
this.function = function;
30+
}
31+
32+
public void run() {
33+
System.out.println(Thread.currentThread().getName() + ": I get a obj.");
34+
Object obj = function.get();
35+
objs.add(obj);
36+
}
37+
}
38+
39+
@Before
40+
public void init() {
41+
objs = new ArrayList<Object>(4);
42+
}
43+
44+
45+
@Test
46+
public void hungryPresident() throws InterruptedException {
47+
ExecutorService fixedThreadPool = Executors.newFixedThreadPool(4);
48+
49+
/*Supplier function = () -> HungryPresident.getInstance();
50+
System.out.println(function.getClass());
51+
function.get();*/
52+
53+
fixedThreadPool.submit(new CreateObj(() -> HungryPresident.getInstance()));
54+
fixedThreadPool.submit(new CreateObj(() -> HungryPresident.getInstance()));
55+
fixedThreadPool.submit(new CreateObj(() -> HungryPresident.getInstance()));
56+
fixedThreadPool.submit(new CreateObj(() -> HungryPresident.getInstance()));
57+
58+
fixedThreadPool.awaitTermination(4, TimeUnit.SECONDS);
59+
60+
}
61+
62+
@After
63+
public void check() {
64+
assert objs.size() > 1;
65+
objs.stream().reduce((a,b) -> a == b);
66+
objs.stream().forEach(a -> System.out.println(a.getClass()));
67+
}
68+
69+
70+
}

0 commit comments

Comments
 (0)