Re: SoftReference operation synchronization detail



Robert Klemme wrote:

Adding to that, here's what I'd do assuming that "cache" is an instance variable: make sure cache is always initialized with a SoftReference. If you do not have a referent initially just use new SoftReference(null). Then you can simplify your code to

[...]

The only downside of this is one additional object creation (the initial SoftReference) which usually only makes a difference if you have a lot of them.

And if you're working in the JRE, itself trying to use the technique on the thread class doesn't always work too well as the Reference static initialiser attempts to starts a new thread...

Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
.