0

I use SwingUtilities.invokeLater to update the UI. Sometimes tasks are executed almost at as soon as invokeLater is called, but sometimes it takes few seconds for it, that I would like to avoid. Are there any settings that can help me with this?

I know that tasks are executed in a AWT event-dispatching thread, but is there a way to forcefully clear the queue of it (probably not a great idea), or somehow add another AWT thread to work in parallel, or any other solution that may help with executing tasks faster?

Is it even possible to influence this things, or all I can do is just create some daemon threads by myself? I'd like to avoid that.

Bobby
  • 534
  • 3
  • 7
  • 21
  • No, you cannot influence the AWT event dispatching thread directly. You can minimize the amount of code in a paintComponent override or other GUI updates. – Gilbert Le Blanc Dec 01 '20 at 11:07
  • 4
    *"sometimes it takes few seconds for it"* .. why? Is the app. doing *anything* else during that time? I suspect this is a case of .. sometimes blocking the EDT. – Andrew Thompson Dec 01 '20 at 11:23
  • Use a profiler with a threads view to find out what the event handler thread does during these few seconds. I'm quite sure it isn't idle, but doing other tasks requested by your application. – Ralf Kleberhoff Dec 01 '20 at 12:10
  • https://stackoverflow.com/questions/13114097/swingutilities-invokelater-on-full-speed-thread – Mustafa Poya Dec 01 '20 at 15:35

2 Answers2

1

Have you tried SwingUtilities.invokeAndWait()? It sounds like you don't want to have the program do other things threaded with the Swing operations, you want them done right now.

Of course this won't actually caused them to be done in any fewer CPU cycles, and if there are things already queued up for the dispatch thread, this doesn't get rid of them or anything.

There are also things like loading up images from files that can either be done synchronously or asynchronously -- if you're trying to eliminate pauses, you might make sure those are being done as you want.

arcy
  • 12,845
  • 12
  • 58
  • 103
  • Thanks Arcy! I tried this and it worked, but later UI in some other cases became locked, so I probably will need to go for a more compliceted solution unfortunately. – Bobby Dec 02 '20 at 07:30
  • 1
    Sounds like another question; I cannot tell from your communication if you think this had anything to do with the "locking", or in fact what you mean by locking. If you post a question and get a response that answers that question on SO, it is (expected? traditional?) to click the check mark next to that response to mark it as "the answer". This affects ratings and so forth, I think of it as the coin used to pay for SO usefulness. – arcy Dec 02 '20 at 16:24
1

There are things that have to be done on the event handler thread. Swing isn't thread-safe, and not following that rule makes WEIRD things happen.

As there is only one such thread (and you can't create additional ones), make sure it's used only for things where it's necessary. So, don't do complex computations on that thread. Check your paintComponent() methods, invokeLater() calls etc. for time-consuming parts.

Either optimize their performance or refactor them to some other "worker" thread. You can have as many worker threads as you like, as long as they stay away from calling Swing methods. The fact that you are asking about invokeLater() implies that you're already using at least one thread besides the event handler.

A decent profiler tool will be helpful in that process.

Ralf Kleberhoff
  • 6,990
  • 1
  • 13
  • 7