.NET4.0新特性:多线程
来源:互联网 发布:数据库字段拆分提取 编辑:程序博客网 时间:2024/05/29 17:30
在.net 40中对多线程的处理增加了很多新的类以方便多线程环境下的编程实现,首先需要了解的是两个非常有用的类Lazy和ThreadLazy,通过这两个类我们可以很方便实现一个单例模式而不用考虑太多的线程安全的问题。
Lazy:类简化了执行对象的延迟初始化和实例化的工作。通过以延迟方式实例化对象,可避免在根本不需要的情况下必须创建所有的对象,或者可以将对象的初始化延迟到第一次访问它们的时候.例如:
{
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可以非常优雅的实现一个简单的单例:
{
private Singleton()
{
}
private static Lazy<Singleton> __inc = new Lazy<Singleton>(() => new Singleton());
public static Singleton Current
{
get { return __inc.Value; }
}
}
通过Lazy我们可以很容易的延迟初始化一些对象。如果我们需要在多线程环境下建立一些线程级别应用可以使用ThreadLocal。在MSND中我们可以了解到除了Dispose之外,ThreadLocal 的所有公共和受保护的成员都是线程安全的,可从多个线程同时使用。Value 和 IsValueCreated 属性返回的值是特定的线程在其访问该属性。
在4.0之前为了实现线程级别存储会使用ThreadStatic标签来标识某个变量是线程级别存储的,例如:
{
static void Main(string[] args)
{
for (int i = 0; i < 5; i++)
{
var thread = new Thread( new ThreadStart( ()=>
{
using( var scope = new ThreadStaticScope() )
{
Console.WriteLine("Thread Id:{0}",ThreadStaticScope.Current.Value);
}
}) );
thread.Start();
}
Console.Read();
}
}
public class ThreadStaticScope:IDisposable
{
[ThreadStatic]
static ThreadStaticScope __inc;
public ThreadStaticScope()
{
Value = Thread.CurrentThread.ManagedThreadId;
__inc = this;
}
public int Value
{
get;
private set;
}
public static ThreadStaticScope Current
{
get { return __inc; }
}
public void Dispose()
{
__inc = null;
GC.SuppressFinalize(this);
}
}
输出结果:
Thread Id:12
Thread Id:13
Thread Id:14
Thread Id:15
从输出结果可以看到每个线程都是独享一个ThreadStaticScope.
换作ThreadLocal可以非常方便的实现同样的效果。
{
static void Main(string[] args)
{
for (int i = 0; i < 5; i++)
{
var thread = new Thread( new ThreadStart( ()=>
{
using (var scope = new ThreadLocalScope())
{
Console.WriteLine("Thread Id:{0}", ThreadLocalScope.Current.Value);
}
}) );
thread.Start();
}
Console.Read();
}
}
public class ThreadLocalScope:IDisposable
{
static ThreadLocal<ThreadLocalScope> __inc = new ThreadLocal<ThreadLocalScope>( );
public ThreadLocalScope()
{
Value = Thread.CurrentThread.ManagedThreadId;
__inc.Value = this;
}
public int Value
{
get;
private set;
}
public static ThreadLocalScope Current
{
get { return __inc.Value; }
}
public void Dispose()
{
__inc.Dispose();
GC.SuppressFinalize(this);
}
}
输出结果:
Thread Id:12
Thread Id:13
Thread Id:14
Thread Id:15
- .NET4.0新特性:多线程
- .net4.0新特性
- .net4.0新特性
- .NET4.0新特性
- .NET4.0对多线程新特性:有用线程安全类
- ASP.NET4.0新特性
- Net4.0---VS2010新特性
- NET4.0新特性动态语言
- .NET4.0新特性集合贴
- .NET4.0新特性集合贴
- .net4.0的一些新特性
- .net4.0的一些新特性
- .NET4.5新特性
- .NET4对多线程新特性:任任务工厂
- asp.net4.0的新特性入门概述
- 自定义的DynamicObject类_.NET4.0新特性
- Net4.0---Framwork新增特性
- java5 多线程新特性
- SWFUpload_v2.2.0.1在win2003.Net3.5下使用心得
- 破解无线网络连接密匙
- C# foreach循环实例详解
- enum
- C#忽略大小写的字符串替换(Replace)函数
- .NET4.0新特性:多线程
- SQL 数据库中的事务
- 如何实现远程连接SQL Server 2008
- 测试工作日志
- 望看到此文的人都能学好控件开发
- .NET4实践:默认加密与哈希算法的改变
- 英特尔Meego系统将允许运行iPhone软件
- _declspec(dllexport)与_declspec(dllimport)
- 关于sqlserver physical schema的一些SQL