I have this segment of code, a lot of things have been left for brevity but this is the view:
public class billing {personal list & lt; PrecalculateValue & gt; Value = new list & lt; Presetulit Value & gt; (); Public Intuit Gateway (date time) {var preCalculated = Values.SingleOrDefault (g = & gt; g.date == date). value; // If the values are present then return it (preCalculated! = Null) {PreCalculated back; } // If it does not exist, then store it in the calculation and values = GetValueFor (date); value. Add (New Spratch Value {date = date, value = value}); return value; } Private object GetValueFor (DateTime date) {// some logic here}}
I have a list & lt; PrecalculateValue & gt; Values
Where I already store all the values calculated for later use, I do this for these main reasons because I do not want to re-run twice for the same customer, Each calculation involves a lot of operations and 500 and 1000 ms, and because of some recursions included in the holes billing class, there is a great chance to reuse that value.
All these work completely until I did a test, where I had two simultaneous calculating clients for two different calculations, and line value. Single (g = & gt; g.date == date). Value
returned an exception because it found more than one result in the collection. So I checked the list and stored values of both clients in the same list can I do to avoid this small problem?
OK, firstly, this line:
return value . Single (G = & gt; GDD == date). value;
It does that the subsequent lines will never call. Do you anticipate that you have made your code a little smaller?
If you want to synchronize while typing in your values
list, then the easiest way would be to lock it on a normal object everywhere in that code
Which you are modifying the list:
int value = GetValueFor (date); Lock (dedicated lockObject) {value. Add (new subkey value {date = date, value = value}); } return value;
But here's something to be noticeable: because it looks like you want a PrecalculateValue
per date time
, a more appropriate data structure Maybe the dictionary is
have to be repeated to find what you are looking for. DateTime
key, will provide lightning-fast, o (1) lookups against <(code)> list & lt; PrecalculateValue & gt;
With this change, your code might look like this:
Public class billing {personal dictionary & lt; Date time, anterior value & gt; Value = New Dictionary & lt; Date time, anterior value & gt; (); Private Redoni normal lock object = new object (); Public Int Gate Value (Datetime Date) {Precqualet Value Cache Clauses; // Note: For the right thread protection, you need to lock / write as well as read, to ensure that with a script / reading / corrupted status / lock (normal lock object) {if (Value, telegraph value (date, cached calculation)) back cached communication. value; } Int Value = GetValueFor (date); // Here we have to check whether the key is present again, just another thread added an item because we checked for the last time. // Also make sure to lock any place you are adding to reading or reading the collection. Lock (normal lock object) {if (! Value (insert value (date)) value [date] = new presetlicate value {date = date, value = value}; } return value; } Private Object GetValueFor (DateTime date) {// some logic here}}
And one last advice: unless it is not important that your collection does not exceed any particular value , The single
method is overkill if you not only get the first value and ignore the potential duplicate, both of the first
are safe (as a low chance of an exception) and It's fast (because it is not recurring on the whole collection).
Comments
Post a Comment