Assuming this is a "singleton" implementation: Am I guaranteed that this will only call productCatalogLoader.load() once, and also that no nullpointers can happen ? Any way to make this simpler ?
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
private ProductCatalog productCatalog;
public ProductCatalog get() {
if (this.productCatalog == null) {
reload();
}
return this.productCatalog;
}
public void reload() {
lock.writeLock().lock();
try {
if (this.productCatalog != null) return;
this.productCatalog = productCatalogLoader.load();
} finally {
lock.writeLock().unlock();
}
}
}
Edit: This was a modestly successful attempt reduce much more complex code to a simple question sample. Several people caught on to the fact that my singleton was too complex ;) (Actually there's also a quartz timer calling reload, but the "reload" implementation is a bit different IRL). I got the answer I needed anyway, broken double checked locking.