public class Singleton {
public static void main(String[] args) {
Singleton s1 = Singleton.getInstance();
Singleton s2 = Singleton.getInstance();
System.out.println(s1 == s2);
}
private static Singleton instance;
/**将构造器使用private修饰,隐藏该构造器**/
private Singleton(){
System.out.println("Singleton被构造!");
}
/**线程安全,但性能有待优化,这样写每次调用getInstance方法都需要同步。
实际上只是第一次加载需要同步**/
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
public class Singleton2 {
/**注意volatile的修饰,有待改进吧**/
private static Lock lock=new ReentrantLock();
private static volatile Singleton2 uniqueInstance;
private Singleton2() {
}
public static void main(String[] args) {
Singleton2 s1 = Singleton2.getInstance();
Singleton2 s2 = Singleton2.getInstance();
System.out.println(s1 == s2);
} public static Singleton2 getInstance() {
if(uniqueInstance == null) {
lock.lock();
try {
/**double check**/
if(uniqueInstance == null)
uniqueInstance = new Singleton2();
}catch (Exception e){
}finally {
lock.unlock();
}
}
return uniqueInstance;
}
}
public class SingletonTest {
/**注意volatile关键字,这个方法还是不错的**/
private static volatile SingletonTest instance = null; private SingletonTest() {
} public static void main(String[] args) {
SingletonTest s1 = SingletonTest.getInstance();
SingletonTest s2 = SingletonTest.getInstance();
System.out.println(s1 == s2);
} private static synchronized void syncInit() {
if (instance == null) {
instance = new SingletonTest();
}
} public static SingletonTest getInstance() {
if (instance == null) {
syncInit();
}
return instance;
}
}
public class Singleton_thread {
/**私有构造方法,防止被实例化**/
private Singleton_thread() {
} public static void main(String[] args) {
Singleton_thread s1 = Singleton_thread.getInstance();
Singleton_thread s2 = Singleton_thread.getInstance();
System.out.println(s1 == s2);
} /**此处使用一个内部类来维护单例,类的加载线程互斥**/
private static class SingletonFactory {
private static Singleton_thread instance = new Singleton_thread();
} /**获取实例**/
public static Singleton_thread getInstance() {
return SingletonFactory.instance;
} }

关于singleton的几个实现的更多相关文章

  1. 23种设计模式--单例模式-Singleton

    一.单例模式的介绍 单例模式简单说就是掌握系统的至高点,在程序中只实例化一次,这样就是单例模式,在系统比如说你是该系统的登录的第多少人,还有数据库的连接池等地方会使用,单例模式是最简单,最常用的模式之 ...

  2. 设计模式之单例模式(Singleton)

    设计模式之单例模式(Singleton) 设计模式是前辈的一些经验总结之后的精髓,学习设计模式可以针对不同的问题给出更加优雅的解答 单例模式可分为俩种:懒汉模式和饿汉模式.俩种模式分别有不同的优势和缺 ...

  3. PHP设计模式(四)单例模式(Singleton For PHP)

    今天讲单例设计模式,这种设计模式和工厂模式一样,用的非常非常多,同时单例模式比较容易的一种设计模式. 一.什么是单例设计模式 单例模式,也叫单子模式,是一种常用的软件设计模式.在应用这个模式时,单例对 ...

  4. The Java Enum: A Singleton Pattern [reproduced]

    The singleton pattern restricts the instantiation of a class to one object. In Java, to enforce this ...

  5. 最适合作为Java基础面试题之Singleton模式

    看似只是最简单的一种设计模式,可细细挖掘,static.synchronized.volatile关键字.内部类.对象克隆.序列化.枚举类型.反射和类加载机制等基础却又不易理解透彻的Java知识纷纷呼 ...

  6. 每天一个设计模式-4 单例模式(Singleton)

    每天一个设计模式-4 单例模式(Singleton) 1.实际生活的例子 有一天,你的自行车的某个螺丝钉松了,修车铺离你家比较远,而附近的五金店有卖扳手:因此,你决定去五金店买一个扳手,自己把螺丝钉固 ...

  7. Qt 中使用Singleton模式需小心

    在qt中,使用Singleton模式时一定要小心.因为Singleton模式中使用的是静态对象,静态对象是直到程序结束才被释放的,然而,一旦把该静态对象纳入了Qt的父子对象体系,就会导致不明确的行为. ...

  8. 设计模式之单例模式——Singleton

                        设计模式之单例模式--Singleton 设计意图: 保证类仅有一个实例,并且可以供应用程序全局使用.为了保证这一点,就需要这个类自己创建自己的对象,并且对外有 ...

  9. C#面向对象设计模式纵横谈——2.Singleton 单件(创建型模式)

    一:模式分类 从目的来看: 创建型(Creational)模式:负责对象创建. 结构型(Structural)模式:处理类与对象间的组合. 行为型(Behavioral)模式:类与对象交互中的职责分配 ...

  10. 【白话设计模式四】单例模式(Singleton)

    转自:https://my.oschina.net/xianggao/blog/616385 0 系列目录 白话设计模式 工厂模式 单例模式 [白话设计模式一]简单工厂模式(Simple Factor ...

随机推荐

  1. Alpha版本 - 测试报告

    Alpha版本 - 测试报告 总体测试计划 前端 模块 子模块 测试项 预期结果 测试工具 执行人 登录/注册模块 无网络 提示无网异常 robolectric 陈龙江 登录 输入用户名/密码为空,点 ...

  2. C#Url处理类

    using System; using System.Text.RegularExpressions; using System.Web; using System.Collections.Speci ...

  3. nginx跟apache访问方法

    ifconfig 在浏览器中输入ip即可访问 centos安装nginx环境 1:进入 cd /usr/local/src  //下载文件放到这个目录中 2:wget http://nginx.org ...

  4. select * 和select 所有字段的区别

    文章取自http://blog.csdn.net/u014305991/article/details/44964171 MySQL 5.1.37 表记录数41,547,002,即4000w行 使用远 ...

  5. 邻接表&链式前向星

    链式前向星: 适合点多.边少的情况 不适用于大量遍历出边的题目(因为cache miss) 邻接表: 如果用邻接表来实现的话,一般就用vector嘛,我们都知道vector都是自动扩容的,在空间满了以 ...

  6. Lodop 打印控件

    1.下载 2.使用 一 下载安装控件 官网下载地址:http://www.lodop.net/download.html 参考:http://www.c-lodop.com/demolist/Prin ...

  7. 【Codeforces 1132E】Knapsack

    Codeforces 1132 E 题意:给\(cnt_i\)个\(i\)(\(1\leq i\leq 8\)),问用这些数所能构成的最大的不超过\(W\)的数. 思路:随机+贪心... 我们考虑将贪 ...

  8. 【Codeforces 1120A】Diana and Liana

    Codeforces 1120 A 题意:给\(n\)个数\(a_1..a_n\),要从其中删去小于等于\(n-m\times k\)个数,使得将这个数组分成\(k\)个一段的序列时有至少一段满足以下 ...

  9. java 面向对象String类

    1.String类:String 是不可变字符序列 1) char charAt(int index)返回字符串中第 index 个字符. 2) boolean equalsIgnoreCase(St ...

  10. 关于TerraBuilder的扩展开发

    熟悉Skyline的朋友,可能会发现,在最新的6.6的产品体系中,TerraBuilder中用于生成三维地形场景的模块,改成了TerrainBuilder. 通常情况下,这款软件模块,我们主要用它来进 ...