eventbus增加个缓存池

由于项目中通信大部分是通过eventbus来发送的,有些场景频繁发送消息,会造成内存上升

所以我们加了个缓冲池,类似handler的message消息

1 看下使用方法:

EventBusHelper.getInstance().post(GuideEventCache.getInstance().fillData(9,"ss"));

2 看下核心类

public abstract class EventCache {

    protected  Object[] mPoolEvents = new Object[5];
    protected  int mPoolSize =5;

    public EventCache() {
    }

    public synchronized E getEvent() {
        if (mPoolSize > 0) {
            final int poolIndex = mPoolSize - 1;
            E event = (E) mPoolEvents[poolIndex];
            mPoolEvents[poolIndex] = null;
            mPoolSize--;
            return event;
        }
        return null;
    }

    public synchronized boolean putEvent(E event) {
        if (isInPool(event)) {
            return false;
        }
        if (mPoolSize < mPoolEvents.length) {
            //清楚数据
            clearObject(event);
            mPoolEvents[mPo

你可能感兴趣的:(常用知识点分类汇总,缓存,java,android)