Two Laptop Bag*
The moose likes Swing / AWT / SWT and the fly likes SecondaryLoop feature in Java7 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "SecondaryLoop feature in Java7" Watch "SecondaryLoop feature in Java7" New topic
Author

SecondaryLoop feature in Java7

Augusto Sellhorn
Ranch Hand

Joined: May 24, 2007
Posts: 57
I updated my blog with a post about SecondaryLoop, I wonder how many Java/UI developers are aware of it;
http://sellmic.com/blog/2012/02/29/hidden-java-7-features-secondaryloop/

I remember wanting for a while to have a way to call long running tasks from my Java program without block the UI, just like the file chooser does. I'm not sure if there was a way to do this before this feature, but it sure wants to make me go back to old code and remove SwingWorkers all over the place!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

Although your code still has a few bugs* it's very informative indeed. I didn't know it existed before reading this thread.

*
- The building of the tree map can be probably be done inside the first SwingWorker, either in its doInBackground method (when not required to run on the EDT) or in its done method (when required to run on the EDT).
- You don't check the results of fileChooser.showOpenDialog(this). If the user canceled you will see "Not a directory!" on your console.
- There is a double cast to List on line 14/15 of your first code snippet.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Augusto Sellhorn
Ranch Hand

Joined: May 24, 2007
Posts: 57
Rob Spoor wrote:Although your code still has a few bugs* it's very informative indeed. I didn't know it existed before reading this thread.

*
- The building of the tree map can be probably be done inside the first SwingWorker, either in its doInBackground method (when not required to run on the EDT) or in its done method (when required to run on the EDT).
- You don't check the results of fileChooser.showOpenDialog(this). If the user canceled you will see "Not a directory!" on your console.
- There is a double cast to List on line 14/15 of your first code snippet.


Thanks for the feedback, I fixed the double cast I think that was a copy/paste error but removed it avoid confusion.

The file dialog is handled kind of weird but keeps the code simple I just wanted to write " List files = findFiles(fileChooser.getSelectedFile());" to make it look simpler. Inside findFiles (which I wasn't planning on showing in the post but had to because it has the loop) handles it and the UI doesn't do any real work (doesn't build the treemap)

There's no SwingWorker in the code, that's kind of the point of it. Yes you could do a SwingWorker and do the 2 tasks, but I like this style better, it reads simpler and in this case the treemap is a JPanel, imagine it does UI stuff before recursively building the map, it's executing first on the EDT which is what I want.

EDT work
Thread work (find files)
EDT Work update status etc
Thread work (build treemap structure)
...
EDT Work update state
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I just saw it via DZone this morning. Once I get the really ugly bugs out of my long-running Swing project (race conditions mucking up my database among others) I'm going to have a look at it. See if I can restructure my "load everything related to a sighting" process to use that and update the GUI in the right order (not the order I'm doing it now either).
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

Augusto Sellhorn wrote:There's no SwingWorker in the code, that's kind of the point of it.

I was talking about the image at the start. I guess that wasn't clear.

Yes you could do a SwingWorker and do the 2 tasks, but I like this style better, it reads simpler

I'm not sure which style I like yet. I haven't worked with SecondaryLoop yet, but I think it could indeed lead to better code. If you indeed need to do some background code, do something to the EDT, then do some background code then you would probably need to use two SwingWorkers, or use the publish-process method pair. That seems nasty. In this case it would all stick together in one method in a way that reads better. And if the calculation requires a return value you can even use an ExecutorService with a Callable and use Future.get to get the result.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SecondaryLoop feature in Java7
 
Similar Threads
Your favorite new Java 7 feature
Swatch gift
Filthy Rich Clients: Couple of questions
Clean client connection removal
Flash Java communication in Android