Partially constructed object / multithreading

I use joda due to its good reputation for multithreading. This makes great distances to make multi-threaded date processing efficient, for example, making all Date / Time / DateTime objects immutable.

But here is a situation where I’m not sure that Joda is really doing the right thing. Perhaps this is so, but I am very interested to see the explanation.

When the call toString () DateTime is called Joda does the following:

/* org.joda.time.base.AbstractInstant */
public String toString() {
    return ISODateTimeFormat.dateTime().print(this);
}

All formatting files are thread safe (they are immutable), but what about formatter-factory:

private static DateTimeFormatter dt;

/*  org.joda.time.format.ISODateTimeFormat */
public static DateTimeFormatter dateTime() {
    if (dt == null) {
        dt = new DateTimeFormatterBuilder()
            .append(date())
            .append(tTime())
            .toFormatter();
    }
    return dt;
}

This is a common pattern in single-threaded applications, but it is known to be prone to errors in a multi-threaded environment.

I see the following dangers:

  • Race condition at zero check → worst case: two objects are created.

, ( ), dt, .

  • objec

( , .)

, Joda , ?

!

+5
4

, formatters . ( ), 3- Java "Final Field Semantics". 2- JSL , .

17.5 17.5.1 JLS. " " - .

, - . w. , "" plase. f. - ( , , toFormatter) dt. a. (a) (f) " " ( ), , , f - (hb (f, a)) JLS. , ...:)

dateTime(). . . r2 ( JLS). - "this" Formatter. dateTime() dt. r1. ? r1 , dt. , a ( , ). r1 . a, mc (a, r1) ( " ", ). , r2 "" , r1. , (r1, r2) ( , JLS).

, hb (w, f). dt, hb (f, a). dt, mc (a, r1). r1 r2, (r1, r2). , hb (w, r2) hb (w, r2) JLS. , , hb (d, w), d - . , r2 w r2 ( ).

- ( , , ..).

! . . - DateTime() null. , , JLS 3- . dt , , dt " defalut". - , . 3- , " " / " ", , .

+4

,

, Joda , ?

, , , , , .

0

I Joda 2007 , , , .

3 Java Language Spec , , , 32- 64-. , , , Joda IMO (. Java.sun.com/docs/books/jls/third_edition/html/memory.html#17.7)

IIRC, 2 JLS , 32- refs , , 64- JVM, , . Java 1.4, JLS ​​v3.

0

IMO - , (, , dateTime(), ). dateTime() dt , , . , dt, null, , .

, , dateTime(). (= ) , .

-1

All Articles