system.util.invokeLater question

This is true, but not quite all the information. I would say, IMHO, you've left out the most important reason for the function.

invokeLater() waits until all of the processing and pending events on the event dispatch thread ('commonly referred to as the GUI thread) are done being processed, then invokes the supplied function on the GUI thread.

This leads to two important uses of the function

  1. As described it is useful for propertyChange events where you want to wait for the bindings to be evaluated.

  2. In an asynchronous thread this function is used to move any UI interaction back to the Event Dispatch Thread.

If the delay parameter is supplied, then the supplied function will be invoked after all currently pending events are processed plus the delay, if no delay parameter is supplied then the function is invoked immediately after all currently pending events are processed.

I'm not sure I would label it as good or bad practice, only 'probably unneeded'.

Here is a good post on the topic, it's a little dated but still very applicable.

4 Likes