Lazy Loading with Aspects
Subject:   beware of lazy loading
Date:   2004-07-07 06:45:42
From:   russellmiles
Response to: beware of lazy loading

Excellent point...

This article shows off a very simple Lazy Loading strategy. If you are deploying to a multi-threaded environment (and let's face it most production deployments of lazy loading would probably be multi-threaded) then the LazyFeatureProxy class can be made thread safe (and not prone to the problems that Java has with double checked locking) then making the getFeature() method synchronized should do the trick as shown below:

public synchronized Feature getFeature()
if (this.delegate == null)
return this.delegate =
(Feature) Class
return this.delegate;

There is a performance penalty for introducing this slight bottle neck ot the system but since the getFeature() call should only be called once in the lifetime of a lazy loaded feature (assuming the features are not unloaded at some point) then this performance penalty should not be too intrusive.

Cheers for the feedback!