From: markspace on
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?
From: Daniel Pitts on
On 7/19/2010 11:39 AM, markspace wrote:
> 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/E17476_01/javase/1.4.2/docs/api/javax/swing/text/Document.html#insertString%28int,%20java.lang.String,%20javax.swing.text.AttributeSet%29>


--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
From: Daniel Pitts on
On 7/19/2010 1:37 PM, Daniel Pitts wrote:
> On 7/19/2010 11:39 AM, markspace wrote:
>> 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/E17476_01/javase/1.4.2/docs/api/javax/swing/text/Document.html#insertString%28int,%20java.lang.String,%20javax.swing.text.AttributeSet%29>
Sorry, wrong javadoc...
The AbstractDocument class javadoc states that the method is thread safe.
<http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/javax/swing/text/AbstractDocument.html#insertString%28int,%20java.lang.String,%20javax.swing.text.AttributeSet%29>

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
From: markspace on
Daniel Pitts wrote:

> Sorry, wrong javadoc...
> The AbstractDocument class javadoc states that the method is thread safe.
> <http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/javax/swing/text/AbstractDocument.html#insertString%28int,%20java.lang.String,%20javax.swing.text.AttributeSet%29>


But the field "model" isn't....

From: Peter Duniho on
markspace wrote:
> Daniel Pitts wrote:
>
>> Sorry, wrong javadoc...
>> The AbstractDocument class javadoc states that the method is thread safe.
>> <http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/javax/swing/text/AbstractDocument.html#insertString%28int,%20java.lang.String,%20javax.swing.text.AttributeSet%29>
>
> But the field "model" isn't....

It doesn't need to be. You may get an out-of-date value for the field,
but that's not going to corrupt the data structure, because the
insertString() method is documented as thread-safe. The worst that
could happen is that the Document referenced is out-of-date, but that's
a race condition that can happen anyway.

Either you care about dealing with the race condition, or you don't.

In the former case, no amount of synchronization within the append()
method can properly address it. Even with synchronization, if you've
got one thread setting the Document and another using it, there's no
guarantees about whether the use will come before or after it's been
set. If you need guarantees, your own code has to implement those
guarantees.

And if you don't care, then obviously there's no need for the append()
method to care either.

Pete