• Post Reply Bookmark Topic Watch Topic
  • New Topic

Many (really many) tasks in Swing thread

 
Wojciech Gorski
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
it looks like adding too many tasks using invokeLater() causes the UI to hang. It's obvious, since the next task must wait until those added before are completed. But it never happens, tasks are executing slower and slower, and the execution stops. The UI is blocked, the event dispatch thread too.
Have you ever noticed such behaviour? I did not find anything about that in Sun documentation. Is it possible, that it's some kind of a queue overflow or something?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Welcome to JavaRanch!
The most likely exlanation for your observations is a deadlock. Remember that a thread (here, there are at least your main thread and the event thread involved) may be "frozen" while it waits for a resource held by some other thread. A true deadlock happens when thread 1 holds resource A and is waiting to acquire resource B, while thread 2 holds resource B and is waiting for A. A similar situation is when one thread holds a resource and then pauses for a long or indefinite time, while a second thread is waiting for that resource.
It's very likely that the event thread is blocked waiting for some resource held by the main thread. Look carefully at any synchronized methods you're calling, and any uses of wait() and notify(), to try to find such a situation. A stack trace can help you see what's happening, too: press control-Break in the console window under Windows, or control-\ under UNIX, to force one.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How many tasks are "really many" and how long running are they? The *only* thing SwingUtilities.invokeLater() should be used for is for performing an update on the GUI from another thread. If you are doing any long running processing it will block the event thread and stop the GUI from repainting or processing any new events. As more tasks are added the actual event handling gets less and less of a "slice" of the thread.
 
Wojciech Gorski
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again,
unfortunately, the solution isn't that easy Let me explain what I'm trying to do:
I have a JTree object and a list of leaves. I want to add each leaf to the tree selection, and show the last added node (so it looks like some small gnomes or something were doing it). The code looks like this:

That code is invoked from a separate thread. It works fine for 200, 300 nodes. For about 1000 it runs slower and slower, as I described. It doesn't block completely, however. If you wait, it finishes it's job. Adding a println before calling invokeLater() shows that this thread works fine, only the event dispatch thread gets "blocked".
Any ideas? I tried adding System.sleep(n) after calling invokeLaters, to give other events a chance, but it didn't really help.
 
Wojciech Gorski
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should be Thread.sleep of course
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the actual problem is that you are creating and destroying too many of the Runnable inner class in your thread. You are probably slowing down due to allocating more and more memory and the garbage collector struggling to get rid of old references while new ones are being created.

One way to fix this is to make a single Runnable and simply set a different path each time. Since there are multiple threads involved here (the invoking thread and the event dispatching thread) you have to synchronize the run method and the method used to set the next path.

Here's an example that I got to work -

 
Wojciech Gorski
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nathan,
thanks for your help. Indeed, it's working fine. Now I'll test your solution on my tree, which is slightly bigger, about 25 000 leaves, with data dynamically downloaded from a database
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!