13
0
geforkt von Mirrors/Velocity

Improve native initialization logic

This uses the double-check pattern as recommended in Effective Java.
Dieser Commit ist enthalten in:
Andrew Steinborn 2018-09-17 01:03:07 -04:00
Ursprung 9c05203ce8
Commit e75ec71b32

Datei anzeigen

@ -8,7 +8,7 @@ import java.util.function.Supplier;
public class NativeCodeLoader<T> implements Supplier<T> {
private final List<Variant<T>> variants;
private Variant<T> selected;
private volatile Variant<T> selected;
public NativeCodeLoader(List<Variant<T>> variants) {
this.variants = ImmutableList.copyOf(variants);
@ -17,25 +17,32 @@ public class NativeCodeLoader<T> implements Supplier<T> {
@Override
public T get() {
if (selected == null) {
selected = select();
selected = tryLoad();
}
return selected.object;
}
private Variant<T> select() {
private Variant<T> tryLoad() {
synchronized (this) {
if (selected != null) {
return selected;
}
for (Variant<T> variant : variants) {
T got = variant.get();
if (got == null) {
continue;
}
return variant;
selected = variant;
return selected;
}
throw new IllegalArgumentException("Can't find any suitable variants");
}
}
public String getLoadedVariant() {
if (selected == null) {
selected = select();
selected = tryLoad();
}
return selected.name;
}