views:

126

answers:

5

I know how to use SwingWorker threads, but I still don't have precise criteria to decide when to use one or not.

I/O seems obvious, but what about methods operating on potentially large collections ?

A criterion could be the actual running time, but what kind of number (in ms) would qualify ?

A: 

There is not a specific number, it is a matter of what the app is supposed to do and how responsive the gui needs to be. Best approach is to do some testing, no one can answer this for you. ( though comments may be of great use to you in determining what testing you need to do )

Nicholas Jordan
+1  A: 

For me it would be 1 s.

If your processing takes more than that, your UI will freeze. In that situation is much better to show a "busy" paint, or progress bar.

How much time would you like to wait for ANY application that you use, become responsive? Let's say you open your IDE or MS-Word or anyother. If you notice most of the times, when the application is loading, a progress bar or some other animation shows, even when the document/project/whatever is small enough as to be opened in 2 s.

OscarRyz
And remember to take into consideration those users with slower boxes than the developer's.
Software Monkey
1s is way too long. 100ms seems more appropriate.
Nemi
A: 

A long running task would be anything long enough for the user to notice glitches or delays in redrawing the UI. Setting the text of a label is probably not "long running", but just taking a few milliseconds to draw an image into an offscreen bitmap may delay the UI redrawing long enough to be noticeable.

ankon
Setting the text of a label or getting the text of a field must be done on the EDT.
Skeptic
@Skeptic - this is true for JLabels, but JTextComponent.setText() is one of the rare Swing methods that IS thread safe. See here: http://java.sun.com/javase/6/docs/api/javax/swing/text/JTextComponent.html
Nemi
@Nemi, there is always something to learn about Swing !
Skeptic
A: 

Basically if you cannot predict how long the processing will take it will be a good idea to put it in a separate thread as it will keep you applciation responsive even in extreme cases with bad data etc. Good candidates are

  • Doing expensive operations on all fields in a model.
  • Depending on an external data source or destination. You never know if that might be a slow network drive or similar.

But why not just simply make a rule, if it has a loop (any for/while) then it goes in a SwingWorker?

Thorbjørn Ravn Andersen
"loops" can be hidden deeply in object creation...
Skeptic
Naturally. Rules of thumb tend to have cornercases.
Thorbjørn Ravn Andersen
+3  A: 

The important thing is how responsive is the UI.

Jef Raskin (of Mac UI fame) said that the maximum delay should be limited to 50 ms. RISC OS guidelines said 100 ms. Button clicks are about 50 ms, so if you want to act on release you need to act fast as the user model is generally click for action. Above 140 ms, not only does it some unresponsive but UI responses appear to disconnected from user actions (see, for instance, O'Reilly's Mind Hacks).

250-350 ms and the (normal) user will think something has gone wrong.

On the other side of things, you need 8 fps (and the includes rendering) to have the illusion of animation (for instance) scrolling. And you know how gamers like their fps.

However, I prefer software that more or less works than best possible software that is not available. Having said that, having Opera lock up for a few minutes whilst it hammered the disc in the middle of this edit did not please me.

Tom Hawtin - tackline
<~100 ms is what we've always used (OS guidelines specify this, plus long experience and user feedback) - but you have to be hyper aware of how things can cause delay (something might seem really fast on your dev machine, but be considerably slower after deployment). Our in-house rule is that *anything* that isn't specifically geared towards updating the UI should go into a worker thread. If something indirectly updates the UI (i.e. update the data model which triggers a UI refresh via binding) it is also done in a background thread.
Kevin Day