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

编程入门 行业动态 更新时间:2024-10-27 02:18:07
本文介绍了C#并发性,锁定和字典对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一堆被加载到数据库对象数据库实体。相同的DB实体可以被装载到比DB中的对象更多。一个定期DB实体需要特殊处理。必须由一个线程时间执行该处理。锁定是为了在这里。

编辑:重要提示:进程调用一个缓慢的Web服务。这就是我想要防止并发。我不知道如何可以安全地完成W / O型锁。

所以我创建将由数据库对象锁定引用一个挂锁对象。挂锁基于对象的,使得对于相同的实体的两个或更多数据库对象将使用相同的挂锁对象实体。我使用的是DB实体的ID作为关键Dictionary对象存储这些挂锁。挂锁对象是只是一个简单的字符串对象为好。这是正确的做法?我想我现在不是在工程或简化这一点。如果方法正确,请问这段代码看?它的工作原理,但我还没有测试它负载下

感谢:)

公共静态FUNC(CustomObject O) {如果(ReadyForUpdate(O)) {锁(LookupPadLockByID(object.ID) {如果(ReadyForUpdate(O)) { PerformUpdate(对象); } } } } 私人静态只读对象padlockLock =新的对象(); 私人静态只读字典< INT,串>挂锁=新词典< INT,串>(); 私有静态对象LookupPadLockByID(INT UNIQUEID) {锁(padlockLock) {如果(!padLocks.ContainsKey(UNIQUEID)) { padLocks.Add(UNIQUEID,UNIQUEID +被锁定); } } 返回挂锁[UNIQUEID]; }

解决方案

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

  • 使用一个词典< INT,对象> 挂锁类型,并投入了新对象( ); 作为值
  • 创建,仅仅持有身份证的一类。这将是可读性更好,如果你想看看你的LockPad类在调试
  • LockPad类:

    类LockPad {公众诠释标识{搞定;私人集; } 公共LockPad(INT ID){ this.Id = ID; } 公共重写字符串的ToString(){返回+ id.ToString锁(); } }

    然后,在该类锁。

    I have a bunch of DB entities that are loaded into DB objects. The same DB entity may be loaded into more than DB object. Periodically a DB entity will require special processing. This processing must be performed by one thread at a time. Locking is in order here.

    EDIT: Important note: the process calls a slow web service. This is what I'm trying to prevent concurrency. I don't see how this can be done safely w/o locks.

    So I create an "padlock" object that will be referenced by the DB objects for locking. The padlock object is entity based so that two or more DB objects for the same entity will use the same padlock object. I’m storing these padlocks in a dictionary object using the DB entity’s ID as the key. The padlock object is just a simple string object as well. Is this the right approach? I'm thinking I'm either over engineering or simplifying this. If the approach is correct, how does this code look? It works, but I've yet to test it under load.

    Thanks :)

    public static func(CustomObject o) { if (ReadyForUpdate(o)) { lock (LookupPadLockByID(object.ID) { if (ReadyForUpdate(o)) { PerformUpdate(object); } } } } private static readonly object padlockLock = new object(); private static readonly Dictionary<int, string> padLocks = new Dictionary<int,string>(); private static object LookupPadLockByID(int uniqueID) { lock (padlockLock) { if (!padLocks.ContainsKey(uniqueID)) { padLocks.Add(uniqueID, uniqueID + " is locked"); } } return padLocks[uniqueID]; }

    解决方案

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

  • Use a Dictionary<int,object> as padLocks' type, and put a new object(); as the value.
  • 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:

    class LockPad { public int Id { get; private set; } public LockPad(int id) { this.Id = id; } public override string ToString() { return "lock of " + id.ToString(); } }

    Then, lock on that class.

    更多推荐

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

    本文发布于:2023-11-10 01:50:55,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1574027.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:字典   对象   发性

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!