且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#并发,锁定和字典对象

更新时间:2023-02-09 16:21:45

锁定字符串不是一个好主意,我建议两个选择:

Locking on a string is not a good idea. I suggest two alternatives:


  1. 使用字典< int,object> 作为padLocks类型,并将一个 ); 作为值。

  2. 创建一个简单地保存id的类;

  1. Use a Dictionary<int,object> as padLocks' type, and put a new object(); as the value.
  2. Create a class that simply holds the id; this would be better for readability if you ever want to look at your LockPad class while debugging.

LockPad类:

class LockPad {
    public int Id { get; private set; }

    public LockPad(int id) {
        this.Id = id;
    }

    public override string ToString() {
        return "lock of " + id.ToString();
    }
}

然后锁定该类。