List concurrent removing and adding

前端 未结 5 1139
渐次进展
渐次进展 2020-12-17 16:36

I am not too sure, so i thought i\'d ask. Would removing and adding items to a System.Collections.Generic.List<> object be non-thread safe?

My sit

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-17 17:16

    Definitely using lock for particular code makes it thread safe, but I do not agree with it for current scenario.

    You can implement method Synchronized to make collection thread safe. This link explains why and how to do that.

    Another purely programmatic approach is mentioned in this link, though I never tested it firsthand but it should work.

    btw, one of the bigger concern is that, are you trying to maintain something like connection pool on you own? if yes then why?

    I take my answer back. Using locks in better answer that using this method.

提交回复
热议问题