I see many code use SwingUtilities.invokeLater()
when not sure if it is in Event Dispatcher Thread, just to be safe. I wonder if it is worthwhile to check (either manually or use SwingUtilities.isEventDispatchThread()
) if SwingUtilities.invokeLater()
is needed before using it "blindly"?
Asked
Active
Viewed 60 times
0

Andremoniy
- 34,031
- 20
- 135
- 241

user3014901
- 367
- 2
- 4
- 15
-
4You should certainly not use it blindly, but you should also not use `isEventDispatchThread` just because you are not sure - find out beforehand if the code you are writing is running on the EDT or not, don't add this stuff "just to be sure". Programming based on guessing instead of knowing will lead to low-quality code. – Jesper Jul 28 '16 at 13:50
-
Us the approach cited [here](http://stackoverflow.com/q/7787998/230513) to find violations. – trashgod Jul 28 '16 at 16:44
-
No, you should not perform an isEventDispatchThread for every invokeLater call. There are sometimes good reasons to use invokeLater, even when you’re already running in the event dispatch thread. For instance, a MenuItem’s Action can use invokeLater to make sure the menu itself has been unposted before performing the action logic. – VGR Jul 28 '16 at 16:53