Created
April 17, 2015 00:03
-
-
Save jhalterman/904e8aba0f38797c4bd7 to your computer and use it in GitHub Desktop.
Double-checked lock pattern in Java 8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static <T> T getWithDcl(Supplier<T> supplier, Supplier<T> factory, Object mutex) { | |
T object = supplier.get(); | |
if (object == null) { | |
synchronized (mutex) { | |
object = supplier.get(); | |
if (object == null) { | |
object = factory.get(); | |
} | |
} | |
} | |
return object; | |
} | |
// Usage | |
Locks.getWithDcl(() -> pool.poll(), () -> factory.apply(this), pool); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment