I\'m a bit confused from what I\'ve heard Java doesn\'t do events.
But I know that it does GUI events.
Am I missing something? Does java have an event handli
The Subscribe/Publish mechanism proposed by others here will let you implement synchronous events. For asynchronous event loops (fire and forget) You may want to look at "actors". An Actor consists of a handler for messages (events) of type A, as well as a threading strategy for executing the handler. This lets you do concurrent, asynchronous event handling like the following:
public class EventExample {
public static void main(String[] args) {
while ((char c = System.in.read()) >= 0) {
eventHandler.act(c);
}
}
public static Actor eventHandler =
Actor.actor(Strategy.simpleThreadStrategy(), new Effect() {
public void e(Character c) {
// Do something with c here.
}
});
}
Get the actors library here. See the Javadoc here.