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

推荐订阅源

S
SegmentFault 最新的问题
Spread Privacy
Spread Privacy
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
Blog — PlanetScale
Blog — PlanetScale
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Apple Machine Learning Research
Apple Machine Learning Research
SecWiki News
SecWiki News
腾讯CDC
P
Privacy International News Feed
Webroot Blog
Webroot Blog
J
Java Code Geeks
爱范儿
爱范儿
A
About on SuperTechFans
S
Secure Thoughts
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
D
DataBreaches.Net
Cloudbric
Cloudbric
Security Archives - TechRepublic
Security Archives - TechRepublic
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
C
Cyber Attacks, Cyber Crime and Cyber Security
P
Proofpoint News Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Security Latest
Security Latest
Forbes - Security
Forbes - Security
小众软件
小众软件
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
Cybersecurity and Infrastructure Security Agency CISA
T
Threatpost
量子位
MongoDB | Blog
MongoDB | Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
月光博客
月光博客
W
WeLiveSecurity
P
Privacy & Cybersecurity Law Blog
Vercel News
Vercel News
Google Online Security Blog
Google Online Security Blog
云风的 BLOG
云风的 BLOG
GbyAI
GbyAI
S
Security @ Cisco Blogs
T
The Exploit Database - CXSecurity.com
Help Net Security
Help Net Security
V
Visual Studio Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
Application and Cybersecurity Blog
Application and Cybersecurity Blog
博客园 - 聂微东
P
Proofpoint News Feed
C
CERT Recently Published Vulnerability Notes
Attack and Defense Labs
Attack and Defense Labs

博客园 - Kain

Microsoft.Practices.Unity 的一个线程安全Bug浅析 NET Reflector 7发布,其 不再免费 (抽象)工厂的另一种实现方式 自定义EF4 Model 代码生成 .net 4.0 中对多线程新特性(四)--任务和任务工厂 .net 4.0 中对多线程新特性(三) .net 4.0 中对多线程新特性(二) Flex&.Net开篇 SqlSever N层表数据查询效率 [读书笔记]Start-up fatigue(启动杂役) 马上又要过中秋和国庆了! 一个小问题 C#2.0 新的关键字 yield 我们的游戏! 不知道有没有同在学习aspnetforums的 郁闷! 关于面试 关于DataGride的Key事件 开心就好!
.net 4.0 中对多线程新特性(一)
Kain · 2010-07-30 · via 博客园 - Kain

 在.net 40中对多线程的处理增加了很多新的类以方便多线程环境下的编程实现,首先需要了解的是两个非常有用的类Lazy<T>和ThreadLazy<T>,通过这两个类我们可以很方便实现一个单例模式而不用考虑太多的线程安全的问题。

      Lazy<T>:类简化了执行对象的延迟初始化和实例化的工作。通过以延迟方式实例化对象,可避免在根本不需要的情况下必须创建所有的对象,或者可以将对象的初始化延迟到第一次访问它们的时候.例如:

代码

    class Program
    {
        
public static Lazy<int> __current = new Lazy<int>(() => Thread.CurrentThread.ManagedThreadId);static void Main(string[] args)
        {
            
for (int i = 0; i < 5; i++)
            {
                var thread 
= new Thread( new ThreadStart( ()=> 
                    {
                        Console.WriteLine(
string.Format("Current Thread Id:{0} Current Value:{1}"
                                ,Thread.CurrentThread.ManagedThreadId,__current.Value ) );
                    }) );
                thread.Start();
            }
            
            Console.Read();
        }
         
    }

 程序输出如下:

可以看到__current.Value在不同的线程下面的值始终为第一个线程的ID,需要注意的是__current.Value属性一旦被调用就会回调构造函数中传入的Fun,如果调用失败__current也不会再次调用Fun,其__current.IsValueCreated始终为真,__current.Value是不能够被修改的。通过Lazy<T>可以非常优雅的实现一个简单的单例:

代码

public class Singleton
    {
        
private Singleton()
        {
        }
private static Lazy<Singleton> __inc = new Lazy<Singleton>(() => new Singleton());public static Singleton Current
        {
            
get { return __inc.Value; }
        }
    }