简体   繁体   English

如果在尝试添加和删除时发生共谋,在ConcurrentDictionary中会发生什么?

[英]What will happen in ConcurrentDictionary if there is a collusion on try add and remove?

In my BL, I want that a certain code will not be executed while another thread is already in progress. 在我的BL中,我希望当另一个线程已经在执行时将不执行某些代码。

The issue is that the code can be executed (by demand) once for each customer but not twice for the same customer 问题在于,每个客户只能(按需)执行一次代码,而同一客户却不能执行两次

I have used the C# ConcurrentDictionary to keep track of the customers already in progress and when the execution is finished i have removed them from the ConcurrentDictionary , what will happen if when im trying to remove customer id from the ConcurrentDictionary while another thread is trying to add (the same or different key)? 我已经使用C# ConcurrentDictionary来跟踪已经在进行中的客户,执行完毕后我将它们从ConcurrentDictionary删除了,如果我试图在另一个线程尝试添加的ConcurrentDictionaryConcurrentDictionary删除客户ID,将会发生什么情况? (相同或不同的密钥)? Is the ConcurrentDictionary locks for write and the try remove will fail? ConcurrentDictionary的写锁定和try remove会失败吗?

private static readonly ConcurrentDictionary<int, DateTime> m_customerInProgress = new ConcurrentDictionary<int, DateTime>();

public void ExecuteLogic(int customerId)
{
        if (m_customerInProgress.TryAdd(customerId, DateTime.Now) == false)
        {
            this.Log().Info("customer in progress ");
            return;
        }

        try
        {
            DoSomething();
        }
        catch (Exception ex)
        {
            this.Log().Info("DoSomething failed ");
        }
        finally
        {
            DateTime startDateTime;

            if (m_customerInProgress.TryRemove(customerId, out startDateTime) == false)
            {
                this.Log().Fatal("this should never happens");
            }
            else
            {
                this.Log().Info("customer finished execution");
            }
        }
}

Can the this.Log().Fatal("this should never happens"); 可以this.Log().Fatal("this should never happens"); ever happen? 曾经发生过吗?

What will happen if the try add and try remove will execute in the same time for the same key? 如果尝试添加和尝试删除将对同一密钥同时执行,将会发生什么?

What will happen if it will execute for different keys? 如果针对不同的密钥执行,将会发生什么?

Your code is fine. 您的代码很好。 The TryAdd will only succeed if the TryRemove has completed. TryAdd如果才会成功TryRemove已完成。

The only odd thing you may see is that after the TryRemove returns the thread (lets call it T1 ) is suspended, and then another thread (lets call that T2 ) calls your method. 您可能会看到的唯一奇怪的事情是,在TryRemove返回该线程( TryRemove称为T1 )后,该线程被挂起,然后另一个线程(让其称为T2 )被调用了您的方法。 It may execute the entire method before T1 resumes execution, so your logging could become intertwined. 它可能会在T1恢复执行之前执行整个方法,因此您的日志记录可能会交织在一起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WCF聊天服务中的ConcurrentDictionary,可以添加但不能删除 - ConcurrentDictionary in WCF chat service, can add but not remove 在 ConcurrentDictionary AddOrUpdate 中为更新部分添加什么 - What to add for the update portion in ConcurrentDictionary AddOrUpdate 尝试删除ConcurrentDictionary中的某些项目时正确的方法是什么 - What is the correct approach when trying to remove some items in ConcurrentDictionary 如果两个引用试图在列表中完全相同的时间添加数据,将会发生什么 <T> 总汇 - What will happen if two references try to add data exactly same time in List<T> colletion ConcurrentDictionary Remove()无法访问 - ConcurrentDictionary Remove() is not accessible ConcurrentDictionary + Lazy - 实例化只会发生一次吗? - ConcurrentDictionary + Lazy — would instantiation happen only once? 为什么ConcurrentQueue和ConcurrentDictionary有“Try”方法 - TryAdd,TryDequeue - 而不是Add和Dequeue? - Why do ConcurrentQueue and ConcurrentDictionary have “Try” methods - TryAdd, TryDequeue - instead of Add and Dequeue? ConcurrentDictionary 添加数据 - ConcurrentDictionary Add Data 从 ConcurrentDictionary 中删除多个元素 - Remove multiple elements from ConcurrentDictionary 如果在线程池尝试执行代码之前取消 Task 会发生什么? - What happen if the Task is canceled before the thread pool try to execute the code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM