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?
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?