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

推荐订阅源

N
Netflix TechBlog - Medium
Microsoft Azure Blog
Microsoft Azure Blog
罗磊的独立博客
博客园 - 三生石上(FineUI控件)
aimingoo的专栏
aimingoo的专栏
B
Blog RSS Feed
V
Visual Studio Blog
P
Proofpoint News Feed
云风的 BLOG
云风的 BLOG
博客园 - 【当耐特】
大猫的无限游戏
大猫的无限游戏
Application and Cybersecurity Blog
Application and Cybersecurity Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
The Cloudflare Blog
B
Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Apple Machine Learning Research
Apple Machine Learning Research
M
MIT News - Artificial intelligence
Know Your Adversary
Know Your Adversary
I
InfoQ
T
The Exploit Database - CXSecurity.com
V
Vulnerabilities – Threatpost
C
Cisco Blogs
Spread Privacy
Spread Privacy
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Palo Alto Networks Blog
Simon Willison's Weblog
Simon Willison's Weblog
月光博客
月光博客
博客园 - Franky
Project Zero
Project Zero
G
Google Developers Blog
S
SegmentFault 最新的问题
博客园 - 聂微东
P
Privacy & Cybersecurity Law Blog
The GitHub Blog
The GitHub Blog
阮一峰的网络日志
阮一峰的网络日志
P
Privacy International News Feed
T
Threat Research - Cisco Blogs
S
Schneier on Security
Microsoft Security Blog
Microsoft Security Blog
G
GRAHAM CLULEY
S
Security @ Cisco Blogs
Martin Fowler
Martin Fowler
A
Arctic Wolf
T
Tenable Blog
L
LINUX DO - 最新话题
TaoSecurity Blog
TaoSecurity Blog
Hugging Face - Blog
Hugging Face - Blog
有赞技术团队
有赞技术团队
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com

博客园 - 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; }
        }
    }