Skip to content

Commit 4c2aa4e

Browse files
committed
AbstractSingletonService: improve thread safety
The double-checked locking will not work unless the assignment is done at the end of the synchronized block (and maybe not even then: http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html).
1 parent d26aed6 commit 4c2aa4e

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/main/java/org/scijava/plugin/AbstractSingletonService.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,21 +111,25 @@ protected List<? extends PT> filterInstances(final List<PT> list) {
111111
private synchronized void initInstances() {
112112
if (instances != null) return;
113113

114-
instances =
114+
final List<PT> list =
115115
Collections.unmodifiableList(filterInstances(getPluginService()
116116
.createInstancesOfType(getPluginType())));
117117

118-
instanceMap = new HashMap<Class<? extends PT>, PT>();
118+
final HashMap<Class<? extends PT>, PT> map =
119+
new HashMap<Class<? extends PT>, PT>();
119120

120121
for (final PT plugin : instances) {
121122
@SuppressWarnings("unchecked")
122123
final Class<? extends PT> ptClass =
123124
(Class<? extends PT>) plugin.getClass();
124-
instanceMap.put(ptClass, plugin);
125+
map.put(ptClass, plugin);
125126
}
126127

127-
log.info("Found " + instances.size() + " " +
128-
getPluginType().getSimpleName() + " plugins.");
128+
log.info("Found " + list.size() + " " + getPluginType().getSimpleName() +
129+
" plugins.");
130+
131+
instanceMap = map;
132+
instances = list;
129133
}
130134

131135
}

0 commit comments

Comments
 (0)