JDK-6846002 : Some methods in javax.swing.text package are incorrectly marked as thread safe
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 7
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 2009-05-28
  • Updated: 2011-01-19
  • Resolved: 2010-05-18
Related Reports
Duplicate :  
Description
Some methods in JTextComponent (and probaly in some of its descendants)
have the following remark in their javadoc:

     * This method is thread safe, although most Swing methods
     * are not. Please see 
     * <A HREF="http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html">How
     * to Use Threads</A> for more information.     

Let's take the JTextComponent.setText() for example,
looking to its code it is quite obvious that it can't be tread safe

                doc.remove(0, doc.getLength());
                doc.insertString(0, t, null);

Desptite the fact that AbstractDocument correctly takes the lock for both remove() and insertString() methods, the document can be updated by another thread in the middle of the action, which will lead to an incorrect state

Surprisingly, the javadoc of JTextComponent.getText() 
doesn't have any note about thread safety, but is not thread safe either
  
Examine the following line: txt = doc.getText(0, doc.getLength()); 

if documents' length is changed during the invocation of doc.getText() method,
you may get a BadLocationException

Conclusion:
Thread safety of the AbstractDocument class doesn't mean the same for the JTextComponent class

Expected fix:
Remove all remarks about thread safety from the JTextComponent class and all its subclasses