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

SimpleDateFormat inexplicably not thread safe

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.4.0
    • core-libs
    • None
    • generic
    • solaris_7

      SimpleDateFormat doesn't appear to be thread safe. This fact is not
      documented, and on first blush it is inexplicable, as SimpleDateFormat's
      abstract state shouldn't be affected by its use.

      The enclosed program illustrates SimpleDateFormat formatting incorrectly when used in a threaded environment. This program also dies with array index errors occasionally.

      I will be disappointed if this problem is "fixed" by adding "synchronized" to the format method of SimpleDateFormat; I don't want my programs serialized that way.

      vedas$ cat -n Main.java
           1 import java.text.SimpleDateFormat;
           2 import java.util.Date;
           3
           4 class Main {
           5 static int n = 0;
           6
           7 static final String dateFormatString = "yyyy-MM-dd kk:mm:ss:SSS";
           8 static final SimpleDateFormat dateFormat =
           9 new SimpleDateFormat(dateFormatString);
          10
          11 private static void makethread() {
          12 Thread t = new Thread() {
          13 public void run() {
          14 for (;;) {
          15 String s = dateFormat.format(new Date());
          16 if (s.length() != dateFormatString.length()) {
          17 System.out.println(s);
          18 if (n++ > 100) System.exit(1);
          19 }
          20 try { Thread.sleep(1); }
          21 catch (InterruptedException e) {}
          22 }
          23 }
          24 };
          25 t.start();
          26 }
          27
          28 static public void main(String[] args) {
          29 makethread();
          30 makethread();
          31 makethread();
          32 makethread();
          33 makethread();
          34 makethread();
          35 makethread();
          36 makethread();
          37 makethread();
          38 makethread();
          39 try { Thread.sleep(60*60*1000); }
          40 catch (InterruptedException e) {}
          41 System.exit(0);
          42 }
          43 }
          44
      vedas$ make
      /gafter/neal-gjc/build/sparc/bin/javac Main.java
      /gafter/neal-gjc/build/sparc/bin/java Main
      2000-10-20 14:48:041:889
      2000-0010-20 14:48:41:943
      2000-10-20 14:48:41:0931
      2000-10-20 14:48:42:0088
      2000-10-20 14:048:42:130
      2000-10-20 014:48:42:300
      2000-10-20 14:48:042:310
      2000-10-20 14:048:42:350
      2000-10-20 0014:48:42:430
      2000-10-020 14:48:42:450
      2000-010-20 14:48:42:490
      2000-10-20 14:48:042:720
      2000-10-0020 14:48:42:780
      2000-10-20 14:048:42:810
      2000-10-20 14:48:42:0812
      2000-10-20 14:48:042:878
      2000-10-20 014:48:42:940
      2000-10-20 14:48:043:190
      2000-10-20 014:48:43:220
      2000-10-20 14:0048:43:359
      2000-10-20 14:48:0043:443
      2000-0010-20 14:48:43:460
      2000-10-20 0014:48:43:513
      2000-10-20 14:48:0043:618
      2000-10-20 14:48:43:0641
      2000-10-020 14:48:43:868
      2000-10-20 14:48:0043:883
      2000-0010-20 14:48:43:943
      2000-10-20 14:48:43:0943
      2000-10-20 14:0048:0043:996
      2000-10-20 14:048:44:042
      2000-10-20 14:048:44:189
      2000-10-20 14:048:44:219
      2000-10-020 14:48:44:304
      2000-10-20 14:48:44:0331
      2000-10-20 14:048:44:404
      2000-10-20 14:48:0044:444
      2000-10-20 0014:48:44:474
      2000-10-20 14:048:44:629
      2000-10-20 14:048:44:849
      2000-10-20 14:48:0044:964
      2000-010-20 14:48:45:150
      2000-10-20 14:48:045:460
      2000-10-20 14:48:045:510
      2000-10-20 14:48:45:0521
      2000-10-20 014:48:45:578
      2000-10-20 14:48:45:0578
      2000-10-0020 14:48:045:650
      2000-10-20 14:48:045:759
      2000-10-20 14:48:045:950
      2000-10-20 14:48:0046:329
      2000-10-20 14:0048:46:410
      2000-10-20 14:48:0046:579
      2000-10-20 14:0048:0046:0614
      2000-10-20 14:48:0046:663
      2000-10-20 14:48:46:0651
      2000-10-20 14:48:46:0981
      2000-10-20 14:0048:47:129
      2000-10-20 14:48:047:170
      2000-010-20 14:48:47:229
      2000-10-20 14:48:0047:273
      2000-0010-20 14:48:0047:0324
      2000-10-20 14:48:47:0401
      2000-10-20 14:48:047:460
      2000-10-20 14:0048:47:523
      2000-10-20 14:0048:47:0544
      2000-10-20 0014:48:47:649
      2000-10-20 14:48:47:0661
      2000-0010-20 14:0048:47:749
      2000-0010-20 14:48:47:777
      2000-10-20 14:0048:47:868
      2000-10-20 14:48:47:0951
      2000-10-20 0014:48:48:024
      2000-10-0020 14:48:47:054
      2000-10-0020 14:48:48:129
      2000-10-20 14:48:48:0201
      2000-10-20 14:48:48:0231
      2000-10-20 14:48:48:0341
      2000-0010-20 14:48:48:358
      2000-0010-20 14:48:48:420
      2000-10-20 14:48:0048:460
      2000-10-20 14:0048:048:510
      2000-10-20 14:0048:48:660
      2000-10-20 14:0048:48:747
      2000-10-20 14:48:48:0774
      2000-10-20 14:48:0048:980
      2000-0010-20 14:48:49:009
      2000-10-20 0014:48:48:030
      2000-10-20 0014:48:49:041
      2000-10-20 14:48:0049:249
      2000-10-20 14:48:049:369
      2000-10-20 14:48:0049:497
      2000-10-020 14:48:49:497
      2000-010-20 14:48:49:520
      2000-0010-20 14:48:49:600
      2000-10-020 14:48:49:740
      2000-10-20 14:0048:50:079
      2000-10-20 14:48:0050:190
      2000-10-20 14:48:50:0364
      2000-10-20 14:0048:50:474
      2000-10-20 14:48:0050:514
      2000-10-20 14:48:50:0551
      2000-10-20 14:48:50:0640
      *** Error code 1
      make: Fatal error: Command failed for target `all'
      vedas$

            okutsu Masayoshi Okutsu
            gafter Neal Gafter (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: