• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

SecondaryLoop feature in Java7

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Sheriff
Posts: 22818
132
Eclipse IDE Spring Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Augusto Sellhorn
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Sheriff
Posts: 28347
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22818
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 just had the craziest dream. This tiny ad was in it.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic