How to acquire a lock by a key

前端 未结 6 1358
一个人的身影
一个人的身影 2020-11-28 09:47

What is the best way to prevent concurrent update of one record in a key-value set without locking the entire set? Semantically, I\'m looking for some kind of locking by a k

6条回答
  •  南方客
    南方客 (楼主)
    2020-11-28 10:34

    If the "record" you mention is a mutable object and "update" means that the object's internal state is modified without disturbing the structure of the container, then you can accomplish what you want just by locking the record object.

    If however "update" means removing the record object from the container and replacing it, then you must lock then entire container to prevent other threads from seeing it in an inconsistent state.

    In either case, you should be looking at the classes in the java.util.concurrent package.

提交回复
热议问题