Lock Position (eq:map<key,map<>> double collection How to lock only the object position used, instead of locking the entire set)

Source: Internet
Author: User

For example, the map on the top of the map definition variable is data, such as a team copy NPC 1 under a large number of rooms namely map<1,<roomid,room>>, in multi-threaded, a thread in the NPC 1 of the bottom of a room, room ID 1 is map <1,<1,room>>, at this time another thread also to build a room ID 2 in the NPC for 1 is map<1,<2,room>>, then I must be the code first

<roomId,room> rooms = data.get (1); If rooms is null, then rooms = new map<roomid,room> (); Then put into data, that is Data.put (1,rooms), but the problem is, under multi-threaded, how to ensure that multiple threads only new one rooms, not each thread is the new one rooms, this is to lock, but to ensure that only new room s out, that must be in the entire data on the lock, this problem is coming, I just want to ensure that the new one rooms out, the result I lock up the whole data, if this side NPC ID not only 1, there are other 2,3,4,5,6,7 ... At this time what to do, if the entire data, the other NPC corresponding rooms will be locked, performance consumption problems, then how to do it, the first to define the data into Concurrenthashmap, that is

Concurrenthashmap data = null; Then using the Putifabsent () method in Concurrenthashmap, Concurrenthashmap is a thread-safe collection, and Putifabsent () is also a thread-safe atomic-level operation, so
can be directly data.putifabsent (1,new <roomId,room>); Putifabsent () meaning that the value corresponding to the key if it does not exist, then the new one comes out and puts it in.(The first time putifabsent () returns null directly,
as with the normal map put method, if the map does not have a key corresponding to the value, the first put to return null, each time because of the value of the return is the key corresponding to the value of the existence, here Putifabsent () for the first time if there is no new value and then put in
return is also null, and get will get the value corresponding to key)Returns the old value if it exists (not new, directly with the old return). Analysis, if 2 threads are simultaneously to the NPC 1 of the rooms,2 to get the rooms if all is null, all need
New rooms, the first line enters Upgradeable putifabsent (), because the Putifabsent () method is atomic-level thread-safe, so that the second thread calls Putifabsent () in a blocked state, Until the first thread calls Putifabsent ()
After releasing the lock after the second line friend can call Putifabsent (), because the first one is new and put into data, the second thread calls Putifabsent () from the data to get the value, so it will not be new, but directly use
The value of the first thread new, which guarantees the synchronization security of the multi-line Chengjian.

Complement: There is also a disgusting sync lock, such as a team copy of map<npcid,map<roomid,room>> this structure defined variable is data, this data has to join the room (players to join in the room), have to exit To remove players from the
No one in the room will be removed from data, there is a quick match rooms operation (to remove the house from data), these three kinds of operations will be modified, some room to add people, some to the room to reduce people and delete room, some
Directly to delete the bedroom, this time must be locked, such as a thread in my add to the bedroom, another thread just put the box to remove from the data, that even if the first my thread to add people in the box is successful and what use, you add success
As a result, the room is removed from the data, that is, you are in the first thread to add to the room, the second thread to remove rooms from data, the first line loads the person entered two dimensions, because the room in the data reference is
Remove, room the whole is abandoned, room whole was deleted into the recycling state, rooms the whole entered two times, the rooms of the people must be more into the two dimensional space, join this time when the addition of people who join the number of times a day limit, that the first thread
Deal with the person will be a hole to fall once, so that the data is confused, so the lock, then how to add the lock? map<roomid,room> rooms = Data.get (npcid); Do you lock this rooms? So if I'm joining a room,
The result of the whole NPC under the corresponding rooms are locked, and some other players are not the operation of the room (such as quitting the room, etc.) is also not done, that does not add the whole rooms of the lock words and how can not cause data confusion? The solution has a way of thinking,
is to operate which shared object on which object to lock, here Add a person is to operate a single room, then the entire room lock is the most legitimate practice, then how to ensure the synchronization of data security? The workaround is to add a state to the class that I lock on this single
Then in the synchronization block in this single room in the judgment of that State, because the other thread operation will also lock the individual room, so the state of the modification must be only one thread at the same time, so there is no data confusion problem.

Lock Position (eq:map<key,map<>> double collection How to lock only the object position used, instead of locking the entire set)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.