2012/12/8 Caldarale, Charles R <chuck.caldar...@unisys.com>: >> From: Christopher Schultz [mailto:ch...@christopherschultz.net] >> Subject: Re: svn commit: r1417194 - in >> /tomcat/trunk/java/org/apache/tomcat/util/buf: ByteChunk.java CharChunk.java > >> On 12/4/12 4:21 PM, ma...@apache.org wrote: >> > + private int hashCode=0; >> > + // did we compute the hashcode ? >> > + private boolean hasHashCode = false; > >> Should hashCode and hasHashCode be volatile? > > Yes, to insure program order is followed; otherwise there's no guarantee that > hashCode and hasHashCode are written in the required sequence.
1) I would prefer a single volatile field rather than dealing with two volatile ones. E.g. make sure that hash() never returns 0 and use that as a flag. If it is a single field, there is no need to make it volatile, likewise the buff/start/end fields are not volatile. 2) I see no need for the public "hash()" method here. The MessageBytes class can be changed to call hashCode(). >> In fact, there seems to be much more code in there than necessary. Why not: > >> > + public int hashCode() { >> > + if (hasHashCode) { >> > + return hashCode; >> > + } >> > + hashCode = hash(); >> > + hasHashCode = true; >> > + return hashCode; > > Or: > > public int hashCode() { > if (!hasHashCode) { > hashCode = hash(); > hasHashCode = true; > } > return hashCode; > } > Best regards, Konstantin Kolinko --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org