[concurrency-interest] Concurrent Bidirectional Map

Dhanji R. Prasanna dhanji at gmail.com
Sat Nov 21 23:28:28 EST 2009

Unfortunately, if you synchronize put() and remove() you lose the
concurrency benefits of CHM. All threads will serialize behind the put lock.
Also, in your implementation, in order for the bi-map-put to be perceived
atomically, you would have to synchronize the get method as well =(

It is an interesting use case, however.
my 2c.


On Sun, Nov 22, 2009 at 2:54 PM, Norman Elton <normelton at gmail.com> wrote:

> Let me start by saying that I am, by no means, a concurrency guru. But
> I did have a use for a concurrent bidirectional map, one that
> maintains key-to-value and value-to-key uniqueness. I whipped up a
> quick implementation of ConcurrentBiMap built around two
> ConcurrentHashMaps. The BiMap implements Map<K,V>, with the single
> extra method inverse(), which returns a Map<V,K>.
> I basically synchronized all the modifying methods (clear, put,
> remove, etc) so that I could safely change both maps atomically. All
> other methods are unsynchronized. The methods that would normally
> return modifiable Sets (entrySet, values, keySet) return unmodifiable
> versions of their "normal" values, since I have not yet come up with a
> way to allow the user to atomically remove a value from such a set.
> I'll attach both the code and a fairly extensive JUnit test case. A
> few questions remain:
> - Have I reinvented the wheel? Is there already a good / better
> implementation of a ConcurrentBiMap?
> - Is there a good way to test the atomicity of code? I could fire up
> multiple threads and let them simultaneously hammer away at a shared
> hash, but I'm not sure this would be a good reproducible test case.
> - Can anyone think of a way to allow the three Set methods (entrySet,
> keySet, values) to return modifiable values? That is, if you remove an
> item from them, they atomically affect the original BiMap.
> Thanks for any pointers,
> Norman
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20091122/948dd166/attachment.html>

More information about the Concurrency-interest mailing list