在主线程中定义了 public static IList<Model.Keyword_Model> StaticMList = new List<Model.Keyword_Model>();
private static Object thisLock = new Object();
在子线程中:
for (int i = 0; i < Keywordtype.Length; i++)
{
Model.Keyword_Model MModel = new jingjiaprj.Model.Keyword_Model();
//给MModel赋值 AddMlist(MModel);
} public static void AddMlist(Model.Keyword_Model mmodel)
{
lock (thisLock)
{
StaticMList.Add(mmodel);
}
}这样锁有问题。我得出来的结果StaticMList里的结果都是重复的。这是为什么?
希望哪位高手能帮忙解答下
private static Object thisLock = new Object();
在子线程中:
for (int i = 0; i < Keywordtype.Length; i++)
{
Model.Keyword_Model MModel = new jingjiaprj.Model.Keyword_Model();
//给MModel赋值 AddMlist(MModel);
} public static void AddMlist(Model.Keyword_Model mmodel)
{
lock (thisLock)
{
StaticMList.Add(mmodel);
}
}这样锁有问题。我得出来的结果StaticMList里的结果都是重复的。这是为什么?
希望哪位高手能帮忙解答下
解决方案 »
- 如何在固定时间自动执行代码(比如每周日24:00)
- winfrom ListView单元格中的字符 选中后要Ctrl+C()复制出来 怎么设置
- web整站下载相关代码
- 急!!谁有做过不同数据库类型间的数据库同步啊?
- webbrower
- 求修改SQL语句
- static构造函数,也非static构造函数有什么区别????????????????????-----------
- 急救!!!DataTable如何在当前行更新数据?
- 请问有C#方面的好书介绍吗
- 请教:SqlCommand对象传参数难题
- 如何使PictureBox中背景透明的图片显示出来后背景真正透明
- SQL Express 2005附加不了数据库文件
就你的代码来看,锁没有必要存在。
这个是在哪里,是在你的类中,还是一个全局的,如果在你的Model.Keyword_Model中,那么每次new,lock的都是自己内部的,不是公共的
public sealed class SingleTon
{
private static SingleTon clone = null;
/// <summary>
/// /// <summary>
/// 辅助锁对象,本身没有意义
/// </summary>
/// </summary>
private static readonly object obj = new object();
public static SingleTon Clone
{
get
{
if (clone == null)
{
lock (obj)
{
clone = Nested.Clone;
return clone;
}
}
return clone;
}
}
class Nested///嵌套类延时实例化。
{
static Nested()
{
}
internal static readonly SingleTon Clone = new SingleTon();
}
}
参考如下:
public sealed class Singleton
{
static Singleton instance=null;
static readonly object padlock = new object();
Singleton()
{
}
public static Singleton Instance
{
get
{
if (instance==null)
{
lock (padlock)
{
if (instance==null)
{
instance = new Singleton();
}
}
}
return instance;
}
}
}