Locking on an interned string?

前端 未结 6 1278
栀梦
栀梦 2021-01-01 16:43

Update: It is acceptable if this method is not thread safe, but I\'m interested in learning how I would make it thread safe. Also, I do not want to lock on

6条回答
  •  醉梦人生
    2021-01-01 17:15

    Never lock on strings. In particular on those that are interned. See this blog entry on the danger of locking on interned strings.

    Just create a new object and lock on that:

    object myLock = new object();
    

提交回复
热议问题