优化此事件系统

时间:2015-01-13 17:21:26

标签: java events system

所以我正在重新设计我的事件系统,我希望尽可能地优化它。我把它减少到大约82毫秒才能发射100万个事件。我想问一下你有什么不同的做法,以及如何进一步优化这个?目前,当一个事件被触发到该方法时,只有一些基本的逻辑被完成,当我计划将它用于即将到来的项目时,它会更加先进,这可能会使执行时间加倍。

package me.xerces.eventbus;

import me.xerces.eventbus.annotation.EventHandler;
import me.xerces.eventbus.event.Event;

import java.lang.invoke.MethodHandle;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

public class EventManager {

    private ArrayList<Map.Entry<Class, EventHandle>> eventHandlerList = new ArrayList<Map.Entry<Class, EventHandle>>();

    public void addEventListener(Object object)
    {
        for(Method method : object.getClass().getDeclaredMethods())
        {
            if(method.isAnnotationPresent(EventHandler.class))
            {
                Map.Entry<Class, EventHandle> eventHandleEntry = new AbstractMap.SimpleEntry<Class, EventHandle>(method.getParameterTypes()[0], new EventHandle(method, object));
                method.setAccessible(true);
                eventHandlerList.add(eventHandleEntry);
            }
        }
    }

    public void addSpecificEventListener(Object object, Class eventClass)
    {
        for(Method method : object.getClass().getDeclaredMethods())
        {
            if(method.isAnnotationPresent(EventHandler.class) && method.getParameterTypes().length > 0 && method.getParameterTypes()[0].equals(eventClass))
            {
                Map.Entry<Class, EventHandle> eventHandleEntry = new AbstractMap.SimpleEntry<Class, EventHandle>(method.getParameterTypes()[0], new EventHandle(method, object));
                method.setAccessible(true);
                eventHandlerList.add(eventHandleEntry);
            }
        }
    }

    public void removeEventListener(Object object)
    {
        Iterator<Map.Entry<Class, EventHandle>> iterator = eventHandlerList.iterator();
        while(iterator.hasNext())
        {
            Map.Entry<Class, EventHandle> entry = iterator.next();
            if(entry.getValue().getMethodClass().equals(object))
                iterator.remove();
        }
    }

    public void fireEvent(Event event)
    {
        Iterator<Map.Entry<Class, EventHandle>> iterator = eventHandlerList.iterator();
        while(iterator.hasNext())
        {
            Map.Entry<Class, EventHandle> entry = iterator.next();
            if(entry.getKey().equals(event.getClass())) {
                try {
                    EventHandle eventHandle = entry.getValue();
                    eventHandle.getMethod().invoke(eventHandle.getMethodClass(), event);
                } catch (IllegalAccessException | InvocationTargetException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }
}

Github链接:https://github.com/X3rces/EventBus 编辑:当JVM将invokation转换为直接方法调用而不是使用JNI时,有没有办法加快速度?

1 个答案:

答案 0 :(得分:1)

你可以避免

 if(entry.getKey().equals(event.getClass())) {

使用

private final Map<Class<?>, List<EventHandle>> eventHandlerMap = new HashMap<>();

查看我的问题所在的EventSystem代码

EventSource with Map<Class<? extends Event>, List<EventListener<? extends Event>> without unchecked call