On Fri, 2 Apr 2021 23:12:21 GMT, Anthony Scarpino <ascarp...@openjdk.org> wrote:

>> According to SO, unsynchronized writes to a map can lead to lock up, see 
>> here:
>> https://stackoverflow.com/a/1003237
>> Or here:
>> https://mailinator.blogspot.com/2009/06/beautiful-race-condition.html?m=1
>> What's the performance impact of adding synchronization on the hash map only?
>
> Ok, thanks for pointing that out, I didn't realize that limitation.  However 
> using synchronizedMap is overkill as it locks every operation.  As I read it, 
> only structural changes like adding and removing keys from the map causes 
> problems.  Getting a value from the map does not.  Therefore I would only 
> need to synchronize the putIfAbsent() at 449, which is pretty minor.  The 
> expunging of the map is already synchronized in WeakHashMap.
> 
> The original code was half the performance of this one.

If you synchronize `putIfAbsent` but not `get` you'll still get the possible 
infinite loop described in the SO post.

-------------

PR: https://git.openjdk.java.net/jdk/pull/3296

Reply via email to