Properly locking a List in MultiThreaded Scenarios?

前端 未结 5 1714
难免孤独
难免孤独 2020-11-30 08:52

Okay, I just can\'t get my head around multi-threading scenarios properly. Sorry for asking a similar question again, I\'m just seeing many different \"facts\" around the in

5条回答
  •  悲哀的现实
    2020-11-30 09:02

    You may be misinterpreting the this answer, what is actually being stated is that they lock statement is not actually locking the object in question from being modified, rather it is preventing any other code using that object as a locking source from executing.

    What this really means is that when you use the same instance as the locking object the code inside the lock block should not get executed.

    In essence you are not really attempting to "lock" your list, you are attempting to have a common instance that can be used as a reference point for when you want to modify your list, when this is in use or "locked" you want to prevent other code from executing that would potentially modify the list.

提交回复
热议问题