Thread safety

302 views
Skip to first unread message

Robin Sheat

unread,
Mar 13, 2007, 10:18:21 PM3/13/07
to google-guice
Hi there, are the calls to things like injector.getInstance thread-
safe? As I gradually move my program to using Guice throughout, the
unported classes still have a lot of Factory.getSomething() calls
which I'll be implementing with calls to getInstance. Should I
implement synchronisation on them, or is it handled internally? (i.e.
if one request causes a singleton to be created, and another request
comes in while that instance is being created, will that be a
problem?)

Cheers, Robin.

Bob Lee

unread,
Mar 13, 2007, 11:37:30 PM3/13/07
to google...@googlegroups.com

Injector is thread safe, and very concurrent. Don't worry about synchronizing.

Bob

cp

unread,
Apr 28, 2015, 5:19:47 AM4/28/15
to google...@googlegroups.com
In com.google.inject.servlet.GuiceServletContextListener the Injector is set as a servlet context attribute.

So I wonder whether we can have static Injector then.

Since we are using jersey(for REST) in some part of our projects and use guice as the DI.
We wanted to share the same injector reference across both these components, by making it static.

Robert Voliva

unread,
Apr 28, 2015, 8:03:51 AM4/28/15
to google...@googlegroups.com
FWIW - We have used a static injector for many, many years - especially in our Jersey services - and we haven't run into any issues.

--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice...@googlegroups.com.
To post to this group, send email to google...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/1d00f451-e6f2-4501-81f9-454e3f1f1780%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tim Boudreau

unread,
May 18, 2015, 3:25:32 PM5/18/15
to google...@googlegroups.com
On Tuesday, April 28, 2015 at 8:03:51 AM UTC-4, Robert Voliva wrote:
FWIW - We have used a static injector for many, many years - especially in our Jersey services - and we haven't run into any issues.

You won't run into issues, per se, but what you'll find is that it limits the flexibility of the system.

Particularly, if you'd like to be able to cleanly and completely unload an application (say, dynamically reconfigure and restart something) without stopping the JVM and without leaks, a static reference to the injector is going to be a problem.

And you actually can do that sort of thing with Guice, if you add some hooks to manage shut-down and unloading of things (i.e. the equivalent of adding a shutdown hook in Runtime.getRuntime(), and let anything that opens connection pools, thread pools, timers, etc. hang shutdown code to get run off of that).

It's one of those things that people are so unused to being able to do in Java that you don't know how nice it is to have until you can actually do it (say, a server that completely unloads and reconfigures itself based on a unix signal, transparently and quickly).

-Tim


Reply all
Reply to author
Forward
0 new messages