简体   繁体   中英

How to configure Guice modules in parallel?

Background: My codebase has a lot of modules and, according to some profiling, creating the Guice injector takes a significant amount of time. This is almost certainly due to the massive amount of modules and the existence of a few modules that take a long time to configure. In theory I can produce 2+ lists of modules that can be configured separately.

Is there any way to parallelize the configuration of these modules?

For example, if there's a way to combine or merge two Guice injectors I could create them in separate threads then join them afterwards.

You might want to use Concurrent Singleton library from Netflix, it allows you to lazy load the Guice modules in concurrent fashion.

https://github.com/Netflix/governator/wiki/Concurrent-Singleton

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 Guice 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM