JDK-6335238 : SimpleDateFormat clone() decendents corrupt each other
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.text
  • Affected Version: 1.4.2
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: linux
  • CPU: x86
  • Submitted: 2005-10-11
  • Updated: 2011-02-16
  • Resolved: 2006-02-14
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6
6 b72Fixed
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.4.2_08"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_08-b03)
Java HotSpot(TM) Client VM (build 1.4.2_08-b03, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Linux tek 2.6.8.1 #1 Fri Feb 4 17:47:37 CST 2005 i686 Intel(R) Pentium(R) 4 CPU 2.66GHz GenuineIntel GNU/Linux


A DESCRIPTION OF THE PROBLEM :
Refer to bug 4407042.

I was trying to use ThreadLocal and clone to solve the problem of thread safety with SimpleDateFormat. It did not work for me and I found this post and tried its code.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run code in bug 4407042.

ACTUAL -
java.text.ParseException: Error: 1 (2000/11/18 00:01:00) 974527260000 != 974534460000
	at SDFClone$DateParseThread.run(SDFClone.java:116)
Date Error: 1 2000/11/18 02:01:00 != 11/18/2000 00:01:00 PST

REPRODUCIBILITY :
This bug can be reproduced often.

---------- BEGIN SOURCE ----------
import java.io.*;
import java.text.*;
import java.util.*;

public class SDFClone {

    static boolean stopped_ = false;
    static final String TIME_STRING = "2000/11/18 00:01:00";
    static final String TIME_STRING_FULL = "11/18/2000 00:01:00 PST";
    static final long UTC_LONG = 974534460000L;
    static SimpleDateFormat sdf_ = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

    public SDFClone(String[] args) {
        stopped_ = false;
        DateParseThread d1 = new DateParseThread();
        DateFormatThread d2 = new DateFormatThread();
        d1.start();
        d2.start();
        try { Thread.sleep(100000); } catch (Exception e) {}
        stopped_ = true;
    }

    class DateParseThread extends Thread {
        public  void run () {
            SimpleDateFormat sdf = (SimpleDateFormat)SDFClone.sdf_.clone();
            
            try {
                int i = 0;
                while (! SDFClone.stopped_) {
                    Date date =sdf.parse(SDFClone.TIME_STRING);
                    long t = date.getTime();
                    i++;
                    if (t != SDFClone.UTC_LONG ) {
                        throw new ParseException("Error: " + i + " (" +sdf.format(date) + ") " + t + " != " + SDFClone.UTC_LONG, 0);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                System.exit(1);
            }
        }
    }
    class DateFormatThread extends Thread {
        public  void run () {
            SimpleDateFormat sdf = (SimpleDateFormat)SDFClone.sdf_.clone();
            int i = 0;
            while (! SDFClone.stopped_) {
                i++;
                String s = sdf.format(new Date(974534460000L));
                if (!s.equals(SDFClone.TIME_STRING)) {
                    System.err.println("Date Error: " + i + " " + s + " != " +SDFClone.TIME_STRING_FULL);
                    System.exit(1);
                }
            }
        }
    }

    public static void main (String[] args) {
        new SDFClone(args);
    }
}
---------- END SOURCE ----------

Comments
EVALUATION tmpBuffer in DigitList which is used by DecimalFormat needs to be initialized in clone(). This should be fixed in Mustang.
27-12-2005

EVALUATION I confirmed that the new test case fails.
14-12-2005

EVALUATION Was the test case executed in which time zone? The test case must run in U.S. Pacific Time time zone (America/Los_Angeles), and it doesn't fail on a 2-processor machine.
12-10-2005