JDK-4411640 : NumberFormat.parse() is not thread-safe
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.text
  • Affected Version: 1.3.0
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 2001-02-05
  • Updated: 2001-06-12
  • Resolved: 2001-06-12
Related Reports
Duplicate :  
Relates :  
Description
Derived from 4101500 (JDC comments). The NumberFormat.parse method is still thread-unsafe. The following test program throws an exception.

--
import java.text.NumberFormat;

public class NumberFormatTest {

  static final NumberFormat sForm = NumberFormat.getNumberInstance();

  public static void main(String [] pArgs) {
    sForm.setMinimumFractionDigits(2);
    sForm.setMaximumFractionDigits(2);

    // do formatting in many threads:
    for (int i = 0; i < Integer.parseInt(pArgs[0]); i++) {
      Runnable r = null;

      switch (i % 3) {
      case 0:
        r = new Runnable() {
          public void run() {
              while(true) {
                try {
                  sForm.parse("10.00");
                }
                catch (Exception exc) {
		    exc.printStackTrace();
		    System.exit(1);
                }
              }
          }
        };
        break;

      case 1:
        r = new Runnable() {
          public void run() {
              while(true) {
                try {
                  sForm.parse("1.00");
                }
                catch (Exception exc) {
		    exc.printStackTrace();
		    System.exit(1);
                }
              }
          }
        };
        break;

      case 2:
        r = new Runnable() {
          public void run() {
              while(true) {
                try {
                  sForm.parse("0.00");
                }
                catch (Exception exc) {
		    exc.printStackTrace();
		    System.exit(1);
                }
              }
          }
        };
        break;

      }

      System.out.println("starting thread number " + i);
      new Thread(r).start();
    }
  }

}

--
% /usr/local/java/jdk1.3/solaris/bin/java NumberFormatTest 10
starting thread number 0
starting thread number 1
starting thread number 2
starting thread number 3
starting thread number 4
starting thread number 5
starting thread number 6
starting thread number 7
starting thread number 8
java.lang.NumberFormatException: 
starting thread number 9
        at java.lang.Long.parseLong(Long.java:331)
        at java.lang.Long.parseLong(Long.java:363)
        at java.text.DigitList.getLong(DigitList.java:153)
        at java.text.DecimalFormat.parse(DecimalFormat.java:808)
        at java.text.NumberFormat.parse(NumberFormat.java:282)
        at NumberFormatTest$2.run(NumberFormatTest.java:37)
        at java.lang.Thread.run(Thread.java:484)


Comments
EVALUATION parse() needs to be synchronized with digitList. masayoshi.okutsu@Eng 2001-02-05 It is not possible in general to make Format objects thread-safe, and so we decided under bug 4264153 to document that they aren't. norbert.lindenberg@Eng 2001-06-12
05-02-2001