Confusion over threads and events
If an object registers itself as a listener on some event , and that event occurs what thread will be used to invoke the object codes.
I am asking because I have written a GUI application which has the Main Thread, the EventDispatchThread for handling Gui work and a number of Threads created for one-off long tasks. I was expecting Objects created in the main thread to run their listener code in the main thread if an event occurs which they are listening on, but I dont think it is doing that, please explain/clarify.
You pretty much answered your own question. The EventDispatchThread (note the name!) is for handling GUI work. Your listeners are called in response to events being dispatched. Therefore all event handlers run in the EventDispatchThread.
A thread executes its own run() method and that is it, no more no less. The main thread doesn't have a run() method but simply invokes the code in main(). If main doesn't contain code to execute event-listeners then it will never execute event listeners. You can not make another thread execute some code if the thread involved does not want to execute it - when you hand a thread some work, the thread has to be prepared to receive it. The EventDispatchThread simply sits in a loop pulling events from the event queue and dispatching them (where dispatching means invoking the processXXXEvent method for the event target which in turn eventually ldeas to execution of the XXXOccurred method for each attached event listener.
Two things:
It was where the XXXOccurred method ran I was interested in, are you saying this runs on the EventDispatchThread as well
I wasnt very clear but I was actually thinking about the PropertyChangeEvent (which is not restricted to GUI) does this go on the EventDispatchThread as well.
> Two things:
> It was where the XXXOccurred method ran I was
> interested in, are you saying this runs on the
> EventDispatchThread as well
Yes, for AWT/Swing event objects.
> I wasnt very clear but I was actually thinking about
> the PropertyChangeEvent (which is not restricted to
> GUI) does this go on the EventDispatchThread as well.
Hmmm that's Java Beans stuff. I honestly don't know whether the beans architecture has a dedicated event thread, uses the GUI event thread or simply uses the current thread. I don't recall beans being an active framework so I suspect it is the current thread, ie whatever thread changes the bean will execute code that "fires" the propertychangeEvent and that executes all the listener methods.
Simple test: print out Thread.currentThread from a listener method.