Posts

Showing posts from November, 2012

Java Lazy Evaluation

Lazy evaluation. This is where a piece of code is written which evaluates a value (usually requiring a relatively large amount of processing), but the code isn't executed until the moment it is needed (if ever). For example, class A calls a method in class B. It passes some values (which take a long time to compute). If the values are passed lazily then class B can decide when and if to compute them. It is up to class A to implement the code for how to evaluate the value. So maybe there are 3 lazy values passed and class B decides it doesn't need to use value 1 but it does use values 2 and 3 (perhaps invoking each evaluation in separate threads, to maximise performance). The code to evaluate value 1 is never called, the code to evaluate values 2 and 3 will be called once and only once. Subsequent retrievals of a lazily evaluated value will return a cached copy of the value (unless the lazy evaluator is reset... see the code below). Here is the class: public abstract cla...