您能看出这个Double Check里的问题吗?
昨天在做code review時看到一位同事寫了這樣的代碼。這段代碼的目的使用Double Check的做法來保證線程安全的延遲加載。但是我看到這代碼之后發現了一個問題,這個問題不是第一次出現。因此,我打算在博客上記錄一筆,希望可以給更多人提個醒吧。
假設,我們有這樣一個Category類型,記錄的是一個樹型的分類結構:
public class Category {public int CategoryID { get; set; }public List<Category> Children { get; set; } }然后,我們需要一個CategoryLoader,提供一個Get方法從ID獲得指定的Category對象:
public class CategoryLoader {private object m_mutex = new object();private Dictionary<int, Category> m_categories;public Category GetCategory(int id){if (this.m_categories == null){lock (this.m_mutex){if (this.m_categories == null){LoadCategories();}}}return this.m_categories[id];}private void LoadCategories(){this.m_categories = new Dictionary<int, Category>();this.Fill(GetCategoryRoots());}private void Fill(IEnumerable<Category> categories){foreach (var cat in categories){this.m_categories.Add(cat.CategoryID, cat);Fill(cat.Children);}}private IEnumerable<Category> GetCategoryRoots() { ... } }代碼的邏輯非常簡單:使用一個字典作為容器,在GetCategory方法內部使用Double Check的方式來保證線程安全(即多個線程同時訪問同一個對象的GetCategory方法不會出現問題)。如果沒有加載,在使用LoadCategories方法構造并填充字典。在LoadCategories方法中會獲取所有的“根分類”,并調用Fill方法填充字典。Fill方法會將傳入的categories集合添加到字典中,并且遞歸地將它們的子分類也填充至字典中。
只可惜,上面的代碼有一些問題,導致Double Check沒有能夠實現我們預期的效果。您能看出這個問題來嗎?
當然,為了演示代碼的簡單,我省略了很多細節。例如Category的ID缺失或有重復,Category對象不是immutable,Children屬性可能會包含null,這可能都會形成問題。不過,我們就暫時不在這方面考究了吧。
晚上我會公布結果(此處)。
from:?http://blog.zhaojie.me/2009/09/double-check-failure.html
總結
以上是生活随笔為你收集整理的您能看出这个Double Check里的问题吗?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 趣味编程:从字符串中提取信息(参考答案
- 下一篇: 您能看出这个Double Check里的