I have a simple console application that runs calculations in several threads (10-20 of them). Now I’m trying to create a simple GUI that allows me to select the file to process and prints logs from all threads.
So, I created a swing GUI with JTextArea for my log and a method to log information to the log:
public synchronized void log(String text) {
logArea.append(text);
logArea.append("\n");
if (logArea.getDocument().getLength() > 50000) {
try {
logArea.getDocument().remove(0,5000);
} catch (BadLocationException e) {
log.error("Can't clean log", e);
}
}
logArea.setCaretPosition(logArea.getDocument().getLength());
}
However, the setCaretPosition method sometimes deadlocks on waiting some lock, and append sometimes throws InterruptedException.
Exception in thread "Thread-401" java.lang.Error: Interrupted attempt to aquire write lock
at javax.swing.text.AbstractDocument.writeLock(AbstractDocument.java:1334)
at javax.swing.text.AbstractDocument.insertString(AbstractDocument.java:687)
at javax.swing.text.PlainDocument.insertString(PlainDocument.java:114)
at javax.swing.JTextArea.append(JTextArea.java:470)
at lt.quarko.aquila.scripts.ui.ScriptSessionFrame.log(ScriptSessionFrame.java:215)
I’m a total newbie in Swing, so I can not understand what am I doing wrong here?
Thanks in advance.
You don’t want to manipulate Swing objects directly from another thread, you want to post manipulations to its event queue.