我必须采取哪些选项才能使此代码线程安全?

发布于 2024-09-06 13:42:18 字数 1167 浏览 2 评论 0原文

我有这段代码,为简洁起见,跳过了很多内容,但场景是这样的:

 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; Values 其中我存储了我已经计算出的所有值以供以后使用,我这样做主要是因为我不想为同一客户端重新计算两次,每次计算都涉及大量操作并需要 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抹茶夏天i‖ 2024-09-13 13:42:18

好吧,首先,这一行:

return Values.Single(g => g.date == date).value;

使得后续的行永远不会被调用。我猜你已经在这里解释了一下你的代码?

如果您想要同步对 Values 列表的写入,最直接的方法是在您要修改列表的代码中的所有位置锁定公共对象:

int value = GetValueFor(date);

lock (dedicatedLockObject) {
    Values.Add(new PrecalculateValue{date = date, value = value});
}

return value;

但是还有其他值得注意的事情:由于您似乎希望每个 DateTime 有一个 PrecalculateValue,因此更合适的数据结构可能是 Dictionary; - 它将根据您的 DateTime 键提供闪电般的 O(1) 查找,而 List

则需要迭代以找到您要查找的内容。

完成此更改后,您的代码可能如下所示:

public class Billing
{
    private Dictionary<DateTime, PrecalculateValue> Values = 
        new Dictionary<DateTime, PrecalculateValue>();

    private readonly commonLockObject = new object();

    public int GetValue(DateTime date)
    {
        PrecalculateValue cachedCalculation;

        // Note: for true thread safety, you need to lock reads as well as
        // writes, to ensure that a write happening concurrently with a read
        // does not corrupt state.
        lock (commonLockObject) {
            if (Values.TryGetValue(date, out cachedCalculation))
                return cachedCalculation.value;
        }

        int value = GetValueFor(date);

        // Here we need to check if the key exists again, just in case another
        // thread added an item since we last checked.
        // Also be sure to lock ANYWHERE ELSE you're manipulating
        // or reading from the collection.
        lock (commonLockObject) {
            if (!Values.ContainsKey(date))
                Values[date] = new PrecalculateValue{date = date, value = value};
        }

        return value;
    }

    private object GetValueFor(DateTime date)
    {
        //some logic here
    }
}

最后一条建议:除非集合中不存在超过一个特定值这一点至关重要,否则 Single 方法就太过分了。如果您只想获取第一个值并忽略潜在的重复项,则 First 既更安全(例如,发生异常的可能性更小)又更快(因为它不必遍历整个值)收藏)。

Well, first of all, this line:

return Values.Single(g => g.date == date).value;

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 to lock on a common object everywhere in the code that you're modifying the list:

int value = GetValueFor(date);

lock (dedicatedLockObject) {
    Values.Add(new PrecalculateValue{date = date, value = value});
}

return value;

But here's something else worth noting: since it looks like you want to have one PrecalculateValue per DateTime, a more appropriate data structure would probably be a Dictionary<DateTime, PrecalculateValue> -- it will provide lightning-fast, O(1) lookup based on your DateTime key, as compared to a List<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:

public class Billing
{
    private Dictionary<DateTime, PrecalculateValue> Values = 
        new Dictionary<DateTime, PrecalculateValue>();

    private readonly commonLockObject = new object();

    public int GetValue(DateTime date)
    {
        PrecalculateValue cachedCalculation;

        // Note: for true thread safety, you need to lock reads as well as
        // writes, to ensure that a write happening concurrently with a read
        // does not corrupt state.
        lock (commonLockObject) {
            if (Values.TryGetValue(date, out cachedCalculation))
                return cachedCalculation.value;
        }

        int value = GetValueFor(date);

        // Here we need to check if the key exists again, just in case another
        // thread added an item since we last checked.
        // Also be sure to lock ANYWHERE ELSE you're manipulating
        // or reading from the collection.
        lock (commonLockObject) {
            if (!Values.ContainsKey(date))
                Values[date] = new PrecalculateValue{date = date, value = value};
        }

        return value;
    }

    private object GetValueFor(DateTime date)
    {
        //some logic here
    }
}

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).

养猫人 2024-09-13 13:42:18

可以使用类似的东西

public int GetValue(DateTime date)
{

    var result = Values.Single(g => g.date == date) ?? GetValueFor(date);

    lock (Values)
    {
        if (!Values.Contains(result)) Values.Add(result);
    }
    return result.value;
}

private PrecalculateValue GetValueFor(DateTime date)
{
    //logic
    return new PrecalculateValue() ;
}

,但建议使用字典来获取键值对列表。

Could use something like this

public int GetValue(DateTime date)
{

    var result = Values.Single(g => g.date == date) ?? GetValueFor(date);

    lock (Values)
    {
        if (!Values.Contains(result)) Values.Add(result);
    }
    return result.value;
}

private PrecalculateValue GetValueFor(DateTime date)
{
    //logic
    return new PrecalculateValue() ;
}

Would advise using a dictionary though for a list of key value pairs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文