Code example for SoftReference

Methods: get

0
        if (reference == null) {
            obj = initialValue();
            mThreadLocal.set(new SoftReference<T>(obj));
            return obj;
        } else { 
            obj = reference.get();
            if (obj == null) {
                obj = initialValue();
                mThreadLocal.set(new SoftReference<T>(obj));
            } 
            return obj;
        } 
    } 
} 
 
class CanvasCache extends SoftReferenceThreadLocal<Canvas> {
    @Override 
    protected Canvas initialValue() {
        return new Canvas();
    } 
}