请问在设计一个事件模型的时候如何检测事件无休止的循环触发?比方下面这个例子就会不停的执行直到堆栈溢出。这算是简单的情况,如果碰到“event1-event2-event3-event4-event1-event2-event3-event4-event1-...”这样的情况那就更麻烦了。
java 代码
- public class TestCircleEvent {
-
- public static void main(String[] args) {
- EventManager.registerEvent("event1");
- EventManager.registerEvent("event2");
-
- EventManager.addListener("event1", new EventListener1());
- EventManager.addListener("event2", new EventListener2());
-
- EventManager.raiseEvent("event1", null);
- }
- }
-
- class EventListener1 implements EventListener {
-
- public void execute(EventObject object) {
- try {
- System.out.println("event1 occured.");
- Thread.sleep(1000);
- EventManager.raiseEvent("event2", null);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
-
- class EventListener2 implements EventListener {
-
- public void execute(EventObject object) {
- try {
- System.out.println("event2 occured.");
- Thread.sleep(1000);
- EventManager.raiseEvent("event1", null);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }