You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Oliver Heger (JIRA)" <ji...@apache.org> on 2011/02/08 22:26:57 UTC

[jira] Updated: (LANG-678) Add support for ConcurrentMap.putIfAbsent()

     [ https://issues.apache.org/jira/browse/LANG-678?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Oliver Heger updated LANG-678:
------------------------------

    Attachment: putIfAbsent.patch

This patch adds the methods {{putIfAbsent()}}, {{createIfAbsent()}}, and {{createIfAbsentUnchecked()}} to {{ConcurrentUtils}}. Corresponding unit tests are included.

> Add support for ConcurrentMap.putIfAbsent()
> -------------------------------------------
>
>                 Key: LANG-678
>                 URL: https://issues.apache.org/jira/browse/LANG-678
>             Project: Commons Lang
>          Issue Type: New Feature
>          Components: lang.concurrent.*
>            Reporter: Oliver Heger
>            Priority: Minor
>         Attachments: putIfAbsent.patch
>
>
> The ConcurrentMap interface provides the {{putIfAbsent()}} method for adding keys which are not yet present in the map in an atomic operation. However, using this method is not always convenient because in many cases the return value is not what you want: If the value is newly added, *null* is returned. Typically the calling code is rather interested in the value which is now stored in the map. This is a proposal to add a new {{putIfAbsent()}} method to {{ConcurrentUtils}} which returns this value.
> Also, when working with a concurrent map, a typical pattern is to check whether the map contains a key. If not, a value is created and put into the map - unless another thread was faster. An example of such a use case is a map acting as a cache. {{ConcurrentUtils}} can provide support for such use cases by combining the {{putIfAbsent()}} operation with a {{ConcurrentInitializer}}: If the key is not found in the map, the initializer is called to create the corresponding value. Finally, {{putIfAbsent()}} is called to add the value to the map.

-- 
This message is automatically generated by JIRA.
-
For more information on JIRA, see: http://www.atlassian.com/software/jira