[concurrency-interest] Cloning in CachingFactorizer in Java Concurrency in Practice

Unmesh joshi unmesh_joshi at hotmail.com
Tue Jan 22 23:09:12 EST 2008


Hi,
 
I just started reading Java Concurrency in practice. In the following CachingFactorizer example
 
BigInteger i = extractFromRequest(req);
BigInteger[] factors = null;
synchronized (this) {
++hits;
if (i.equals(lastNumber)) {
++cacheHits;
factors = lastFactors.clone();
}
}
if (factors == null) {
factors = factor(i);
synchronized (this) {
lastNumber = i;
lastFactors = factors.clone();
}
}
encodeIntoResponse(resp, factors);
}
 
factors and lastFactors are cloned in the synchronized blocks, but there is no explanation at that point why they are cloned. In the Object Sharing section, stack confinement is discussed, but using a different example. I think it will help reader if a note is added at that point saying that cloning here is for stack confinement, because assignments to factors or lastfactors is indirectly publishing the array to other thread which can be processing the array in encodeIntoResponse method.
 
Thanks,
Unmesh
_________________________________________________________________
Tried the new MSN Messenger? It’s cool! Download now.
http://messenger.msn.com/Download/Default.aspx?mkt=en-in
-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/attachments/20080123/b686d8c0/attachment.html 


More information about the Concurrency-interest mailing list