velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "David Esposito" <espos...@newnetco.com>
Subject RE: Thread safety
Date Mon, 01 Oct 2001 21:40:25 GMT


>
> >I haven't stress tested the hell out of it but I have a "default" context
> >(with a bunch of global variables, tools, etc...) that I nest
> inside of the
> >context that is created during each request ...
> >
> >VelocityContext ctx = new VelocityContext(defaultContext);
> >
> >is that safe?
>
> A more complicated question.  Without looking at the code (look ma, no
> hands), I'd say this operation is technically only safe if the following
> conditions are met:
>   - the default context is constructed before other threads that
> use it are
> created,
>   - nothing is ever added to or removed from the default context after it
> is initially created and populated,
>   - the objects that the default context references are similarly
> immutable.
>
> (In reality, the first condition is probably not met, but there is almost
> certainly other synchronization that makes it work anyway, even though
> technically there could be race conditions on some architectures
> that would
> make it not thread-safe.)
>

Well, what i'm actually referring to is creating a my defaultContext inside
of my Servlet init() method. Which, i believe (but wouldn't swear to without
reading the spec), is guaranteed to execute before any of attempt to "use"
the context via the doPost/doGet  ... The objects I stick in there are, as
you suggest, immutable ... examples include:

- list of states
- list of countries
- "tool" objects that contain formatting functions (static methods) ...

etc ...

so it sounds like, in my particular case, sharing a velocity context across
threads is safe ..

phew ... dodged a bullet there ... ;)

-Dave


Mime
View raw message