Search code examples
javaqueueswtevent-dispatch-threaddispatch

Need to flush the Java EDT ( event dispatch queue )


I have a Java app where many threads are writing to a StyledTextBox rapidly. At some point all the threads are terminated. However, the TextBox continues to receive text for a bit presumably because the dispatch queue was a bit backed up. Is it possible to somehow force the EDT to be flushed so that when the threads are terminated the updates to the TextBox end immediately?

Thank you, Jim


Solution

  • Ok I found the solution. The idea is to install a "verify" listener which gets called whenever text is about to be added to the StyledText. So when text is about to be added ( from the backed up dispatch queue ) the code looks for a flag to be set indicating whether or not the threads have been terminated. If the threads have been terminated then ignore the text update. This allows the control to stop being updated while allowing the event queue to be drained. The following code snippet solves the problem.

    txtOutput is the StyledText control.
    endingThreads is a boolean set to true when the threads have been terminated.
    This appears to be the easiest way to handle the issue in an SWT application.

            txtOutput.addVerifyListener(new VerifyListener() {
            public void verifyText(VerifyEvent e)
            {
                if( !endingThreads )
                {
                    e.doit = true;
                }
                else
                {
                    e.doit = false;
                }
            }
        });