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 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?

Was it helpful?

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top