I would like to know how many singleton instances will be created in case my code containing Singleton Class is deployed multiple times using different input arguments on the same machine. Also i would like to know how JVM will handle such situation. will multiple instances of JVM will be created in such case?
-
5If you are creating multiple JVMs, there will be a singleton instance per JVM. – Andy Turner Mar 08 '16 at 08:18
3 Answers
There is no link between multiple JVMs - they have completely separate state from one another (unless you add some mechanism to make them communicate explicitly, or access shared state e.g. from the same database or files).
Because of this, there is no way of one JVM knowing whether another JVM has an instance of a singleton (or even makes use of the singleton class). This means that there will be one singleton instance per JVM.

- 137,514
- 11
- 162
- 243
-
what will happen in case there in only one JVM. and same application is deployed multiple times within one JVM? – Ankita Agarwal Mar 08 '16 at 09:37
Every unique class loader will have one instance of a Singleton class, even if they are in the same JVM. In your case, since there are multiple deployments on the same JVM, each deployment would use its version of class loader to create the singleton. So you will have multiple instances of the singleton on the JVM.

- 78
- 1
- 9
Singleton object means only one instance is allowed. So there is no question of creating multiple instances. If you create new object of this singleton class,always return first object which is created.

- 447
- 4
- 10