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

NumberFormat.parse() is not thread-safe

XMLWordPrintable

    • generic
    • generic

      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)

            nlindenbsunw Norbert Lindenberg (Inactive)
            okutsu Masayoshi Okutsu
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: