-2

Singleton pattern allows to contain one instance per application thread.

How can I make sure only single instance of guava Service Manager is running per JVM ? So when ever it launches a new seperate entry java thread can check whether the service manager is running.

Njax3SmmM2x2a0Zf7Hpd
  • 1,354
  • 4
  • 22
  • 44

1 Answers1

1

Why do you think that simply not creating multiple instances wouldn't work? Implement a ServiceManagerProvider as a singleton and use only serviceManagerProvider.get() for accessing the Service Manager.


Consider using Dependency Injection instead of the singleton (anti-)pattern:

@Singleton
public class ServiceManagerProvider implements Provider<ServiceManager> {
    private final ServiceManager serviceManager = ...

    @Overrride
    public ServiceManager get() {
        return serviceManager;
    }
}

Here, you get a single instance per injector, which is exactly what you (should) want.

maaartinus
  • 44,714
  • 32
  • 161
  • 320