I have created a panel on which a set of objects is drawn. Each of the object is added as a mouse event listener to the panel. As I know, after an event occurs the listeners are notified and the code might be (or is?) executed in several threads. Is it possible to attach a custom code that will be executed after all listeners finish executing their code?
Share
As for Noel comment:
Using the
SwingUtilities.invokeLater()you are sure the code is executed after all the listener were notified. But one of these listeners may perform it’s job in a separated thread.If you need to execute your code after all the listeners not only were notified but they finished their job you could do something like this:
pseudocode:
If you have your listeners as
If all ( or some ) of your listeners perform their job in a different thread. Your
ExecuteAtTheEndmay be executing it’s code at the end of the notification, but not at the end of the execution of the listeners code.So, what you do?
You have to synchronize the usage of some lock flag and execute your code until that flag is not used.
It may be a counter that is incremented when the listener is executing and decremented when is not:
And then you just proceed until this flag is false:
Of course it is much more complicated as I put it in the pseudo-code, but it should work, if you are in this kind of circumstance.