Is JTextArea.append(String) really thread safe?

M

markspace

So here's another thread safety question: is the
JTextArea.append(String) really thread safe? Here's the guts of the
method in question:

public void append(String str) {
Document doc = getDocument();
if (doc != null) {
try {
doc.insertString(doc.getLength(), str, null);
} catch (BadLocationException e) {
}
}
}

Note the call to getDocument() is unsynchronized. What does the
getDocument() method do? It's part of JTextComponent's API:

public Document getDocument() {
return model;
}

Things aren't looking good. Maybe field "model" is declared final or
volatile?

private Document model;

Ouch, it's just private. So, I conclude that the Swing docs lie and
append() is not thread safe. Can anyone show me where I went wrong?
 
D

Daniel Pitts

So here's another thread safety question: is the
JTextArea.append(String) really thread safe? Here's the guts of the
method in question:

public void append(String str) {
Document doc = getDocument();
if (doc != null) {
try {
doc.insertString(doc.getLength(), str, null);
} catch (BadLocationException e) {
}
}
}

Note the call to getDocument() is unsynchronized. What does the
getDocument() method do? It's part of JTextComponent's API:

public Document getDocument() {
return model;
}

Things aren't looking good. Maybe field "model" is declared final or
volatile?

private Document model;

Ouch, it's just private. So, I conclude that the Swing docs lie and
append() is not thread safe. Can anyone show me where I went wrong?
The Document object itself is thread-safe, at least with regards to
insertString see:
<http://download.oracle.com/docs/cd/...a.lang.String, javax.swing.text.AttributeSet)>
 
O

Olaf Klischat

Sorry, wrong javadoc...
The AbstractDocument class javadoc states that the method is thread safe.
<http://download.oracle.com/docs/cd/...a.lang.String, javax.swing.text.AttributeSet)>

Well, technically, somebody could write a separate implementation of
Document without extending AbstractDocument, so the thread-safety should
be part of the interface's contract. More importantly, the two calls on
the document in

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

....aren't synchronized, so I suppose it might happen that the index
returned by getLength() no longer points to the end of the document by
the time insertString() is called, resulting in either a
BadLocationException (which is eaten and results in the append()
operation doing nothing) or an insertion of the string before the end of
the document -- both cases seem to violate the contract of the append()
method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top