atlas-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Satish Duggana (JIRA)" <j...@apache.org>
Subject [jira] [Updated] (ATLAS-1014) Unnecessary locking and double checked locking for a threadlocal in RequestContext.
Date Tue, 12 Jul 2016 16:16:20 GMT

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

Satish Duggana updated ATLAS-1014:
----------------------------------
    Description: 

    public static RequestContext get() {
        if (CURRENT_CONTEXT.get() == null) {
            synchronized (RequestContext.class) {
                if (CURRENT_CONTEXT.get() == null) {
                    createContext();
                }
            }
        }
        return CURRENT_CONTEXT.get();
    }

There is no need to have double checked locking and synchronization for setting current thread's
state. That logic can be removed by adding the below code while instantiating CURRENT_CONTEXT.

``` java
    private static final ThreadLocal<RequestContext> CURRENT_CONTEXT = new ThreadLocal<RequestContext>()
{
        @Override
        protected RequestContext initialValue() {
            RequestContext context = new RequestContext();
            context.requestTime = System.currentTimeMillis();
            return context;
        }
    };
```

RequestContext#createContext usages can be changed to RequestContext#get

  was:


    public static RequestContext get() {
        if (CURRENT_CONTEXT.get() == null) {
            synchronized (RequestContext.class) {
                if (CURRENT_CONTEXT.get() == null) {
                    createContext();
                }
            }
        }
        return CURRENT_CONTEXT.get();
    }
There is no need to have double checked locking and synchronization for setting current thread's
state. That logic can be removed by adding the below code while instantiating CURRENT_CONTEXT.

    private static final ThreadLocal<RequestContext> CURRENT_CONTEXT = new ThreadLocal<>()
{
        @Override
        protected Object initialValue() {
            RequestContext context = new RequestContext();
            context.requestTime = System.currentTimeMillis();
            return context;
        }
    };


RequestContext#createContext usages can be changed to RequestContext#get


> Unnecessary locking and double checked locking for a threadlocal in RequestContext.
> -----------------------------------------------------------------------------------
>
>                 Key: ATLAS-1014
>                 URL: https://issues.apache.org/jira/browse/ATLAS-1014
>             Project: Atlas
>          Issue Type: Task
>            Reporter: Satish Duggana
>
>     public static RequestContext get() {
>         if (CURRENT_CONTEXT.get() == null) {
>             synchronized (RequestContext.class) {
>                 if (CURRENT_CONTEXT.get() == null) {
>                     createContext();
>                 }
>             }
>         }
>         return CURRENT_CONTEXT.get();
>     }
> There is no need to have double checked locking and synchronization for setting current
thread's state. That logic can be removed by adding the below code while instantiating CURRENT_CONTEXT.
> ``` java
>     private static final ThreadLocal<RequestContext> CURRENT_CONTEXT = new ThreadLocal<RequestContext>()
{
>         @Override
>         protected RequestContext initialValue() {
>             RequestContext context = new RequestContext();
>             context.requestTime = System.currentTimeMillis();
>             return context;
>         }
>     };
> ```
> RequestContext#createContext usages can be changed to RequestContext#get



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Mime
View raw message