I am writing a game engine using LWJGL (Java and OpenGL). I have written an event bus to provide a pub-sub pattern (similar to the Guava Event Bus). I have an application class (which is a singleton) that creates the event bus. When the client would like to be notified about an event, they simply grab the event bus instance from the application class and register their class to the bus. After that, within the registered class, any method annotated with @Event and containing the correct parameter gets called during the corresponding event.
I am wondering what performance problems could possibly arise using this design, as well as multithreading problems.
For example:
public class Foo {
public Foo() {
Application.getInstance().getEventBus().register(this);
}
@Event
public void onEvent(ApplicationUpdateEvent event) {
// Called when the application updates
}
}
This design utilizes reflection to invoke the listening methods, so I'm sure performance is lost there, especially during something like the above example - called in a loop every time the application updates.
I am kind of unsure what type of multithreading problems could arise. Maybe events dispatched or handled on one thread and not another?