Hi, 

I'm currently evaluating Joda for use in a project (total newbie :) and
it appears to contain just what I need. 

However, I've been taking a look at the source code (version 1.4) and
came across what I think might be a potential thread safety issue in
org.joda.time.PeriodType. Maybe I've just misunderstood the
implementation - if so, I'm happy to be enlightened. :)

The various static methods which return the singleton PeriodType
instances (e.g. months(), years(), etc.) all use lazy initialization to
set the underlying static field values on first use. However, they just
use a simple guard against null to check for first use, e.g.

    private static PeriodType cMonths;

    ...

    public static PeriodType months() {
        PeriodType type = cMonths;
        if (type == null) {
            type = new PeriodType( ... );
            cMonths = type;
        }
        return type;
    }

Two threads could simultaneously call the method, both could see (type
== null) and attempt to initialise the static field. Assigning a field
value to an object reference is not guaranteed to be atomic, so I think
there's a small chance of it causing problems. As I understand it, even
using double-checked locking isn't guaranteed to work: 

http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html

Wouldn't it be safer to just initialise all the static fields in a
static initializer block, which is guaranteed to be called just once?
You'd lose the lazy initialization, but that's perhaps not such a big
deal.

Alastair.



This is an email from the CPP Group Plc, Holgate Park, York, YO26 4GA; 
telephone +44 (0)1904 544500.
This message may contain information that is confidential. If you are not the 
intended recipient,
you may not peruse, use, disseminate, distribute or copy this message. If you 
have received this
message in error, please notify the sender immediately by email, facsimile or 
telephone and either
return or destroy the original message.
The CPP Group Plc accepts no responsibility for any changes made to this 
message after it has been
sent by the original author.  This email has been scanned for all viruses by 
the MessageLabs Email Security System.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >>  http://get.splunk.com/
_______________________________________________
Joda-interest mailing list
Joda-interest@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/joda-interest

Reply via email to