[concurrency-interest] Double Checked Locking and String

Aleksey Shipilev shade at redhat.com
Thu Sep 7 07:05:36 EDT 2017


On 09/07/2017 01:01 PM, Shevek wrote:
> Is the following code correct:
> 
> Map<T, String> m = new ConcurrentHashMap<>();
> 
> public void getString(T t) {
>     String out = m.get(t);
>     if (out != null)
>         return out;
>     synchronized (m) {
>         out = m.get(t);
>         if (out == null) {
>             out = String.valueOf(t); // all fields final.
>             m.put(t, out);
>         }
>     }
>     return out;
> }

Unclear what you want here. ConcurrentHashMap gives you most of the memory semantics you want
without having to go through all this. Also, this seems to be the poster child of
putIfAbsent/computeIfAbsent.

-Aleksey

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: OpenPGP digital signature
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20170907/015573ec/attachment.sig>


More information about the Concurrency-interest mailing list