Java Reference
In-Depth Information
QUICK SUMMARY
1. Object reuse is discouraged as a general-purpose operation but may be appropriate
for small groups of objects that are expensive to initialize.
2. There are trade-offs between reusing an object via an object pool or using a
thread-local variable. In general, thread-local variables are easier to work with, as-
suming that a one-to-one correspondence between threads and reusable objects is
desired.
Weak, Soft, and Other References
Weak and soft references in Java also allow objects to be reused, though as developers, we
don't always think of it in those terms. These kinds of references—which I will generally
refer to as indefinite references—are more frequently used to cache the result of a long calcu-
lation or a database lookup rather than to reuse a simple object. For example, in the stock ser-
vlet, an indirect reference could be used to cache the result of the getHistory() method
(which entails either a lengthy calculation or a long database call). That result is just an ob-
ject, and when it is cached via an indefinite reference, we are simply reusing the object be-
cause it is otherwise expensive to initialize.
Search WWH ::




Custom Search