An interesting subject. I was looking for EventQueue code a bit, and until I solved your problem, I might have some useful pointers:
- The implementation of the Oracle EventQueue does not preserve the size variable, therefore, unless you take full control of the EventQueue (see 3), you cannot do this better than a linear queue scan using the Oracle JRE.
- You can write your own EventQueue (maybe copy the Oracle implementation, as well as some setup ** will be the easiest) and use
EventQueue.push(EventQueue) to set your own implementation. All events in the queue will be transferred to your queue, so you can count them when they are sent to the queue. Unfortunately, this is still a linear scan, but at least now it is platform independent. - Alternatively, you can set your own implementation of EventQueue (see 2) as soon as possible after creating the original event queue (do this in the static block of code at the beginning of the class that contains your main method). Then your implementation can count all events as they are published, and you do not need to scan the queue when you want to know the size. You just need to hope that no one else pushes your own EventQueue on top of you;)
** Some tweak: I have not tried this, but I would share all the public / protected static code (everyone who refers to these methods / variables still use java.awt.EventQueue, and you can too) add a size variable and update this variable in the following four methods: postEvent(AWTEvent, int) , getNextEventPrivate() , getNextEvent(int) and removeSourceEvent(Object, boolean) .
The big problem with this modification is that EventQueue calls some AWT method calls with default visibility (e.g. Toolkit.getEventQueue() and Component.getAccessControlContext() ), which you cannot call, since your implementation will be a different package . You will need to find a workaround for each case individually.
source share