33import java .util .HashMap ;
44import java .util .List ;
55import java .util .Map ;
6+ import java .util .concurrent .ConcurrentHashMap ;
67
78import picoded .core .struct .GenericConvertHashMap ;
89import picoded .core .struct .GenericConvertList ;
@@ -115,7 +116,7 @@ protected GenericConvertMap<String, Object> getStackConfig(String name) {
115116 /**
116117 * Stores the respective stack providers
117118 */
118- protected Map <String , CoreStack > providerStackMap = new HashMap <>();
119+ protected final ConcurrentHashMap <String , CoreStack > providerStackMap = new ConcurrentHashMap <>();
119120
120121 /**
121122 * Get the stack of the provider specified by the name,
@@ -132,18 +133,27 @@ public CoreStack getProviderStack(String name) {
132133 return cache ;
133134 }
134135
135- // Cache not found, get config to initialize a new stack
136- GenericConvertMap <String , Object > providerConfig = getStackConfig (name );
137- if (providerConfig == null ) {
138- throw new IllegalArgumentException ("Unknown provider name, config not found : " + name );
136+ synchronized (providerStackMap ) {
137+ // Check the cache again (avoid race condition)
138+ cache = providerStackMap .get (name );
139+ if (cache != null ) {
140+ return cache ;
141+ }
142+
143+ // Cache not found, get config to initialize a new stack
144+ GenericConvertMap <String , Object > providerConfig = getStackConfig (name );
145+ if (providerConfig == null ) {
146+ throw new IllegalArgumentException ("Unknown provider name, config not found : " + name );
147+ }
148+
149+ // Initialization of stack and store into cache
150+ cache = initStack (providerConfig .getString ("type" ), providerConfig );
151+ providerStackMap .put (name , cache );
152+
153+ // Return result
154+ return cache ;
139155 }
140-
141- // Initialization of stack and store into cache
142- cache = initStack (providerConfig .getString ("type" ), providerConfig );
143- providerStackMap .put (name , cache );
144-
145- // Return result
146- return cache ;
156+
147157 }
148158
149159 /**
0 commit comments