What you are asking for is theoretically impossible without explicit cooperation from the client thread (the one that called start()
). Just stop to think about it: the main thread is always busy running some specific code, in some specific method. The complete call stack is dedicated to the current call. Now you would like to interrupt that and start executing listener code out of the blue.
The way Swing achieves this is by running a main event loop in the Event Dispatch Thread. In this loop, you can imagine instances of Runnable
being pulled off of a queue and their run
methods called in turn. Only when one run
method returns can the next one be called.
This is what you need to design for your case as well, but I'm not sure whether you had something like that in mind. The only alternative is explicit support for listener methods being executed on a foreign thread.