SOLVED: Java question - code in Arduino IDE

Hi all,

In the Serial Monitor code for the IDE (the part that displays incoming serial text on the IDE screen), I wonder what the purpose of this is:

    public void message (final String s)
    {
        SwingUtilities.invokeLater (new Runnable() {
            public void run() {

            //
            // main block of code
            //

        });
    }

With that code commented out, a large block of serial data (like 4 pages or so) comes in smoothly, but at a slow (looks like around 9600 baud) rate.

With that code in place, the same block of serial data comes in as fast pieces, but ultimately taking about the same time as when the code is commented out.

The reason I ask is that if I have a program that is "spewing" out a lot of serial data, the IDE seems like it can get overfilled or overflowed and then the whole thing (IDE and serial monitor) get locked up, requiring me to get to a command line and kill java (which kills the IDE and the serial monitor).

With that "invoke later" code removed, the serial monitor does not get clogged up. A program can spew out all the serial data it wants and the serial monitor just plugs away, printing it all until it's done.

So, anyone know what that "invoke later" code is for, what it does and do I NEED it?

Thanks!

I'm a little confused; did you remove the body of the function and replace it with "main block of code" or is that how it really looks? Can't imagine why you would remove it, if you did.

I found this in AbstractTextMonitor.java:

  public void message(final String s) {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        textArea.append(s);
        if (autoscrollBox.isSelected()) {
          textArea.setCaretPosition(textArea.getDocument().getLength());
        }
      }
    });
}

To my eye, message() takes the provided String and, rather than process it immediately on the calling thread, queues it for processing on the event dispatching thread with the runnable. Looks like it handles appending text to the (in your case: serial) text box and also checks the auto scroll checkbox before scrolling to the bottom if required.
I would have thought at least the textArea.append(s); would be required to get any serial output, no?

krupski:
So, anyone know what that "invoke later" code is for, what it does and do I NEED it?

This is an Arduino forum and that code is Java, not Arduino-oriented.

However, knowing a little Java, I will explain: Using Swing in Java requires that all Swing functions (except for a few well-documented exceptions) be executed on a particular thread, known as the Event Dispatch Thread (EDT). "invokeLater" causes the code in the "run" method to be queued and then executed on the EDT.

You need to do it if you do not want to break Swing.

vaj4088:
This is an Arduino forum and that code is Java, not Arduino-oriented.

However, knowing a little Java, I will explain: Using Swing in Java requires that all Swing functions (except for a few well-documented exceptions) be executed on a particular thread, known as the Event Dispatch Thread (EDT). "invokeLater" causes the code in the "run" method to be queued and then executed on the EDT.

You need to do it if you do not want to break Swing.

Thank you for the explanation. I had looked it up but really didn't understand what was meant. Your reply cleared it up. Thanks and a Karma++ to you.