[concurrency-interest] Concurrent Bidirectional Map

Norman Elton normelton at gmail.com
Sat Nov 21 22:54:15 EST 2009


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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ConcurrentBiMap.java
Type: application/octet-stream
Size: 4630 bytes
Desc: not available
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20091121/8e98f3e2/attachment.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ConcurrentBiMapTests.java
Type: application/octet-stream
Size: 6561 bytes
Desc: not available
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20091121/8e98f3e2/attachment-0001.obj>


More information about the Concurrency-interest mailing list