Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
update DynamicFactory to be thread safe - when a constructor for give…
…n parameters is not found a lock is obtained before searching for an appropriate match.

Change-Id: I5d43e9263cb37cf985f6426fa892dd3e31e74529
Signed-off-by: Egon Willighagen <egonw@users.sourceforge.net>
  • Loading branch information
johnmay authored and egonw committed Mar 24, 2013
1 parent 99f3c7c commit 700bf50
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/main/org/openscience/cdk/DynamicFactory.java
Expand Up @@ -596,22 +596,29 @@ public <T extends ICDKObject> T ofClass(Class<T> intf) {
* @throws IllegalArgumentException thrown if a key is provided which cannot
* be resolved.
*/
@SuppressWarnings("unchecked")
private <T> Creator<T> get(ConstructorKey key) {


@SuppressWarnings("unchecked")
Creator<T> creator = (Creator<T>) cache.get(key);


if (creator != null) {
return creator;
} else {
creator = find(key);
return register(key, creator); // avoids invoking find again
synchronized (lock) {
// if the creator is still null... try and find one and register it
if((creator = (Creator<T>) cache.get(key)) == null) {
creator = find(key);
creator = register(key, creator); // avoids invoking find again
}
}
}

return creator;

}

/* thread lock for finding new constructors */
private final Object lock = new Object();

/**
* Find a constructor whose parameters are assignable from the provided
Expand Down

0 comments on commit 700bf50

Please sign in to comment.