Files

14 lines
1015 B
Plaintext

Q1 eager: getInstance() == getInstance() -> true
Q2 naive lazy, 8 threads: distinct instances=more than 1 constructor runs=8
Q3 synchronized, 8 threads: distinct instances=1 constructor runs=1
Q4 DCL with volatile, 8 threads: distinct instances=1 constructor runs=1
Q4 DclSingleton.instance volatile? true
Q4 DclWithoutVolatile.instance volatile? false
Q5 holder before first getInstance(): constructor runs=0
Q5 holder after two getInstance() calls: constructor runs=1 same=true
Q6 serialisation round trip keeps the same instance? classic=false hardened(readResolve)=true enum=true
Q6 reflection newInstance() on classic private constructor returns the singleton? false
Q6 reflection on hardened constructor: InvocationTargetException caused by java.lang.IllegalStateException: already initialised
Q6 reflection on enum constructor: java.lang.IllegalArgumentException: Cannot reflectively create enum objects
Q7 Spring singleton scope: same bean twice from one context? true; same bean across two contexts? false