javaswing

Thread Safety of JTextArea.append


The Javadoc says the append method is thread-safe. However, I recall that when I tried using the append to the text area from different threads (several months ago), I get jumbled text where thread 1 would append some characters and thread 2 would append some other characters.

So instead of getting STRINGstring in the jtextarea, I get SstTrRINingG.

What differences would there be between:

  1. synchronizing the append
  2. bottlenecking appends from different threads through a threadpoolexecutor
  3. using invokeLater on the EDT

or are they all fine for fixing the issue? Thanks


Solution

  • While append() was thread safe with respect to the EDT, append() in Java 7 is not. Appends using invokeLater() will be processed in the order in which they are enqueued. A critical examination of the other approaches would require an sscce.