Replace 
        synchronized(UpdateXmlAction.class)
with 
        synchronized(this) 

and you have the same behaviour, as if you would make the whole method
synchronized. 
You don't need your locked flag, it's provided by java object monitors
automatically. 

BUT, synchronizing doExecute in an action is a _very_ bad idea, since the
actions are in fact designed to have multiple threads running with it (it
would work in most cases, but it's fine).

Better in your case: just make updateXml() synchronized.

Ah, and also: if you have basic variables you check in different threads,
make them volatile (and don't make them static, since you have exactly one
instance of an action, it could be an instance variable in this case).

Last question: if two users click the link simultaneously, what should
happen? In your code, the second user overwrites the changes of the first
user (silently).

Regards
Leon


> -----Ursprüngliche Nachricht-----
> Von: Durham David R Jr Contr 805 CSPTS/SCE
> [mailto:[EMAIL PROTECTED]
> Gesendet: Dienstag, 14. Dezember 2004 23:46
> An: Struts Users Mailing List
> Betreff: RE: Preventing concurrent execution of an Action
> 
> You might be able to get away with simply adding 'synchronized' to the
> execute method's signature.
> 
> 
>     synchronized public ActionForward execute(...) {
>          updateXml();
>     }
> 
>     private void updateXml() { ... }
> 
> 
> The lock/key threading techniques have to do with, I think, a more
> complex threading issue than the one you've presented.
> 
> 
> - Dave
> 
> 
> > -----Original Message-----
> > From: Behrang Saeedzadeh [mailto:[EMAIL PROTECTED]
> > Sent: Tuesday, December 14, 2004 4:34 PM
> > To: [EMAIL PROTECTED]
> > Subject: Preventing concurrent execution of an Action
> >
> > Hi
> >
> > I've an action that creates an XML file on the server. It's executed
> > by clicking on a link. I don't want multiple instances of it to be
> > executed concurrently.
> >
> > Does an approach like the following work?
> >
> > public class UpdateXmlAction ...
> > {
> >        private static final boolean locked;
> >
> >        public void execute(...) {
> >
> >                if (UpdateXmlAction.locked) {
> >                        return;
> >                }
> >
> >                Synchronized(UpdateXmlAction.class) {
> >                        locked = true;
> >                        updateXml();
> >                        locked = false;
> >                }
> >
> >        }
> > }
> >
> > Thanks in advance.
> >
> >
> > --
> >
> > Behrang Saeedzadeh
> > http://www.jroller.com/page/behrangsa
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> >
> >
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to