惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 【当耐特】
Help Net Security
Help Net Security
P
Proofpoint News Feed
J
Java Code Geeks
爱范儿
爱范儿
Last Week in AI
Last Week in AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
F
Full Disclosure
Google DeepMind News
Google DeepMind News
H
Help Net Security
G
Google Developers Blog
Jina AI
Jina AI
Vercel News
Vercel News
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
Lohrmann on Cybersecurity
S
Schneier on Security
Microsoft Azure Blog
Microsoft Azure Blog
IT之家
IT之家
Security Archives - TechRepublic
Security Archives - TechRepublic
阮一峰的网络日志
阮一峰的网络日志
N
News and Events Feed by Topic
GbyAI
GbyAI
B
Blog
O
OpenAI News
博客园_首页
Cisco Talos Blog
Cisco Talos Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Hacker News: Ask HN
Hacker News: Ask HN
TaoSecurity Blog
TaoSecurity Blog
腾讯CDC
MongoDB | Blog
MongoDB | Blog
M
MIT News - Artificial intelligence
C
Cybersecurity and Infrastructure Security Agency CISA
Cyberwarzone
Cyberwarzone
Webroot Blog
Webroot Blog
Simon Willison's Weblog
Simon Willison's Weblog
Y
Y Combinator Blog
C
Cisco Blogs
A
Arctic Wolf
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
The Exploit Database - CXSecurity.com
Security Latest
Security Latest
AI
AI
W
WeLiveSecurity
aimingoo的专栏
aimingoo的专栏
The Register - Security
The Register - Security
Project Zero
Project Zero
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
Netflix TechBlog - Medium
Blog — PlanetScale
Blog — PlanetScale

博客园 - L.Zhang

.Net Remoting RMI框架 自己开发连接池 JDBC访问数据库 MyEclipse下开发Web Service 使用传统的XMLHttpRequest发出Ajax请求 XML CDATA XPath 数据库操作的sql脚本 直接选择排序 直接插入排序 气泡排序 Builder 生成器模式(创建型模式) Abstract Factory 抽象工厂模式(创建型模式) Factory Method 工厂方法模式(创建型模式) 使用Profile Service 服务端如何使用Session 让服务端返回xml 用Get方式访问
Singleton单件模式(创建型模式)
L.Zhang · 2007-11-01 · via 博客园 - L.Zhang

1.简单实现
这种方式的实现对于线程来说并不是安全的,因为在多线程的环境下有可能得到Singleton类的多个实例

public sealed class Singleton
{
    
//静态字段
    static Singleton instance = null;
    
//私有构造函数
    private Singleton()
    {

    }

//静态属性
    public static Singleton Instance
    {
        
get
        {
            
if (instance == null)
            {
                instance 
= new Singleton();
            }
            
return instance;
        }
    }
}

2.线程安全
在同一个时刻加了锁的那部分程序只有一个线程可以进入。这种情况下,对象实例由最先进入的那个线程创建,后来的线程在进入时(instence == null)为假,不会再去创建对象实例了。但是这种实现方式增加了额外的开销,损失了性能。

public sealed class Singleton
{
    
//静态字段
    static Singleton instance = null;
    
//静态只读锁
    static readonly object padlock = new object();
    
//私有构造函数
    private Singleton()
    {
    }
    
//静态属性
    public static Singleton Instance
    {
        
get
        {
            
lock (padlock)
            {
                
if (instance == null)
                {
                    instance 
= new Singleton();
                }
                
return instance;
            }
        }
    }
}

3.双重锁定

public sealed class Singleton
{
    
static Singleton instance = null;
    
static readonly object padlock = new object();private Singleton()
    {
    }
public static Singleton Instance
    {
        
get
        {
            
if (instance == null)
            {
                
lock (padlock)
                {
                    
if (instance == null)
                    {
                        instance 
= new Singleton();
                    }
                }
            }
            
return instance;
        }
    }
}

    要点
• Singleton模式中的实例构造器可以设置为protected以允许子类派生。
• Singleton模式一般不要支持ICloneable接口,因为这可能会导致多个对象实例,与Singleton模式的初衷违背。
• Singleton模式一般不要支持序列化,因为这也有可能导致多个对象实例,同样与Singleton模式的初衷违背。
• Singletom模式只考虑到了对象创建的管理,没有考虑对象销毁的管理。就支持垃圾回收的平台和对象的开销来
  讲,我们一般没有必要对其销毁进行特殊的管理。
• 不能应对多线程环境:在多线程环境下,使用Singleton模式仍然有可能得到Singleton类的多个实例对象。