Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6335238

SimpleDateFormat clone() decendents corrupt each other

XMLWordPrintable

    • b72
    • x86
    • linux

      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 ----------

            peytoia Yuka Kamiya (Inactive)
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: