IConfigurationSectionHandler的线程安全性问题
2009-11-27 14:44
405 查看
class TestMutilThreadSection : IConfigurationSectionHandler
{
private IList<string> _list;
public IList<string> List
{
get { return _list; }
}
#region IConfigurationSectionHandler Members
public object Create(object parent, object configContext, System.Xml.XmlNode section)
{
_list = new List<string>();
_list.Add("1");
Thread.Sleep(2 * 1000);
_list.Add("2");
Thread.Sleep(2 * 1000);
return this;
}
#endregion
}
class Program
{
static void Main(string[] args)
{
Thread t1 = new Thread(new ThreadStart(AccessSection));
t1.Start();
Thread.Sleep(3000);
Thread t2 = new Thread(new ThreadStart(AccessSection));
t2.Start();
}
static void AccessSection()
{
TestMutilThreadSection section = ConfigurationManager.GetSection("TestMutilThreadSection") as TestMutilThreadSection;
IList<string> list = section.List;
foreach (string str in list)
Console.WriteLine(string.Format("ThreadId:{0},{1}", Thread.CurrentThread.ManagedThreadId, str));
}
}
运行结果:
ThreadId:3,1
ThreadId:4,1
ThreadId:4,2
请按任意键继续. . .
结论:
TestMutilThreadSection 类在一开始多线程访问的时候,会出现_list字段的线程安全问题。
{
private IList<string> _list;
public IList<string> List
{
get { return _list; }
}
#region IConfigurationSectionHandler Members
public object Create(object parent, object configContext, System.Xml.XmlNode section)
{
_list = new List<string>();
_list.Add("1");
Thread.Sleep(2 * 1000);
_list.Add("2");
Thread.Sleep(2 * 1000);
return this;
}
#endregion
}
class Program
{
static void Main(string[] args)
{
Thread t1 = new Thread(new ThreadStart(AccessSection));
t1.Start();
Thread.Sleep(3000);
Thread t2 = new Thread(new ThreadStart(AccessSection));
t2.Start();
}
static void AccessSection()
{
TestMutilThreadSection section = ConfigurationManager.GetSection("TestMutilThreadSection") as TestMutilThreadSection;
IList<string> list = section.List;
foreach (string str in list)
Console.WriteLine(string.Format("ThreadId:{0},{1}", Thread.CurrentThread.ManagedThreadId, str));
}
}
运行结果:
ThreadId:3,1
ThreadId:4,1
ThreadId:4,2
请按任意键继续. . .
结论:
TestMutilThreadSection 类在一开始多线程访问的时候,会出现_list字段的线程安全问题。
相关文章推荐
- IConfigurationSectionHandler的问题
- Struts 1 和 strtust 2 的线程安全性问题
- Spring 并发访问的线程安全性问题
- [C#]DLL继承IConfigurationSectionHandler即可访问宿主的.exe.config
- [C#]DLL继承IConfigurationSectionHandler即可访问宿主的.exe.config
- 深入研究Servlet线程安全性问题
- 深度分析 Java 的枚举类型:枚举的线程安全性及序列化问题
- Android性能优化5 多线程并发的性能问题所幸的是,Android系统为我们提供了Looper、Handler、MessageQueue来帮助实现上面的线程任务模型: Looper: 能够确保线
- 深入研究Servlet线程安全性问题
- 深入研究Servlet线程安全性问题
- [原创]自定义Config例子,使用IConfigurationSectionHandler 接口
- 基于Android中Looper , Handler , Message的线程池,轻松解决Sqlite数据库的线程安全问题
- servlet线程安全性问题理解
- 深入研究Servlet线程安全性问题
- IConfigurationSectionHandler 接口的实现
- Servlet笔记——(2.3)Servlet线程安全性问题
- 深入研究Servlet线程安全性问题
- Android中Handler,线程相关问题
- Spring 并发访问的线程安全性问题
- servlet的线程安全性问题