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

DateFormat is not threadsafe

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 1.1.4, 1.1.6, 1.2.0
    • core-libs
    • generic, x86
    • generic, windows_95, windows_nt



      Name: joT67522 Date: 11/17/97


      The DateFormat class does not appear to be
      threadsafe. It is easy to reproduce.
      Run the following code. You will get a lot of
      StringIndexOutOfBoundsExceptions.
      Please advise me if there is a design problem
      with my code. Thank you.

      Mike Freemon
      Sr. Systems Analyst
      Information Technology
      Illinois Power
      Decatur IL USA
      ###@###.###
      voice: 217-425-4197


      import java.text.*;
      import java.util.*;

      public class Main implements Runnable
      {
          private static DateFormat dateFormat;
          private static Thread thr1;
          private static Thread thr2;
          private static Thread thr3;
          private static Thread thr4;
          
      public static void main(String args[])
      {
              dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.MEDIUM);
              TimeZone tz = TimeZone.getTimeZone("CST");
              dateFormat.setTimeZone(tz);

              thr1 = new Thread(new Main());
              thr2 = new Thread(new Main());
              thr3 = new Thread(new Main());
              thr4 = new Thread(new Main());
              
              thr1.start();
              thr2.start();
              thr3.start();
              thr4.start();
              
              try
              {
                  thr1.join();
                  thr2.join();
                  thr3.join();
                  thr4.join();
              }
              catch(Exception e)
              {}
              
      } //end of method main
          
          public void run()
          {
              Date ddd = new Date(97,10,12,8,59,0);
              long start = ddd.getTime();
              long i = 0;
              while(true)
              {
                  Date dateCurrent = new Date(start + i);
                  String sss = dateFormat.format(dateCurrent); //error here
                  
                  i++;
                  if (i%100 == 0) //simple status msg
                      System.out.println("count is " + i + "; sss is " + sss);
              }
                  
          } //end of method run
          
      } //end of class Main
      (Review ID: 20178)
      ======================================================================

            joconnersunw John Oconner (Inactive)
            johsunw Joon Oh (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: