您的位置:首页 > 编程语言 > Java开发

Java单例类进阶

2015-09-19 16:08 411 查看
Java单例类的基本实现:

public class Singleton {
private Singleton(){
<span style="white-space:pre"> </span>System.out.println("Singleton is create");
}
private static Singleton instance = new Singleton();
public static Singleton getInsatnce(){
<span style="white-space:pre"> </span>return instance;
}
}

但是这种基本实现方法,在JVM加载这个单例类时,单例对象就会被创建,造成一定的浪费。
为避免上述问题,就有了延迟加载的方式:

public class Singleton{
private static Singleton instance = null;
public static synchronized Singleton getInstance(){
if(instance == null){
instance = new Singleton();
}
return instance;
}
}

延迟加载的getInstance()方法有必要加上synchronized关键字,否则在多线程的情况下容易出现多个实例。
添加synchronized关键字后,会使运行速度变慢,所以有了另外一种解决同步关键字低效率:

public class Singleton {

private Singleton(){
System.out.println("Singleton is create");
}

public static Singleton getInsatnce(){
return SingletonHolder.instance;
}

public static void createString(){
System.out.println("createString in Singleton");
}

private static class SingletonHolder{
private static Singleton instance = new Singleton();
}

}


使用内部类来维护单例的实例化,只有在调用getInstance()方法时,才会加载SIngletonHolder类。同时实例的建立是在加载时完成,对线程友好。

通过测试,似乎效果不错,测试代码:

public class Singleton {
private int count = 0;

private Singleton(){
System.out.println("Singleton is create");
}

public static Singleton getInsatnce(){
return SingletonHolder.instance;
}

public static void createString(){
System.out.println("createString in Singleton");
}

private static class SingletonHolder{
private static Singleton instance = new Singleton();
}

public int countAdd(){
count++;
return count;
}

}

import java.text.SimpleDateFormat;
import java.util.Date;

public class Main implements Runnable{
private static int count = 0;
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i=0 ;i<5;i++){
new Thread(new Main()).start();
}
}

@Override
public void run() {
// TODO Auto-generated method stub

for(int i=0; i< 10; i++){
Singleton singleton = Singleton.getInsatnce();
SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd HH:MM:SS");
System.out.println(df.format(new Date())+": "+singleton.countAdd());
}

}

}


输出结果:
Singleton is create
2015-59-19 15:09:224: 2
2015-59-19 15:09:224: 5
2015-59-19 15:09:224: 4
2015-59-19 15:09:225: 6
2015-59-19 15:09:225: 7
2015-59-19 15:09:224: 3
2015-59-19 15:09:224: 1
2015-59-19 15:09:225: 9
2015-59-19 15:09:225: 10
2015-59-19 15:09:226: 11
2015-59-19 15:09:226: 12
2015-59-19 15:09:226: 13
2015-59-19 15:09:226: 14
2015-59-19 15:09:225: 16
2015-59-19 15:09:226: 17
2015-59-19 15:09:225: 8
2015-59-19 15:09:226: 19
2015-59-19 15:09:225: 7
2015-59-19 15:09:226: 20
2015-59-19 15:09:227: 21
2015-59-19 15:09:227: 23
2015-59-19 15:09:227: 24
2015-59-19 15:09:227: 25
2015-59-19 15:09:227: 26
2015-59-19 15:09:226: 18
2015-59-19 15:09:227: 27
2015-59-19 15:09:226: 15
2015-59-19 15:09:228: 28
2015-59-19 15:09:227: 27
2015-59-19 15:09:228: 30
2015-59-19 15:09:228: 31
2015-59-19 15:09:228: 32
2015-59-19 15:09:227: 22
2015-59-19 15:09:229: 34
2015-59-19 15:09:228: 33
2015-59-19 15:09:229: 36
2015-59-19 15:09:228: 29
2015-59-19 15:09:229: 38
2015-59-19 15:09:229: 39
2015-59-19 15:09:230: 40
2015-59-19 15:09:230: 41
2015-59-19 15:09:230: 42
2015-59-19 15:09:229: 37
2015-59-19 15:09:230: 43
2015-59-19 15:09:229: 35
2015-59-19 15:09:232: 44
2015-59-19 15:09:232: 45
2015-59-19 15:09:232: 46
2015-59-19 15:09:232: 47
2015-59-19 15:09:233: 48

以上部分内容来自:https://www.ibm.com/developerworks/cn/java/j-lo-Singleton/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 单例模式