1

Java Swing's SwingUtilities.invokeLater() allows you to enqueue a thread to run after the last AWT event (user gesture) is processed.

Is invokeLater() guaranteed to wait until after the completion of a drag and drop gesture?

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
Daniel
  • 855
  • 10
  • 21
  • 1
    Can you elaborate on why this is important. An [sscce](http://sscce.org/) that exhibits your use case may help. – trashgod Jul 26 '12 at 22:28
  • 1
    Technically, it doesn't enqueue a `Thread`, it adds a `Runnable` to the `EventQueue`. The `EventQueue` then does an `instanceof` to determine the type of Object and if it is a `Runnable`, it calls `run` – MadProgrammer Jul 26 '12 at 23:00
  • Thanks for the input. I'm debugging timing issues in someone else's code and unfortunately it's difficult to create a concise example; I'm moreso looking for expertise and guidance here. – Daniel Aug 10 '12 at 22:40

2 Answers2

5

No, EventQueue guarantees two things for queued instances of Runnable:

  • The instances will be executed sequentially.

  • That execution will be in the same order as the instances are enqueued.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 2
    [`invokeLater`](http://docs.oracle.com/javase/7/docs/api/javax/swing/SwingUtilities.html#invokeLater(java.lang.Runnable)) also guarantees that the queued `Runnable` will be executed after all currently pending AWT events have been processed. – Jeffrey Jul 26 '12 at 23:37
  • @Jeffrey: Good point. I'm not sure about the benefit during DnD. I'd welcome a better answer. – trashgod Jul 26 '12 at 23:46
1

The way invokeLater works is that it adds your Runnable to a list of objects that will be executed in sequence on the Event Dispatch Thread. The EDT is responsible for every event that is fired in your application, including mouse events, key events, repaints, etc. In order for invokeLater to wait until the end of a Drag and Drop gesture, the DnD gesture would have to block the EDT until it was fully complete. This would also mean that your application would become completely unresponsive until the DnD gesture was finished.

So, no, invokeLater will not wait until your DnD gesture has finished.

Jeffrey
  • 44,417
  • 8
  • 90
  • 141
  • +1 for a key insight: `invokeLater()` can't predict what new `Runnable` will be posted as the gesture continues. – trashgod Jul 27 '12 at 04:38