title: Swing JTextArea multithreading problem - InterruptedException
body: <p>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.</p>
  
  <p>So, I created a swing GUI with JTextArea for my log and a method to log information to the log:</p>
  
  <pre><code>public synchronized void log(String text) {
      logArea.append(text);
      logArea.append("\n");
  
      if (logArea.getDocument().getLength() &gt; 50000) {
          try {
              logArea.getDocument().remove(0,5000);
          } catch (BadLocationException e) {
              log.error("Can't clean log", e);
          }
      }
  
      logArea.setCaretPosition(logArea.getDocument().getLength());
  }
  </code></pre>
  
  <p>However, the <code>setCaretPosition</code> method sometimes deadlocks on waiting some lock, and <code>append</code> sometimes throws InterruptedException.</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>I'm a total newbie in Swing, so I can not understand what am I doing wrong here? </p>
  
  <p>Thanks in advance.</p>