• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Java question

Munky

Diamond Member
Right now I have a piece of code that gets a list of file names in a directory like shown in the code below:

However, doing it this way causes the GUI thread to block until all the files have been listed in a certain directory. My question is - how do I get a list of files in a directory so that the list is updated one element at a time, and the GUI can display the elements one at a time instead of waiting for the whole list to be completed?
 
I'd suggest spawning it off on its own thread, and having it callback a local method to update your UI. It will result in a UI update all at once, but at least your GUI thread won't block.

Pseudo-code follows; it probably won't compile as is, but you should get the main idea.
 
Spawn a thread to get the list using a thread-safe data structure. Then in your main thread, you can check to see when there are new items in the structure and process based on that.
 
diego, doesn't you solution have two different threads accessing the gui? I don't believe that's allowed in swing (I assume that's what we're talking about).

Thyme, are you proposing to have the gui thread block on the thread-safe data structure (probably a queue)? If root.list() is the slow call, I don't think that will make any difference, as the call won't return anything until it's all done and then it'll be fast. I don't think it's possible to get a one-by-one update without using something different than root.list().
 
Originally posted by: kamper
Thyme, are you proposing to have the gui thread block on the thread-safe data structure (probably a queue)? If root.list() is the slow call, I don't think that will make any difference, as the call won't return anything until it's all done and then it'll be fast. I don't think it's possible to get a one-by-one update without using something different than root.list().

Yes, you're right. He'll need to use a different method to populate the data structure, but I don't know what there is available.
 
Originally posted by: kamper
diego, doesn't you solution have two different threads accessing the gui? I don't believe that's allowed in swing (I assume that's what we're talking about).

I think it's highly discouraged, as the GUI work should be done within the event dispatch thread. I believe it still does work, although (or at least has in some of the quick GUIs I've made).

This (Java Tutorial, Swing/Threads) should provide the OP with a starting point on doing it the correct way.
 
Ok, thanks for the tips, guys. I had a hunch that I'd have to use something instead of root.list().
 
Originally posted by: diegoalcatraz
I believe it still does work, although (or at least has in some of the quick GUIs I've made).
That's a really bad strategy when it comes to threaded code 😉 The core .net gui components actually verify that you're using the right thread and throw an exception if not. I think that's a good idea.
 
Originally posted by: kamper
Originally posted by: diegoalcatraz
I believe it still does work, although (or at least has in some of the quick GUIs I've made).
That's a really bad strategy when it comes to threaded code 😉 The core .net gui components actually verify that you're using the right thread and throw an exception if not. I think that's a good idea.

Yeah, I ran into that with C#. Unfortunately, Java doesn't come right out and fail like that, so I had assumed (incorrectly) that such an approach was allowed. It wasn't until this discussion and finding subsequent documentation that I've fully realized my mistake.
 
I actually don't see why gui libraries don't make an effort to be thread safe. Yes, they say it's for performance reasons, but I have trouble believing that the overhead is that great, given that not a lot of time gets spent actually executing gui code. At the very least they should try to provide a mechanism to allow a programmer to access the components safely from any thread, even if it requires explicit synchronization.
 
Back
Top