You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Esbrook, Scott" <Sc...@compuware.com> on 2002/10/03 13:45:21 UTC

threading question

Hello,

Quick Struts/servlet threading question: If many classes extended the
following 'AbstractAction' class (eg, 'ConcreteAction'), would use of the
'testString' member variable be threadsafe?

public abstract class AbstractAction extends Action{
   protected String testString;
   public ActionForward perform(
      ActionMapping map,ActionForm form,HttpServletRequest
req,HttpServletResponse res){
      testString = req.getParameter("myParam");
      // other common processing....
      return process(map,form,req,res);
   }
   public abstract ActionForward process(
      ActionMapping map,ActionForm form,HttpServletRequest
req,HttpServletResponse res);
}
public class ConcreteAction extends AbstractAction{
   public ActionForward process(
      ActionMapping map,ActionForm form,HttpServletRequest
req,HttpServletResponse res){
      // do something with testString...
      // return an ActionForward object...
   }
}

I was strictly thinking of using the protected members on a per-request
basis, always initializing them in the AbstractAction's perform() method.
Furthermore, if use of testString is OK, wouldn't it be OK to have a member
vars of type HttpRequest, etc. and set it/them in AbstractAction's
perform(), thereby avoiding all the associated parameter passing? 

Using Struts 1.0.2 if that makes any difference.

Thanks,

Scott Esbrook
Software Developer
Compuware Corporation



The contents of this e-mail are intended for the named addressee only. It
contains information that may be confidential. Unless you are the named
addressee or an authorized designee, you may not copy or use it, or disclose
it to anyone else. If you received it in error please notify us immediately
and then destroy it. 


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: threading question

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Thu, 3 Oct 2002, Esbrook, Scott wrote:

> Date: Thu, 3 Oct 2002 07:45:21 -0400
> From: "Esbrook, Scott" <Sc...@compuware.com>
> Reply-To: Struts Users Mailing List <st...@jakarta.apache.org>
> To: "'struts-user@jakarta.apache.org'" <st...@jakarta.apache.org>
> Subject: threading question
>
> Hello,
>
> Quick Struts/servlet threading question: If many classes extended the
> following 'AbstractAction' class (eg, 'ConcreteAction'), would use of the
> 'testString' member variable be threadsafe?
>

As others eventually drank enough coffee to figure out :-), the testString
instance in your example below is *not* threadsafe if you expect to store
per-request state information in it.  The general rule is that you can use
*local* variables for this sort of thing (because they get created on the
stack for each thread), but not instance or static variables.

Remember that Struts will create only one instance of each ConcreteAction
for a given action path, so multiple simultaneous requests to the same
Action will share that instance (and therefore the instance variable).

Craig

> public abstract class AbstractAction extends Action{
>    protected String testString;
>    public ActionForward perform(
>       ActionMapping map,ActionForm form,HttpServletRequest
> req,HttpServletResponse res){
>       testString = req.getParameter("myParam");
>       // other common processing....
>       return process(map,form,req,res);
>    }
>    public abstract ActionForward process(
>       ActionMapping map,ActionForm form,HttpServletRequest
> req,HttpServletResponse res);
> }
> public class ConcreteAction extends AbstractAction{
>    public ActionForward process(
>       ActionMapping map,ActionForm form,HttpServletRequest
> req,HttpServletResponse res){
>       // do something with testString...
>       // return an ActionForward object...
>    }
> }
>
> I was strictly thinking of using the protected members on a per-request
> basis, always initializing them in the AbstractAction's perform() method.
> Furthermore, if use of testString is OK, wouldn't it be OK to have a member
> vars of type HttpRequest, etc. and set it/them in AbstractAction's
> perform(), thereby avoiding all the associated parameter passing?
>
> Using Struts 1.0.2 if that makes any difference.
>
> Thanks,
>
> Scott Esbrook
> Software Developer
> Compuware Corporation
>
>
>
> The contents of this e-mail are intended for the named addressee only. It
> contains information that may be confidential. Unless you are the named
> addressee or an authorized designee, you may not copy or use it, or disclose
> it to anyone else. If you received it in error please notify us immediately
> and then destroy it.
>
>
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>