我有一个我正在跟踪的对象的动态列表,并且将来可能会使用许多线程。有人可以看一下这个实现的错误吗?
换句话说,我想跟踪这个对象的很多实例:
object AzureBatchTrackerLock = new object();
Dictionary<string, List<TableServiceEntity>> AzureBatchTracker = new Dictionary<string, List<TableServiceEntity>>();
我目前的方法是使用lock
专用对象,使用List和Dictionry来处理动态数组......
BatchThread.cs
/// This object itself contains a dictionary AND a lock.
/// This object will ALSO be contained in a second dictionary that also has its own lock
public class BatchThread
{
// When I perform updates to AzureBatchTracker dictionary, lock the following
internal readonly object AzureBatchTrackerLock = new object();
internal Dictionary<string, List<TableServiceEntity>> AzureBatchTracker = new Dictionary<string, List<TableServiceEntity>>();
}
BatchProcessor.cs
public class BatchProcessor
{
// Class-wide scope
readonly object _BatchEntriesLock = new object();
Dictionary<string, BatchThread> _BatchEntries = new Dictionary<string, BatchThread>();
// The "outer loop"
public BatchThread GetAnEntry(string ThingToProcess)
{
// Prepare return variable
BatchThread foundBatchThread = null;
// And then I would select an object like this:
// Notice that I'm performing what may be a concurrent entry here
// on a non-locked object ............................
// Is this a bad idea? Should I lock? Does it matter?
// If I must lock, want to exit as quickly as possible. Must not be later than the next lock() command
var activeRow = _BatchEntries[ThingToProcess];
// Since I have the object I care about, I'll work with the "secondary" lock object
// It works in practice, but I don't know what kind concurrency issues I'll run into.
lock (activeRow.Value.AzureBatchTrackerLock)
{
// Add and remove from the collection
ActiveRow.Value.AzureBatchTracker.Add(new exampleEntry);
// Do Stuff
ActiveRow.Value.AzureBatchTracker.Remove(something);
foundBatchThread = activeRow.Value.AzureBatchTracker[ThingToProcess];
}
return foundBatchThread;
}
// The inner loop
// Called whenever the "Outer" array needs to be expanded
public void AddNewBatchEntry(string newEntryName)
{
lock (_BatchEntriesLock)
{
// Add placeholder to the collection
_BatchEntries.Add( newEntryName, new BatchThread() );
// Do Stuff
}
}
}
您认为此实施中存在任何缺陷吗?还有更好的方法吗?
答案 0 :(得分:4)
如果您使用的是.NET 4.0,则可以使用ConcurrentDictionary。
答案 1 :(得分:1)
是的,你需要锁定
var ActiveRow = BatchEntries[ThingToProcess];
,因为你还要添加到字典中(除非你确定它们是在单独的时间发生的)。如果您可以保证在“外循环”执行时不会添加,那么不,您不需要锁定 - 您可以进行并发读取。
如果add方法很少发生,有很多读取,你应该查看ReaderWriterLock
答案 2 :(得分:1)
很难知道你的锁是否处于正确的水平而不知道如何使用这个代码,但是如果你的外部循环在一个线程中执行,它看起来还不错(除了因为你使用大写而难以阅读局部变量名,使一切看起来public
)。在优化方面,您应该避免使用lock
并实施ReaderWriterLockSlim
。