Consider this code:
void DoSomething(int key)
{
concurrentDictionary.GetOrAdd(key, (k)=>
{
//Do some expensive over network and database to retrieve value.
});
Consider 2 threads are invoking DoSomething(2)
. At the same time they will see that there's no item with Key==2
in the dictionary. Consider Thread1
starts doing the expensive algorithm to retrieve the value of 2.
Question 1: Will Thread2
waits for Thread1
to accomplish its job? Or simply tries to retrieve the value itself, and throwing it away at the time of adding it to the dictionary? (As the Thread1
added this already )
Question 2: If Thread2
doesn't wait, what's the best solution to avoid multiple running of that expensive algorithm?
It tells you in the documentation:
If you call
GetOrAdd
simultaneously on different threads, addValueFactory may be called multiple times, but its key/value pair might not be added to the dictionary for every call.
For question 2, I'd consider changing from a ConcurrentDictionary<int,Something>
to a ConcurrentDictionary<int,Lazy<Something>>
where the addValueFactory
method simply constructs a Lazy<Something>
that specifies the ExecutionAndPublication
mode. The expensive operation would then by the valueFactory
for the Lazy<Something>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With