I'm trying to do some monitoring of the AWT Event Dispatch Thread's (EDT) health from a separate thread. If I start missing heartbeats I want to dump the EDT stack trace. The problem is the EventQueue doesn't expose a way to retrieve the current dispatch thread, the method that does is package private (probably for good reason). So I can either search through all the threads and look for a thread with a name like AWT-EventQueue-X or use an invokeLater or invokeAndWait and have my runnable save off the thread, something like:
EventQueue.invokeLater(new Runnable() {
public void run() {
eventDispatchThread = Thread.currentThread();
}
});
Then every time I go to dump the thread stack I have to first make sure the EDT I've got is still alive and if not go through the whole process again to get the thread. I'm just looking for a cleaner way to do this.
The event dispatching thread (EDT) is a background thread used in Java to process events from the Abstract Window Toolkit (AWT) graphical user interface event queue.
No, Java Swing components are not thread-safe in Java.
Swing event handling code runs on a special thread known as the event dispatch thread. Most code that invokes Swing methods also runs on this thread. This is necessary because most Swing object methods are not "thread safe": invoking them from multiple threads risks thread interference or memory consistency errors.
EventQueue is a platform-independent class that queues events, both from the underlying peer classes and from trusted application classes.
Alternatively, you can extend EventQueue
, as shown here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With