What's cheaper: Traversal using n iterators of a single ConcurrentHashMap or n instances of a HashMap
-
12-10-2019 - |
Question
Imagine a producer-consumer scenario, thread A produces entries, one to many other threads consume them.
For this I am passing a bunch of entries to each consumer thread.
Do do this I am asking myself if it's cheaper (primary in sense of cpu utlization, secondary in memory):
- to provide each consumer thread a seperate instance of a
HashMap
. After passing theMap
to one consumer, a new instance of theMap
will created and used for passing the next produced entries to the next thread
or
- to use a single
ConcurrentHashMap
and create anIterator
for each consumer threads and after passing theIterator
to the thread clearing theMap
- so that each Iterator contains its own view of the underlyingMap
.
What do you think? Is a more-or-less generic answer possible?
Or is it strongly dependent of some variables like number of entries, threads etc?
EDIT: Or should I use some other kind of data structure which may better solve these kinds of problems?
Solution
The java concurrent package provides a data structure for exact this scenario.
@see java.util.concurrent.BlockingDeque
But please do some perfomance test: because the results stongly depends on you use case. And if this is only micro optimization, than a: clean, easy to understand, thread save approach would be much better than performance optimization whithout impact.
OTHER TIPS
The most expensive thing cpu-wise is threads contention. Seems, your first approach will produce no contention at all -- each thread will have it's local version of Map -- at the expense of higher memory consumption.
I'd, for one, benchmark two scenarios for several setups (nr of threads, sizes of map, etc) that make sense. It's hard to tell exact figures without benchmark.