[ 
https://issues.apache.org/jira/browse/LANG-1611?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17264566#comment-17264566
 ] 

Andrey Fadeev edited comment on LANG-1611 at 1/14/21, 3:16 AM:
---------------------------------------------------------------

If there's only one token for an Initializer instance, why not trigger 
initialization simply by calling the get() method, like in the 
\{{AtomicInitializer but using check for expiration instead of a }}null?

 


was (Author: andrey fadeev):
If there's only one token for an Initializer instance, why not trigger 
initialization simply by calling the get() method, like in the 
{{AtomicInitializer but using check for expiration instead of a }}null?

 
{quote}{color:#bbb529}@Override
{color}{color:#cc7832}public {color}{color:#507874}T 
{color}{color:#ffc66d}get{color}() {color:#cc7832}throws 
{color}ConcurrentException {
 {color:#507874}T {color}result = 
{color:#9876aa}reference{color}.get(){color:#cc7832};
{color}{color:#cc7832}
{color}{color:#cc7832} if {color}(result == {color:#cc7832}null{color}) {
 result = initialize(){color:#cc7832};
{color}{color:#cc7832} if 
{color}(!{color:#9876aa}reference{color}.compareAndSet({color:#cc7832}null, 
{color}result)) {
 {color:#808080}// another thread has initialized the reference
{color} result = {color:#9876aa}reference{color}.get(){color:#cc7832};
{color} }
 }

 {color:#cc7832}return {color}result{color:#cc7832};
{color}}
{quote}

> Add an initializer which can compare and re-initialize value
> ------------------------------------------------------------
>
>                 Key: LANG-1611
>                 URL: https://issues.apache.org/jira/browse/LANG-1611
>             Project: Commons Lang
>          Issue Type: New Feature
>          Components: lang.concurrent.*
>            Reporter: Franz Wong
>            Priority: Minor
>
> I would like to have a new class which is similar to 
> {{AtomicSafeInitializer}}. But we can compare and re-initialize the value.
> The use case is to refresh access token (e.g. OAuth) when it expires. 
> Multiple threads tries to refresh the same token at the same time, but there 
> should be only one thread to do it, in addition, only the thread which passes 
> the expired token can trigger the initialization.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to