Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ConverterManager.getInstance() init thread-safe. #776

Merged
merged 2 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/main/java/org/joda/time/convert/ConverterManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,14 @@
public final class ConverterManager {

/**
* Singleton instance, lazily loaded to avoid class loading.
* Holds the singleton instance, lazily loaded to avoid class loading.
*/
private static ConverterManager INSTANCE;
private static final class LazyConverterManagerHolder {
static final ConverterManager INSTANCE = new ConverterManager();
}

public static ConverterManager getInstance() {
if (INSTANCE == null) {
INSTANCE = new ConverterManager();
}
return INSTANCE;
return LazyConverterManagerHolder.INSTANCE;
}

private ConverterSet iInstantConverters;
Expand Down
7 changes: 5 additions & 2 deletions src/test/java/org/joda/time/convert/TestConverterManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,12 @@ public void testSingleton() throws Exception {
Constructor con = cls.getDeclaredConstructor((Class[]) null);
assertEquals(1, cls.getDeclaredConstructors().length);
assertEquals(true, Modifier.isProtected(con.getModifiers()));

Class holderCls = Class.forName(cls.getName() + "$LazyConverterManagerHolder");
assertEquals(true, Modifier.isPrivate(holderCls.getModifiers()));

Field fld = cls.getDeclaredField("INSTANCE");
assertEquals(true, Modifier.isPrivate(fld.getModifiers()));
Field fld = holderCls.getDeclaredField("INSTANCE");
assertEquals(true, Modifier.isFinal(fld.getModifiers()));
}

//-----------------------------------------------------------------------
Expand Down
Loading