Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 4765383
Votes 2
Synopsis JTextArea.append(String) not thread safe
Category java:classes_swing
Reported Against 1.3.1
Release Fixed 7(b27)
State 10-Fix Delivered, bug
Priority: 5-Very Low
Related Bugs 4760477
Submit Date 18-OCT-2002
Description




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

FULL OPERATING SYSTEM VERSION :

SunOS modeler 5.6 Generic_105181-29 sun4u sparc
SUNW,Ultra-5_10

ADDITIONAL OPERATING SYSTEMS : all, I'd think



A DESCRIPTION OF THE PROBLEM :
The Javadoc for javax.swing.JTextArea says the "append"
method is thread safe.  It is not, and the error could
result in loss of data from the document.

The implementation uses this line and discards the possible
BadLocationException:

 doc.insertString(doc.getLength(), str, null);

Alternately, we could write the same code in 2 lines, with
numbers:

1   int len=doc.getLength();
2   doc.insertString(len,str,null);

The problem occurs if line 1 executes, then some other
thread alters the length of the document, then line 2
executes.  The possible results are that the data is
inserted at some place not the end of the document, or that
an attempt is made to insert data beyond the end of the
document, resulting in a BadLocationException, which is then
discarded, thereby loosing the data.

REPRODUCIBILITY :
This bug can be reproduced occasionally.

CUSTOMER WORKAROUND :
Wrap the Document with a Document offering an append method
and synchronize the append, insert, and remove methods.
Then use the document's append method instead of
JTextArea.append.  (Note, wrapping the document on a
JTextArea with wrapped lines brings about occasional
exceptions in the event dispatch thread as noted in the bug
with Sun internal review ID 163709.)
(Review ID: 163721) 
======================================================================
Work Around
N/A
Evaluation
The root problem is that the interface to Document is not rich enough to perform reasonable operations such as append, and does not publicly expose any kind of locking/transactional/CAS functionality. It's not so much that JTextArea.append has a race condition, but it necessarily must have one.

Append requires two operation on a document: find length and insert text. What happens if the text is modified between these operations? Either the new text is inserted somewhere (that was perhaps never logically at the end of the document), or BadLocationException is thrown. These errors do occur in real applications.

A handful of methods throughout the text package claim thread-safety falsely:
JTextComponent.replaceSelection()
JTextComponent.setText()
JTextArea.append()
JTextArea.insert()
JTextArea.replaceSelection()
JEditorPane.replaceSelection()
JEditorPane.setText()
JTextPane.insertComponent()
JTextPane.insertIcon()
JTextPane.replaceSelection()
JTextPane.setCharacterAttributes()
JTextPane.setParagraphAttributes()
JTextPane.setLogicalStyle()
Posted Date : 2008-01-24 14:05:48.0
Comments
  
  Include a link with my name & email   


PLEASE NOTE: JDK6 is formerly known as Project Mustang