* Java 11 support: lazy-loading pattern * Java 11 support: layers pattern * Java 11 support: leader-election/lazy-loading patterns
This commit is contained in:
@ -46,18 +46,18 @@ public class App {
|
||||
public static void main(String[] args) {
|
||||
|
||||
// Simple lazy loader - not thread safe
|
||||
HolderNaive holderNaive = new HolderNaive();
|
||||
Heavy heavy = holderNaive.getHeavy();
|
||||
var holderNaive = new HolderNaive();
|
||||
var heavy = holderNaive.getHeavy();
|
||||
LOGGER.info("heavy={}", heavy);
|
||||
|
||||
// Thread safe lazy loader, but with heavy synchronization on each access
|
||||
HolderThreadSafe holderThreadSafe = new HolderThreadSafe();
|
||||
Heavy another = holderThreadSafe.getHeavy();
|
||||
var holderThreadSafe = new HolderThreadSafe();
|
||||
var another = holderThreadSafe.getHeavy();
|
||||
LOGGER.info("another={}", another);
|
||||
|
||||
// The most efficient lazy loader utilizing Java 8 features
|
||||
Java8Holder java8Holder = new Java8Holder();
|
||||
Heavy next = java8Holder.getHeavy();
|
||||
var java8Holder = new Java8Holder();
|
||||
var next = java8Holder.getHeavy();
|
||||
LOGGER.info("next={}", next);
|
||||
}
|
||||
}
|
||||
|
@ -36,7 +36,7 @@ public class HolderNaiveTest extends AbstractHolderTest {
|
||||
|
||||
@Override
|
||||
Heavy getInternalHeavyValue() throws Exception {
|
||||
final Field holderField = HolderNaive.class.getDeclaredField("heavy");
|
||||
final var holderField = HolderNaive.class.getDeclaredField("heavy");
|
||||
holderField.setAccessible(true);
|
||||
return (Heavy) holderField.get(this.holder);
|
||||
}
|
||||
|
@ -36,7 +36,7 @@ public class HolderThreadSafeTest extends AbstractHolderTest {
|
||||
|
||||
@Override
|
||||
Heavy getInternalHeavyValue() throws Exception {
|
||||
final Field holderField = HolderThreadSafe.class.getDeclaredField("heavy");
|
||||
final var holderField = HolderThreadSafe.class.getDeclaredField("heavy");
|
||||
holderField.setAccessible(true);
|
||||
return (Heavy) holderField.get(this.holder);
|
||||
}
|
||||
|
@ -38,16 +38,16 @@ public class Java8HolderTest extends AbstractHolderTest {
|
||||
|
||||
@Override
|
||||
Heavy getInternalHeavyValue() throws Exception {
|
||||
final Field holderField = Java8Holder.class.getDeclaredField("heavy");
|
||||
final var holderField = Java8Holder.class.getDeclaredField("heavy");
|
||||
holderField.setAccessible(true);
|
||||
|
||||
final Supplier<Heavy> supplier = (Supplier<Heavy>) holderField.get(this.holder);
|
||||
final Class<? extends Supplier> supplierClass = supplier.getClass();
|
||||
final var supplier = (Supplier<Heavy>) holderField.get(this.holder);
|
||||
final var supplierClass = supplier.getClass();
|
||||
|
||||
// This is a little fishy, but I don't know another way to test this:
|
||||
// The lazy holder is at first a lambda, but gets replaced with a new supplier after loading ...
|
||||
if (supplierClass.isLocalClass()) {
|
||||
final Field instanceField = supplierClass.getDeclaredField("heavyInstance");
|
||||
final var instanceField = supplierClass.getDeclaredField("heavyInstance");
|
||||
instanceField.setAccessible(true);
|
||||
return (Heavy) instanceField.get(supplier);
|
||||
} else {
|
||||
|
Reference in New Issue
Block a user