[concurrency-interest] BigDecimal Safe Publication

Stanimir Simeonoff stanimir at riflexo.com
Sun Aug 26 09:47:26 EDT 2012


> But is not the compiler allowed to replace the local variable h with the
> field hash, since this does not change intra-thread semantic?
>
> Imagine  val[off++] actually throws ArrayIndexOutOfBoundsExcepton, that
would lead to incorrect result, i.e. hash would be initialized while it
should have not been. Same w/ any other Error (like ThreadDeath).

Stanimir



>
> Am 26.08.2012 12:27, schrieb Per Mildner:
>
>  On Aug 20, 2012, at 7:58 PM, Zhong Yu <zhong.j.yu at gmail.com> wrote:
>>
>>  On Mon, Aug 20, 2012 at 6:49 AM, James <james at inaseq.com> wrote:
>>>
>>>> I have a system that processes a lot of Doubles.  From time to time I
>>>> need
>>>> the accuracy of BigDecimal math but creating a BigDecimal is relatively
>>>> expensive so I only do it when needed.  Hence I use lazy initialization
>>>> as
>>>> shown below.
>>>>
>>>> private Double price;  // although not final is effectively immutable
>>>> and
>>>> guaranteed non-null when used below
>>>> private transient BigDecimal priceBD;
>>>>
>>>> public BigDecimal getPriceBD() {
>>>> if (priceBD == null) {
>>>> priceBD = BigDecimal.valueOf(price);  // strict singleton semantics not
>>>> required
>>>> }
>>>> return priceBD;
>>>> }
>>>>
>>> You may have omitted it for brevity, but a local variable is necessary
>>> here for correctness. See String.hashCode()
>>>
>> Where, and why, would a local variable help?
>>
>> Also, I do not see how this relates to String.hashCode().
>>
>> What am I missing?
>>
>> For reference, String.java:
>> {
>>      ...
>>      private int hash; // Default to 0
>>      ...
>>      public int hashCode() {
>>          int h = hash;
>>          if (h == 0 && count > 0) {
>>              int off = offset;
>>              char val[] = value;
>>              int len = count;
>>
>>              for (int i = 0; i < len; i++) {
>>                  h = 31*h + val[off++];
>>              }
>>              hash = h;
>>          }
>>          return h;
>>      }
>>    ...
>> }
>>
>> Regards,
>>
>> Per Mildner
>> Per.Mildner at sics.se
>>
>>
>>
>> ______________________________**_________________
>> Concurrency-interest mailing list
>> Concurrency-interest at cs.**oswego.edu <Concurrency-interest at cs.oswego.edu>
>> http://cs.oswego.edu/mailman/**listinfo/concurrency-interest<http://cs.oswego.edu/mailman/listinfo/concurrency-interest>
>>
>
> ______________________________**_________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.**oswego.edu <Concurrency-interest at cs.oswego.edu>
> http://cs.oswego.edu/mailman/**listinfo/concurrency-interest<http://cs.oswego.edu/mailman/listinfo/concurrency-interest>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20120826/e9a4d738/attachment.html>


More information about the Concurrency-interest mailing list