Say we have:
var dictionary = new ConcurrentDictionary<string, Lazy<Heavy>>();
Activation is Heavyvery resource intensive. Consider this code:
Heavy
return dictionary.GetOrAdd("key", key => { return new Lazy<Heavy>(() => { return Instantiate(); }); }).Value;
The method Instantiate(), of course, returns an instance of the type Heavy.
Instantiate()
Instantiate()Is it 100% guaranteed for this key that the method will be called no more than once ?
Some people claim that having multiple threads, we can only create a few instances Lazy<Heavy>, which is very cheap. The actual method Instantiate()will be called no more than once.
Lazy<Heavy>
, . ?
Instantiate . GetOrAdd :
Instantiate
GetOrAdd
GetOrAdd , addValueFactory , / .
: addValueFactory - GetOrAdd. , GetOrAdd - - 2 Lazy<Heavy>, GetOrAdd, ( factory - , , factory, , GetOrAdd). .Value GetOrAdd - Lazy<Heavy>, Instantiate .
addValueFactory
.Value
Source: https://habr.com/ru/post/1689825/More articles:How and why did the ISO C ++ Standards Committee (RG21) decide to accept the proposal for the spacecraft operator as is? - c ++Is data allocated on the heap slower for access than data allocated on the stack? - c ++Формат времени и временный пояс BitTrex - resthttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1689823/how-do-i-add-google-fonts-to-a-gatsby-site&usg=ALkJrhi1jCIavNlVZMzgai8-THZX8X2pHARemove the accents in the line except for "-" - c #How to perform tasks in Celery using datetime from MySQL? - pythonNormalize string except - - javaWhat is the difference between thenApply and thenApplyAsync Java CompletableFuture? - javaRemoving variables from a numpy array - pythonSplit a string in space, with the exception of internal quotation marks - Go - stringAll Articles