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

推荐订阅源

C
CXSECURITY Database RSS Feed - CXSecurity.com
Stack Overflow Blog
Stack Overflow Blog
月光博客
月光博客
T
Threat Research - Cisco Blogs
小众软件
小众软件
有赞技术团队
有赞技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell
Apple Machine Learning Research
Apple Machine Learning Research
C
Cyber Attacks, Cyber Crime and Cyber Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Tailwind CSS Blog
Cisco Talos Blog
Cisco Talos Blog
V
V2EX
博客园 - 【当耐特】
C
Cybersecurity and Infrastructure Security Agency CISA
Hugging Face - Blog
Hugging Face - Blog
The Cloudflare Blog
The Last Watchdog
The Last Watchdog
Simon Willison's Weblog
Simon Willison's Weblog
T
Threatpost
S
Secure Thoughts
O
OpenAI News
P
Proofpoint News Feed
S
SegmentFault 最新的问题
Forbes - Security
Forbes - Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Application and Cybersecurity Blog
Application and Cybersecurity Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Last Week in AI
Last Week in AI
宝玉的分享
宝玉的分享
Scott Helme
Scott Helme
T
Tenable Blog
A
Arctic Wolf
L
LINUX DO - 热门话题
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
www.infosecurity-magazine.com
www.infosecurity-magazine.com
V
Visual Studio Blog
Hacker News: Ask HN
Hacker News: Ask HN
Hacker News - Newest:
Hacker News - Newest: "LLM"
腾讯CDC
博客园 - Franky
WordPress大学
WordPress大学
Know Your Adversary
Know Your Adversary
博客园_首页
雷峰网
雷峰网
IT之家
IT之家
PCI Perspectives
PCI Perspectives
L
LINUX DO - 最新话题
H
Heimdal Security Blog

博客园 - 泽来

java多线程---线程之间的通信 设计原则 适配器模式 java复习进程 java小点滴 转>java5.0新特性 面试题收集 我买的书 java编程思想中关于多态性的描述 javascipt正则表达式 - 泽来 - 博客园 javascript小代码 - 泽来 - 博客园 DataGrid数据在Execel中打开 - 泽来 - 博客园 javascript高级教程 智力题收集 asp.net的TextBox回车触发事件 - 泽来 - 博客园 Builder生成器模式 是男人就坚持100下(强力益智,请留下战绩) XMLHTTP简介 - 泽来 - 博客园 多级无刷新联动(GB2312编码转换为UTF-8未取得满意效果) - 泽来 - 博客园
单件模式(singleton)
泽来 · 2005-12-16 · via 博客园 - 泽来

保证一个类只有一个实例的机制.

站在类的设计者的角度,强制使一个类只能有一个实例,而不是站在类的使用者角度。

要点:
1.singleton不要实现ICloneable,避免出现多个实例,与singleton冲突
2.singleton不要支持序列化,如上同理。
3.不要实现在多现程环境中.

单件模形初形

 public class Singleton
    
{
                
private static Singleton instance = null;
               
private Singleton() { }
        
public static Singleton Instance
        
{
            
get 
            
{
                
//惰性初 始化
                if (instance == null)
                
{
                                               instance 
= new Singleton();

                            }
                 }


                 
return instance;
             }

                
            
        }

    

单件模形与多线程

public class Singleton
    
{
        
//volatile 关键字的作用是  防止编辑器在编辑时对  它所修饰的对象
        
//进行微调,达到真正锁定多线程的目的
        private static volatile Singleton instance = null;
        
//一个辅助对象,用来作线程锁定
        private static object lockHelper = new object();

        
private Singleton() { }
        
public static Singleton Instance
        
{
            
get 
            
{
                
//双检查
                if (instance == null)
                
{
                    
//锁定,防止多线程访问产生对个对象
                    lock (lockHelper)
                    

                        
if(instance == null)
                        
{
                            instance 
= new Singleton();
                        }

                    }

                                     
return instance;
                }


            }

        }

    }

用static readyonly 表示,缺点是不支持参数传递

public class Singleton
{
    
public static readyonly Singleton sl = new Singleton();
    
private Singleton(){}
}

///////////////////////////////////////////////
             上面的代码与下面的等效
//////////////////////////////////////////////
public class Singleton
{
    
public static readyonly Singleton s1;
    
//静态的实例构造器
    static Singleton()
    
{
         s1 
= new Singleton();
    }

}

///////////////////////////////////////////////
.net机制自动为static对象进行了多线程处理.所以可以省略
///////////////////////////////////////////////