RFR: 8304818: Prune HttpURLConnection cache when corresponding Authenticator is garbage collected

Daniel Jeliński djelinski at openjdk.org
Fri Mar 24 16:24:44 UTC 2023


On Fri, 24 Mar 2023 14:51:25 GMT, Daniel Fuchs <dfuchs at openjdk.org> wrote:

>> That's a good point. One solution would be to use a ConcurrentHashMap instead of a simple HashMap.
>
> jshell> var map = Map.of(1,1,2,2,3,3,4,4)
> map ==> {1=1, 2=2, 3=3, 4=4}
> 
> jshell> var map2 = new HashMap<Integer, Integer>();
> map2 ==> {}
> 
> jshell> map2.putAll(map)
> 
> jshell> map2.forEach((k, v) -> map2.remove(k));
> |  Exception java.util.ConcurrentModificationException
> |        at HashMap.forEach (HashMap.java:1432)
> |        at (#4:1)
> 
> jshell> var map3 = new ConcurrentHashMap<Integer, Integer>();
> map3 ==> {}
> 
> jshell> map3.putAll(map)
> 
> jshell> map3.forEach((k, v) -> map3.remove(k));
> 
> jshell> map3.isEmpty()
> $10 ==> true

also, not sure how much it applies to ConcurrentHashMap, but `remove` changes the iteration order of the `HashMap`, and `iterator.remove()` does not.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/13159#discussion_r1147805013


More information about the net-dev mailing list