powertools-lambda-java icon indicating copy to clipboard operation
powertools-lambda-java copied to clipboard

Some code suggestion from CodeGuru

Open XinyuLiu5566 opened this issue 4 years ago • 1 comments

  • The code is using ConcurrentHashMap, but the usage of containsKey() and get() may not be thread-safe. In between the check and the get() another thread can remove the key and the get() will return null. Fix: Consider calling get(), checking instead of your current check if the returned object is null, and then using that object only, without calling get() again.

  • This code uses '%s' to format int: 'size' expression. Use %d, not %s, for integers. This ensures locale-sensitive formatting.

  • Replacing put() with putIfAbsent() to help prevent accidental overwriting. putIfAbsent() puts the value only if the ConcurrentHashMap does not contain the key and therefore avoids overwriting the value written there by the other thread's putIfAbsent().

XinyuLiu5566 avatar Oct 11 '21 12:10 XinyuLiu5566

Hi @XinyuLiu5566 Thanks for opening the PR. There seems to be some failing tests. Can you please look into it ?

pankajagrawal16 avatar Oct 11 '21 12:10 pankajagrawal16

Thanks @jeromevdl for merging #984. Could you please close this PR, as it contains the same changes and my PR was created just to fix some issues with this one.

kozub avatar Nov 16 '22 09:11 kozub

Fixed with #984

jeromevdl avatar Nov 16 '22 09:11 jeromevdl