JAL-4298 try much harder to avoid modifying console gui anywhere outside swing worker... patch/JAL-4298_synchronize_consoledoc
authorJames Procter <j.procter@dundee.ac.uk>
Fri, 20 Oct 2023 11:55:57 +0000 (12:55 +0100)
committerJames Procter <j.procter@dundee.ac.uk>
Fri, 20 Oct 2023 11:55:57 +0000 (12:55 +0100)
src/jalview/gui/Console.java

index 9fe7195..89380a3 100644 (file)
@@ -50,6 +50,7 @@ import javax.swing.JLabel;
 import javax.swing.JPanel;
 import javax.swing.JScrollPane;
 import javax.swing.JTextArea;
+import javax.swing.SwingUtilities;
 import javax.swing.border.Border;
 import javax.swing.text.DefaultCaret;
 
@@ -610,10 +611,10 @@ public class Console extends WindowAdapter
           try
           {
             this.wait(100);
-            if (pin.available() == 0)
-            {
-              trimBuffer(false);
-            }
+//            if (pin.available() == 0)
+//            {
+//              trimBuffer(false);
+//            }
           } catch (InterruptedException ie)
           {
           }
@@ -672,27 +673,23 @@ public class Console extends WindowAdapter
           // check string buffer - if greater than console, clear console and
           // replace with last segment of content, otherwise, append all to
           // content.
-          long count;
           while (displayPipe.length() > 0)
           {
-            count = 0;
             StringBuffer tmp = new StringBuffer(), replace;
             synchronized (displayPipe)
             {
               replace = displayPipe;
               displayPipe = tmp;
             }
-            // simply append whole buffer
-            synchronized (textArea.getDocument())
+            // Append formatted message to textarea using the Swing Thread.
+            SwingUtilities.invokeLater(new Runnable()
             {
-              textArea.append(replace.toString());
-              count += replace.length();
-              if (count > byteslim)
+              public void run()
               {
+                textArea.append(replace.toString());
                 trimBuffer(false);
               }
-            }
-            
+            });            
           }
           if (displayPipe.length() == 0)
           {
@@ -701,7 +698,14 @@ public class Console extends WindowAdapter
               this.wait(100);
               if (displayPipe.length() == 0)
               {
-                trimBuffer(false);
+                // post a trim on Swing Thread.
+                SwingUtilities.invokeLater(new Runnable()
+                {
+                  public void run()
+                  {
+                    trimBuffer(false);
+                  }
+                });
               }
             } catch (InterruptedException e)
             {
@@ -761,9 +765,7 @@ public class Console extends WindowAdapter
       @Override
       public void run()
       {
-        displayPipe.append(input); // change to stringBuffer
-        // displayPipe.flush();
-
+        displayPipe.append(input); 
       }
     });
     // stderr.println("Time taken to Spawnappend:\t" + (System.nanoTime() -