Skip to content

Concurrent Singleton

Randgalt edited this page Jan 4, 2013 · 7 revisions

Guice’s default Singleton scope synchronizes all object creation on a single lock (see here). It does this to avoid deadlocks with circular dependencies. Governator adds the FineGrainedLazySingleton annotation that locks on the key so that multiple singletons can be created concurrently. Circular dependencies are rare so FineGrainedLazySingleton risks deadlocks in those situations for the benefit of better concurrency.

A class annotated with FineGrainedLazySingleton will be…

  • …lazily created (like Lazy Singleton)
  • …created by the FineGrainedLazySingletonScope which synchronizes on the Guice Key instead of InternalInjectorCreator.class)
  • …able to be created alongside other FineGrainedLazySingleton in different threads