This discussion is archived
9 Replies Latest reply: Feb 5, 2013 8:56 PM by r035198x RSS

create threadlocal     when called   multiple times ?

user11138293 Newbie
Currently Being Moderated
code to set in threadlocal
               @Override
     public void doWorkflowAction(WFEntity wfEntity, int actionId,
               Map<? extends  Object, ? extends  Object> inputs) {
          wfEntityService.createOrUpdate(wfEntity);
          String workflowName=wfEntity.getHibernateWorkflowEntry().getWorkflowName();
          OsWorkflowContext  workflowContext=OsWorkflowContextHolder.getWorkflowContext();
          workflowContext.setCaller(UserContextHolder.getUserName());
          workflowContext.setInstanceId(wfEntity.getProcessInstanceId());
          findWorkflowTemplate(workflowName, wfEntity.getOrgAbbrev()).doAction(actionId, inputs);
     }
the method doWorkflowAction first creates threadlocal object calls doAction on the worklfowTemplate this will result in calling several other objects and if one the object calls doWorkflowAction with diffeerent paramenters

OsWorkflowContext workflowContext=OsWorkflowContextHolder.getWorkflowContext(); line will it create a new threadlocal varaible or update the current one?

its all running in the same thread , please advice

in other a method creates athreadlocal varaible and calls somemethod in that method I can access the threadlocalvariable if this method again creates the threadlocal variable will there be two threadlocalvaraibles in the same thread ?
I am confusd please help me understand.
  • 1. Re: create threadlocal     when called   multiple times ?
    EJP Guru
    Currently Being Moderated
    Suppose you post the code you are asking about, instead of just the code that calls it?
  • 2. Re: create threadlocal     when called   multiple times ?
    user11138293 Newbie
    Currently Being Moderated
    I did not understand what do you mean ? You want me to post the code what I am referring to ?
  • 3. Re: create threadlocal     when called   multiple times ?
    EJP Guru
    Currently Being Moderated
    What exactly is it that is so hard to understand?
  • 4. Re: create threadlocal     when called   multiple times ?
    user11138293 Newbie
    Currently Being Moderated
    I am sorry but I dont understand
    Suppose you post the code you are asking about, instead of just the code that calls it?
    can you plz put in other words., thanks and appreciate it.
  • 5. Re: create threadlocal     when called   multiple times ?
    EJP Guru
    Currently Being Moderated
    Come off it. There is nothing hard to understand about a request to post more code, and there is certainly no doubt as to what code.

    If you don't want your question answered, you're going the right way about it: in fact you're well on the way to getting it locked as unanswerable.
  • 6. Re: create threadlocal     when called   multiple times ?
    user11138293 Newbie
    Currently Being Moderated
    more code sure

    here I Have the method
         @Override
         public void doWorkflowAction(WFEntity wfEntity, int actionId,
                   Map<? extends  Object, ? extends  Object> inputs) {
              wfEntityService.createOrUpdate(wfEntity);
              String workflowName=wfEntity.getHibernateWorkflowEntry().getWorkflowName();
              OsWorkflowContext  workflowContext=OsWorkflowContextHolder.getWorkflowContext();
              workflowContext.setCaller(UserContextHolder.getUserName());
              workflowContext.setInstanceId(wfEntity.getProcessInstanceId());
              findWorkflowTemplate(workflowName, wfEntity.getOrgAbbrev()).doAction(actionId, inputs);
         }
    this method is creating Threadlocal object using OsWorkflowContextHolder
    public abstract class OsWorkflowContextHolder {
    
         /**
          * Holds the <code>OsWorkflowContext</code> on a per-thread basis.
          */
         private static ThreadLocal workflowContextHolder = new ThreadLocal() {
              protected Object initialValue() {
                   return new OsWorkflowContext();
              }
         };
    
         /**
          * Gets the <code>OsWorkflowContext</code> for the current thread.
          */
         public static OsWorkflowContext getWorkflowContext() {
              return (OsWorkflowContext) workflowContextHolder.get();
         }
    
         /**
          * Binds a new <code>OsWorkflowContext</code> to the current thread.
          */
         public static void clearWorkflowContext() {
              workflowContextHolder.set(new OsWorkflowContext());
         }
    }
    
    
    public class OsWorkflowContext {
    
         /**
          * Stores the workflow instance ID.
          */
         private Long instanceId;
    
         /**
          * Stores the caller identity.
          */
         private String caller;
    
         /**
          * Gets the workflow instance ID.
          */
         public long getInstanceId() {
              return instanceId.longValue();
         }
    
         /**
          * Sets the workflow instance ID.
          */
         public void setInstanceId(long instanceId) {
              this.instanceId = new Long(instanceId);
         }
    
         /**
          * Gets the caller identity.
          */
         public String getCaller() {
              return caller;
         }
    
         /**
          * Sets the caller identity.
          */
         public void setCaller(String caller) {
              this.caller = caller;
         }
    
         /**
          * Returns <code>true</code> if a workflow instance ID is currently bound to this context, else returns <code>false</code>.
          */ 
         public boolean hasInstanceId() {
              return (instanceId != null);
         }
    }
    and finally calls
    doAction(actionId, inputs);
    doAction(actionId, inputs); method has a big chain it calls several other objects and everywhere I need threadlocal object I call
         
              Workflow workflow = createWorkflow(OsWorkflowContextHolder.getWorkflowContext().getCaller());
     
    now In one of the methods triggered by call to doAction(actionId, inputs); I cam calling

    doWorkflowAction(WFEntity wfEntity, int actionId,
                   Map<? extends  Object, ? extends  Object> inputs)
      
    this is second call , will this result in replacing the object in threadlocal or create a new object in threadlocal?
    what happens after the second call ?

    Edited by: user11138293 on Jan 16, 2013 3:29 PM

    Edited by: user11138293 on Jan 16, 2013 3:30 PM

    Edited by: user11138293 on Jan 16, 2013 3:30 PM
  • 7. Re: create threadlocal     when called   multiple times ?
    r035198x Pro
    Currently Being Moderated
    The short answer would be that as long as the methods are running on the same thread they will get the same instance (initialized once by the first call). The initialization is only done if get is called from a thread where the value was not previously initialized.
  • 8. Re: create threadlocal     when called   multiple times ?
    user11138293 Newbie
    Currently Being Moderated
    but in my case its all one thread no multiple threads so initially when my thread calls
    OsWorkflowContext  workflowContext=OsWorkflowContextHolder.getWorkflowContext();
              workflowContext.setCaller(UserContextHolder.getUserName());
              workflowContext.setInstanceId(wfEntity.getProcessInstanceId());
    threadlocal variable is created with instance of OsWorkflowContext this instance has set methods so if I update this instance using set methods all further calls to OsWorkflowContextHolder.getWorkflowContext(); will return just the updated instance right ?
  • 9. Re: create threadlocal     when called   multiple times ?
    r035198x Pro
    Currently Being Moderated
    Yes, and you can test it out easily to see for yourself. If you have only one thread then you don't need to use a thread local. If you do have multiple threads started then take care to clear those variables (in a finally block) when you are done with them.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points