Scalable way to access every element of ConcurrentHashMap exactly once

前端 未结 3 1898
暗喜
暗喜 2021-01-06 17:40

I have 32 machine threads and one ConcurrentHashMap map, which contains a lot of keys. Key has defined a public method visit

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-06 18:33

    I could try to inherit from ConcurrentHashMap, get my hands on the instances of its inner Segment, try to group them into 32 groups and work on each group separately. This sounds like a hardcore approach though.

    Hardcore indeed but about the only thing I would see that would work. toArray() builds the array by doing an enumeration so no win there. I can't believe that a synchronized HashSet would be better unless the ratio of visit() runs to other map operations is pretty high.

    The problem with the working with the Segments is that you are going to have to be extremely careful that your code is resilient because I assume other threads may be altering the table at the same time you are visiting the nodes and you need to avoid the inevitable race conditions. Delicate for sure.

    The big question in my mind is if this is necessary? Has a profiler or timing runs shown to you that this is taking too long to visit() each of the keys in one thread? Have you tried to do a thread-pool for each visit() call and have one thread doing the enumeration and the pool threads doing the visit()?

提交回复
热议问题