Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread Safety of JTextArea.append

Tags:

java

swing

The java doc 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

like image 961
lgp Avatar asked Dec 08 '11 20:12

lgp


1 Answers

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.

like image 77
trashgod Avatar answered Oct 03 '22 15:10

trashgod