关于singleton的几个实现
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的几个实现的更多相关文章
- 23种设计模式--单例模式-Singleton
一.单例模式的介绍 单例模式简单说就是掌握系统的至高点,在程序中只实例化一次,这样就是单例模式,在系统比如说你是该系统的登录的第多少人,还有数据库的连接池等地方会使用,单例模式是最简单,最常用的模式之 ...
- 设计模式之单例模式(Singleton)
设计模式之单例模式(Singleton) 设计模式是前辈的一些经验总结之后的精髓,学习设计模式可以针对不同的问题给出更加优雅的解答 单例模式可分为俩种:懒汉模式和饿汉模式.俩种模式分别有不同的优势和缺 ...
- PHP设计模式(四)单例模式(Singleton For PHP)
今天讲单例设计模式,这种设计模式和工厂模式一样,用的非常非常多,同时单例模式比较容易的一种设计模式. 一.什么是单例设计模式 单例模式,也叫单子模式,是一种常用的软件设计模式.在应用这个模式时,单例对 ...
- The Java Enum: A Singleton Pattern [reproduced]
The singleton pattern restricts the instantiation of a class to one object. In Java, to enforce this ...
- 最适合作为Java基础面试题之Singleton模式
看似只是最简单的一种设计模式,可细细挖掘,static.synchronized.volatile关键字.内部类.对象克隆.序列化.枚举类型.反射和类加载机制等基础却又不易理解透彻的Java知识纷纷呼 ...
- 每天一个设计模式-4 单例模式(Singleton)
每天一个设计模式-4 单例模式(Singleton) 1.实际生活的例子 有一天,你的自行车的某个螺丝钉松了,修车铺离你家比较远,而附近的五金店有卖扳手:因此,你决定去五金店买一个扳手,自己把螺丝钉固 ...
- Qt 中使用Singleton模式需小心
在qt中,使用Singleton模式时一定要小心.因为Singleton模式中使用的是静态对象,静态对象是直到程序结束才被释放的,然而,一旦把该静态对象纳入了Qt的父子对象体系,就会导致不明确的行为. ...
- 设计模式之单例模式——Singleton
设计模式之单例模式--Singleton 设计意图: 保证类仅有一个实例,并且可以供应用程序全局使用.为了保证这一点,就需要这个类自己创建自己的对象,并且对外有 ...
- C#面向对象设计模式纵横谈——2.Singleton 单件(创建型模式)
一:模式分类 从目的来看: 创建型(Creational)模式:负责对象创建. 结构型(Structural)模式:处理类与对象间的组合. 行为型(Behavioral)模式:类与对象交互中的职责分配 ...
- 【白话设计模式四】单例模式(Singleton)
转自:https://my.oschina.net/xianggao/blog/616385 0 系列目录 白话设计模式 工厂模式 单例模式 [白话设计模式一]简单工厂模式(Simple Factor ...
随机推荐
- 使用JavaConfig和注解方式实现零xml配置的Spring MVC项目
1. 引言 Spring MVC是Spring框架重要组成部分,是一款非常优秀的Web框架.Spring MVC以DispatcherServlet为核心,通过可配置化的方式去处理各种web请求. 在 ...
- 服务器出现大量的127.0.0.1:3306 TIME_WAIT连接 解决方法 [转载]
netstat -an 192.168.12.13:3306 192.168.12.12:30443 TIME_WAIT 192.168.12.13:3306 192.1 ...
- Nginx禁止目录执行php文件权限
location ~ /dir/.*.(php|php5)?$ { deny all; } 禁止dir目录执行php文件权限 .csharpcode, .csharpcode pre { font-s ...
- jQ 移动端返回顶部代码整理
//返回顶部 $('#btn-scroll').on('touchend',function(){ var T = $(window).scrollTop(); var t = setInterval ...
- spring.factories
在Spring Boot中有一种非常解耦的扩展机制:Spring Factories.这种扩展机制实际上是仿照Java中的SPI扩展机制来实现的. Java SPI机制SPI的全名为Service P ...
- 最长公共子序列(模板 LCSL)
博客: https://www.cnblogs.com/sasuke-/p/5396843.html 模板 #include<iostream>#include<cstdio> ...
- ROS 订阅图像节点
博客 http://blog.csdn.net/github_30605157/article/details/50990493 参考ROS原网站 http://wiki.ros.org/image_ ...
- enq: FB - contention
Troubleshooting 'enq: FB - contention' Format Block Enqueues. (Doc ID 1379986.1) In this DocumentPur ...
- 如何在Qt Creator中导入图标资源
本文主要描述如何在Qt Creator中创建资源文件,并的打入导入图标文件. 查看图标资源文件时,可以在项目的工程文件上鼠标单击右键-Open With-资源编辑器,效果如下图所示: 在项目的工程文件 ...
- [转]QGis2.9在windows下的编译以及二次开发包下载
今天心血来潮,将QGis在github上的代码更新后,又编译了一下.留意到源代码包里面的INSTALL文件有更新,于是本次编译完全基于官方的编译说明.编译过程非常顺利,除了在CMake的第一次conf ...