我必须采取哪些选项才能使此代码线程安全?
我有这段代码,为简洁起见,跳过了很多内容,但场景是这样的:
public class Billing
{
private List<PrecalculateValue> Values = new List<PrecalculateValue>();
public int GetValue(DateTime date)
{
var preCalculated = Values.SingleOrDefault(g => g.date == date).value;
//if exist in Values, return it
if(preCalculated != null)
{
return preCalculated;
}
// if it does not exist calculate it and store it in Values
int value = GetValueFor(date);
Values.Add(new PrecalculateValue{date = date, value = value});
return value;
}
private object GetValueFor(DateTime date)
{
//some logic here
}
}
我有一个 List
其中我存储了我已经计算出的所有值以供以后使用,我这样做主要是因为我不想为同一客户端重新计算两次,每次计算都涉及大量操作并需要 500 到 1000 之间ms,并且由于孔计费类中涉及一些递归,因此很有可能重用该值。
所有这些工作都完美,直到我做了一个测试,我为两个不同的客户端同时进行了两个计算,并且 Values.Single(g => g.date == date).value
行返回了一个异常,因为它在集合中发现了多个结果。 所以我检查了列表,它将两个客户端的值存储在同一个列表中。我该怎么做才能避免这个小问题呢?
I have this segment of code , a lot of things skipped for brevity but the scene is this one:
public class Billing
{
private List<PrecalculateValue> Values = new List<PrecalculateValue>();
public int GetValue(DateTime date)
{
var preCalculated = Values.SingleOrDefault(g => g.date == date).value;
//if exist in Values, return it
if(preCalculated != null)
{
return preCalculated;
}
// if it does not exist calculate it and store it in Values
int value = GetValueFor(date);
Values.Add(new PrecalculateValue{date = date, value = value});
return value;
}
private object GetValueFor(DateTime date)
{
//some logic here
}
}
I have a List<PrecalculateValue> Values
where i store all the values i already calculated for later use, i do these mainly because i don't want to recalculate things twice for the same client, each calculation involve a lot of operations and take between 500 and 1000 ms, and there is a big chance of reuse that value, because of some recursion involved in the hole billing class.
All of these work perfectly until i made a test where i hit two simultaneous calculations for two different clients, and the line Values.Single(g => g.date == date).value
returned an exception because it found more than one result in the collection.
So i checked the list and it stored values of both clients in the same list. What can i do to avoid this little problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,首先,这一行:
使得后续的行永远不会被调用。我猜你已经在这里解释了一下你的代码?
如果您想要同步对
Values
列表的写入,最直接的方法是在您要修改列表的代码中的所有位置锁定
公共对象:但是还有其他值得注意的事情:由于您似乎希望每个
DateTime
有一个PrecalculateValue
,因此更合适的数据结构可能是Dictionary;
- 它将根据您的DateTime
键提供闪电般的 O(1) 查找,而List
则需要迭代以找到您要查找的内容。完成此更改后,您的代码可能如下所示:
最后一条建议:除非集合中不存在超过一个特定值这一点至关重要,否则
Single
方法就太过分了。如果您只想获取第一个值并忽略潜在的重复项,则First
既更安全(例如,发生异常的可能性更小)又更快(因为它不必遍历整个值)收藏)。Well, first of all, this line:
makes it so that the subsequent lines will never be called. I'm guessing you've paraphrased your code a little bit here?
If you want to synchronize writes to your
Values
list, the most straightforward way would be tolock
on a common object everywhere in the code that you're modifying the list:But here's something else worth noting: since it looks like you want to have one
PrecalculateValue
perDateTime
, a more appropriate data structure would probably be aDictionary<DateTime, PrecalculateValue>
-- it will provide lightning-fast, O(1) lookup based on yourDateTime
key, as compared to aList<PrecalculateValue>
which would have to iterate to find what you're looking for.With this change in place, your code might look something like this:
And one last piece of advice: unless it's critical that no more than one of a particular value exist in your collection, the
Single
method is overkill. If you'd rather just get the first value and disregard potential duplicates,First
is both safer (as in, less chance of an exception) and faster (because it doesn't have to iterate over the entire collection).可以使用类似的东西
,但建议使用字典来获取键值对列表。
Could use something like this
Would advise using a dictionary though for a list of key value pairs.