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

推荐订阅源

腾讯CDC
Schneier on Security
Schneier on Security
B
Blog RSS Feed
aimingoo的专栏
aimingoo的专栏
P
Proofpoint News Feed
A
About on SuperTechFans
Recorded Future
Recorded Future
Recent Announcements
Recent Announcements
Microsoft Security Blog
Microsoft Security Blog
L
LangChain Blog
Hugging Face - Blog
Hugging Face - Blog
The GitHub Blog
The GitHub Blog
Google DeepMind News
Google DeepMind News
T
Tailwind CSS Blog
Vercel News
Vercel News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
MyScale Blog
MyScale Blog
V2EX - 技术
V2EX - 技术
N
Netflix TechBlog - Medium
F
Fortinet All Blogs
V
Visual Studio Blog
Martin Fowler
Martin Fowler
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - Franky
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
The Exploit Database - CXSecurity.com
F
Full Disclosure
Scott Helme
Scott Helme
H
Heimdal Security Blog
博客园 - 叶小钗
Google DeepMind News
Google DeepMind News
Cyberwarzone
Cyberwarzone
Application and Cybersecurity Blog
Application and Cybersecurity Blog
V
Vulnerabilities – Threatpost
Blog — PlanetScale
Blog — PlanetScale
Security Latest
Security Latest
WordPress大学
WordPress大学
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Troy Hunt's Blog
S
SegmentFault 最新的问题
Forbes - Security
Forbes - Security
Jina AI
Jina AI
S
Securelist
小众软件
小众软件
Simon Willison's Weblog
Simon Willison's Weblog
J
Java Code Geeks
AWS News Blog
AWS News Blog
N
News and Events Feed by Topic
博客园 - 三生石上(FineUI控件)
量子位

博客园 - 泽来

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对象进行了多线程处理.所以可以省略
///////////////////////////////////////////////