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

JTextField.setText() is not Thread safe, contray to JavaDoc

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P3 P3
    • None
    • 1.3.1
    • client-libs



      Name: sv35042 Date: 10/18/2002


      FULL PRODUCT VERSION :
      java version "1.3.1_04"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1_04-
      b02)
      Java HotSpot(TM) Client VM (build 1.3.1_04-b02, mixed mode)

      FULL OPERATING SYSTEM VERSION :

      Windows 2000 SP2

      ADDITIONAL
      OPERATING SYSTEMS :



      EXTRA RELEVANT SYSTEM CONFIGURATION :
      Don't think the OS is the problem here.

      A DESCRIPTION OF THE PROBLEM :
      JTextField.setText() which is JTextComponent's setText() is not
      Thread safe contrary to Javadoc of the setText() method.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Please run the source provided below.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      Since JavaDoc says this method is ThreadSafe, I expect to see either the
      original value or the updated value.

      Instead, sometimes you see
      "12", "21" or "". Depending on when getText() was executed during
      setText().

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import javax.swing.*;
      import javax.swing.text.*;

      public class SetTextTest {

          
      public static void main(java.lang.String[] args) {
          
              /*
               * Uncomment this block to fix
               *
              
      final JTextField jtf = new JTextField() {
                  public synchronized void setText(String text) {
                      
      super.setText(text);
                  }
                  public synchronized String getText() {
                      return
      super.getText();
                  }
              };
              */
              final JTextField jtf = new JTextField();
              
              
      jtf.setText("0");
          
              Thread t1 = new Thread() {
                  public void run() {
                      while (true) {
                          
      jtf.setText("1");
                      }
                  }
              };
              Thread t2 = new Thread() {
                  public void run() {
                      while (true)
      {
                          jtf.setText("2");
                      }
                  }
              };
              System.out.println("Start");
          
              t1.start();
              
      t2.start();
          
              long startTime = System.currentTimeMillis();
          
              long runFor = 30000;
          
              
      while (System.currentTimeMillis() < startTime + runFor) {
                  
      SwingUtilities.invokeLater(new Runnable() {
                      public void run() {
          
                          String currentText =
      jtf.getText();
          
                          if (currentText.length() != 1) {
                              System.out.println("Text: '" +
      currentText + "'");
                          }
                      }
                  });
          
              }
              System.out.println("Finished");
              
      System.exit(0);
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER WORKAROUND :
      Making get and set synchronized will solve it.

      But given that Swing is
      mostly not Thread Safe, I guess you won't change this. But please in
      JavaDoc that setText() is not Thread Safe in this sense. May be it is from a
      data integrety point of view.


              final JTextField jtf = new
      JTextField() {
                  public synchronized void setText(String text) {
                      
      super.setText(text);
                  }
                  public synchronized String getText() {
                      
      return super.getText();
                  }
              };
      (Review ID: 159960)
      ======================================================================

            idk Igor Kushnirskiy (Inactive)
            svioletsunw Scott Violet (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: