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;
}
}
输出结果:
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/
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对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树
- [原创]java局域网聊天系统