2

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

Zoyd
  • 3,449
  • 1
  • 18
  • 27
Chimera
  • 5,884
  • 7
  • 49
  • 81

3 Answers3

5

You can push() your own EventQueue subclass, as shown here, and add your own flush() method to empty the queue. Note that getNextEvent() blocks if the queue is empty, so check peekEvent() first.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thank you for the response. Does that method apply to SWT apps as well? – Chimera Feb 08 '12 at 16:54
  • Ah, I didn't recognize `StyledTextBox` as an SWT component; I defer to you on adding the tag. AFAIK, SWT does not use the `EventQueue`. – trashgod Feb 08 '12 at 17:39
2

Use a queue for your changed text to apply, schedule events an the EDT that work the queue empty before returning. This would lead to "empty" events, once the threads finished and one further scheduled event on the EDT ran.

stryba
  • 1,979
  • 13
  • 19
  • Thank you for the response. If it's not possible to directly flush the event queue then I will need to implement a solution similar to what you suggest. – Chimera Feb 08 '12 at 01:04
1

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;
            }
        }
    });
Chimera
  • 5,884
  • 7
  • 49
  • 81