I've recently been playing around with soft, weak and phantom reference types in Java and have been wondering if there's any uses out there for them that I haven't come across. I've used them in the past for various things and they've always fallen under the category of the following:
- Using phantom references in place of finalizers - I prefer this since there's 0 chance of a dead object being resurrected
- Using weak references in a hashmap to hold an object=>value mapping, where the mapping should only be in place if an object exists elsewhere (useful when needing to add extra information to an object in a library for instance whose source can't be modified)
- Using soft references for caching (works much better than weak references because the JVM keeps them around for much longer and only lets go if it feels it needs to.)
However, there's only 3 uses there and I daresay there's lots more things that they might be useful for which I've never come across. All suggestions welcome!