Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a clean way to retrieve the AWT event dispatch thread

Tags:

java

swing

awt

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.

like image 708
Kevin Waldron Avatar asked Feb 11 '11 21:02

Kevin Waldron


People also ask

What is event dispatch thread in Java?

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.

Is Swing thread safe in Java?

No, Java Swing components are not thread-safe in Java.

What is event driven thread EDT is Swing?

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.

What is AWT EventQueue?

EventQueue is a platform-independent class that queues events, both from the underlying peer classes and from trusted application classes.


1 Answers

Alternatively, you can extend EventQueue, as shown here.

like image 194
trashgod Avatar answered Nov 03 '22 16:11

trashgod