Thanks Sam. Your reply now adds clarity to my observations. I have made changes, switched to toProvider(SomeProvider.class) as requirement is for unique instances all the times in our case.
Regards, Surya. On Jan 5, 10:09 am, Sam Berlin <[email protected]> wrote: > There's nothing special about @Provides vs toProvider. @Provides is just > syntactic sugar for creating a Provider<T> instance and binding it with > toProvider(instance). > > There's nothing built into Guice to make Providers thread-safe. If your > provider has state that is mutated among different calls to provider.get(), > you need to handle synchronization manually if you expect the provider to > be called from different threads. The same thing applies to the actual > objects Guice injects (ie, non-providers). > > I think the catch is that toProvider(SomeProvider.class) is an unscoped > provider binding, so each injection of Provider<T> will create a new > instance of the Provider (I think), so there's no shared state since each > instance is different. @Provides is analogous to > toProvider(providerInstance), which in turn is analogous to > bind(..).toInstance(..), all of which act as singletons and need to be > thread-safe. > > sam > > > > > > > > On Thu, Jan 5, 2012 at 12:18 AM, surya aditya <[email protected]> wrote: > > Dear Friends, > > > In multithreaded environment in multi-core cpu machine, I found > > @provides to be not-thread safe. However if I write Provider class and > > bind using 'toProvider' then it is showing expected behavior. > > > Is this my right understanding that @Provides is not thread safe. Has > > anybody experienced this before? By not thread safe I mean an instance > > of an object is being reused when i use @provides, ofcourse this was > > totally unacceptable as the Object has instance variable causing > > inconsistencies. > > > Appreciate feedback. > > > Peace, > > Surya > > > -- > > You received this message because you are subscribed to the Google Groups > > "google-guice" group. > > To post to this group, send email to [email protected]. > > To unsubscribe from this group, send email to > > [email protected]. > > For more options, visit this group at > >http://groups.google.com/group/google-guice?hl=en. -- You received this message because you are subscribed to the Google Groups "google-guice" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.
