Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Concurrent access to HashMap #30

Closed
ikoblik opened this issue May 8, 2013 · 8 comments
Closed

Concurrent access to HashMap #30

ikoblik opened this issue May 8, 2013 · 8 comments
Labels

Comments

@ikoblik
Copy link

ikoblik commented May 8, 2013

In AbstractSyncMessageBus field subscriptionsPerListener is accessed asynchronously. Simplest solution would be to use ReadWriteLock or ConcurrentHashMap. Please see this discussion:

http://www.coderanch.com/t/232685/threads/java/Concurrent-access-HashMap

@ikoblik
Copy link
Author

ikoblik commented May 8, 2013

Same issue in AbstractConcurrentSet.

@bennidi
Copy link
Owner

bennidi commented May 8, 2013

Yes, it is accessed concurrently without synchronization...but read-only. And that's the point - you don't need to synchronize read access to any data structure. I designed the access to all data structures according to this principle to increase through-put. The thread you are pointing to actually talks about concurrent read/write access, which of course needs to be synchronized.

@bennidi bennidi closed this as completed May 8, 2013
@ikoblik
Copy link
Author

ikoblik commented May 8, 2013

you don't need to synchronize read access to any data structure

That's a little too broad, it depends on implementation of the data structure in question.

Please see the JavaDoc of HashMap: http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html . Line starting with "Note that this implementation is not synchronized.".

There's blog post stating that non-synchronized access can lead to an infinite loop:
http://lightbody.net/blog/?p=307

@ikoblik
Copy link
Author

ikoblik commented May 9, 2013

Hi bennidi,

I had a little spare time today to write unit test showing the issue. It's quite hard to reproduce infinite cycle but it's quite easy to show inconsistency arising from concurrent update while reading HashMap. You can find the unit test here:
https://gist.github.com/ikoblik/5545835

Hope it helps!
Ivan.

@bennidi
Copy link
Owner

bennidi commented May 9, 2013

Interesting! Thanks for pointing that out. I haven't thought about the contains() method as it is not a vital part of my code currently. But it definitely needs to be in sync with the writes. I think I will introduce a ReadWriteLock now. As for the subscription code. The rehashing issue also exists in that part of the code then. I will take a closer look and fix that asap.

you don't need to synchronize read access to any data structure

You're right, in the end it depends on the implementation -> repeated reads to the same elements/segments/whatever might lead to internal optimizations of the data structure that result in reordering and thus, writes. But as for the HashMap and article you pointed to: It clearly says, that at least one thread needs to modify the map to make it behave unpredictably. So usually, concurrent reads don't need to be synchronized.

Anyways, thanks a lot for taking the time to provide that test case and shed some light on this specific detail of maps rehashing behavior.

@ikoblik
Copy link
Author

ikoblik commented May 10, 2013

Sure, glad to help! Thank you for the good work you're doing.

@bennidi bennidi reopened this May 11, 2013
@bennidi
Copy link
Owner

bennidi commented May 11, 2013

I adapted your test case and reproduced the error you described. It includes your documentation and I mentioned your name and blog URL to give you some credit - if that's ok with you.

I introduced the ReentrantReadWriteLock to correctly synchronize the set implementation. I will do the same for the subscription code. It will definitely make it into the next release which should be ready within a week or so.

@bennidi bennidi closed this as completed May 11, 2013
@bennidi bennidi reopened this May 11, 2013
@ikoblik
Copy link
Author

ikoblik commented May 12, 2013

Hi! You didn't have to link to me. The linked test is published under MIT or any other license you chose to use for mbassador in future. It was pleasure interacting with you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants