Replace hashtables with concurrenthashmaps #3488
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Re-Opened PR of #3486 due to bad merging on my part.
ConcurrentHashMap vs Hashtable
Hashtables are much slower than a normal hashmap. Although they provide thread-safety, they do so at the cost of locking the entire map for reads and writes.
They are also outdated compared to concurrenthashmaps (hashtables were introduced with the original java util, while concurrenthashmaps were introduced in jdk 1.5)
ConcurrentHashMaps are much more efficient because instead of locking the entire map, they lock individual bins. They also do not lock on reads, which makes read-access much faster than a hashtable. Furthermore, they offer thread-safe iteration since they copy the internal data to the iterator.
If that doesn't convince you to use ConcurrentHashMaps, then I don't know what will.
PR Change
This PR simply changes the hashtables within the TownyUniverse class and the hashtable for townblocks within the TownyWorld class to ConcurrentHashMaps.
Regarding ABI changes, with the latest Towny API most of the previous hashtables were only for internal use and not directly accessible by external plugins anyway. Thus the change should not break any external plugins.
I have tested this PR, but non-extensively; I really doubt anything will break anyway.