Skip to content

Commit

Permalink
#34 fixed possible issues with concurrent access to ServiceLoader ins…
Browse files Browse the repository at this point in the history
…tance
  • Loading branch information
leventov committed Feb 3, 2015
1 parent 0e97692 commit 01f91bd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@
* @see HashCharShortMap
*/
public final class HashCharShortMaps {

private static final ServiceLoader<HashCharShortMapFactory> LOADER =
ServiceLoader.load(HashCharShortMapFactory.class);
private static HashCharShortMapFactory defaultFactory = null;

private static class DefaultFactoryHolder {
private static final HashCharShortMapFactory defaultFactory =
ServiceLoader.load(HashCharShortMapFactory.class).iterator().next();
}

/**
* Returns the default {@link HashCharShortMapFactory} implementation, to which
Expand All @@ -54,13 +55,7 @@ public final class HashCharShortMaps {
*/
@Nonnull
public static /*<>*/ HashCharShortMapFactory/*<>*/ getDefaultFactory() {
if (defaultFactory != null) {
return (HashCharShortMapFactory/*<>*/) defaultFactory;
} else {
// synchronization?
return (HashCharShortMapFactory/*<>*/)
(defaultFactory = LOADER.iterator().next());
}
return (HashCharShortMapFactory/*<>*/) DefaultFactoryHolder.defaultFactory;
}

/* define ek */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@
* @see HashCharSet
*/
public final class HashCharSets {
private static final ServiceLoader<HashCharSetFactory> LOADER =
ServiceLoader.load(HashCharSetFactory.class);
private static HashCharSetFactory defaultFactory = null;

private static class DefaultFactoryHolder {
private static final HashCharSetFactory defaultFactory =
ServiceLoader.load(HashCharSetFactory.class).iterator().next();
}

/**
* Returns the default implementation of {@link HashCharSetFactory}, to which all static methods
Expand All @@ -46,13 +48,7 @@ public final class HashCharSets {
*/
@Nonnull
public static /*<>*/ HashCharSetFactory/*<>*/ getDefaultFactory() {
if (defaultFactory != null) {
return (HashCharSetFactory/*<>*/) defaultFactory;
} else {
// synchronization?
return (HashCharSetFactory/*<>*/)
(defaultFactory = LOADER.iterator().next());
}
return (HashCharSetFactory/*<>*/) DefaultFactoryHolder.defaultFactory;
}

/* define ep */
Expand Down

0 comments on commit 01f91bd

Please sign in to comment.