java - Is a ConcurrentHashSet required if threads are using different keys? -
suppose have hash set of request ids i've sent client server. server's response returns request id sent, can remove hash set. run in multithreaded fashion, multiple threads can adding , removing ids hash set. however, since ids generated unique (from thread safe source, let's atomicinteger
gets updated each new request), hashset
need concurrenthashset
?
i think case might cause problem if hashset
encounters collisions may require datastructure changes underlying hashset
object, doesn't seem occur in use case.
yes. since underlying array hash table might need resized instance , because of course ids can collide. having different keys not @ all.
however, since know ids increasing, , if can have upper bound on maximum number of ids outstanding (lets 1000). can work upper , lower bound , fixed size array offset indexing lowest key, in case not need mutexes or concurrent data structure. such data structure fragile since if have more upper bound oustanding hell break loose. unless performance of concern, use concurrenthashset
.