Uploaded image for project: 'Atlas'
  1. Atlas
  2. ATLAS-1014

Unnecessary locking and double checked locking for a threadlocal in RequestContext.

    XMLWordPrintableJSON

Details

    • Task
    • Status: Resolved
    • Major
    • Resolution: Done
    • None
    • None
    • None
    • None

    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 a threadlocal state. That logic can be removed by adding the below code while instantiating CURRENT_CONTEXT.

          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

      Attachments

        Activity

          People

            Unassigned Unassigned
            satish.duggana Satish Duggana
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: