Search code examples
javamultithreadingalgorithmconcurrencyconcurrenthashmap

What's cheaper: Traversal using n iterators of a single ConcurrentHashMap or n instances of a HashMap


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 the Map to one consumer, a new instance of the Map will created and used for passing the next produced entries to the next thread

or

  • to use a single ConcurrentHashMap and create an Iterator for each consumer threads and after passing the Iterator to the thread clearing the Map - so that each Iterator contains its own view of the underlying Map.

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.