I have 32 machine threads and one ConcurrentHashMap
, which contains a lot of keys. Key
has defined a public method visit
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 Segment
s 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()
?