EventBus 线程池 OOM 案例浅析

线程池OOM案例浅析

背景

近来公司压测,发现 EventBus 使用异步线程池时,发现出现大量线程阻塞,导致线程池不可用。

线程池执行流程

![](https://images2018.cnblogs.com/blog/1012259/201804/1012259-20180425224622974-1285062397.png)

源码分析

guava 中EventBus 异步线程池执行的方式默认是单例线程安全的,所以如果执行大批量的同类型事件将导致异步线程在订阅类处阻塞等待。阻塞等待的线程越多,线程池资源将被耗尽,导致系统 FULL GC。


package com.google.common.eventbus;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.j2objc.annotations.Weak;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.concurrent.Executor;
import org.checkerframework.checker.nullness.compatqual.NullableDecl;

class Subscriber {
    @Weak
    private EventBus bus;
    @VisibleForTesting
    final Object target;
    private final Method method;
    private final Executor executor;

    static Subscriber create(EventBus bus, Object listener, Method method) {
        // 如果没有加 @AllowConcurrentEvents 则默认单例创建有锁机制的订阅者对象
        return (Subscriber)(isDeclaredThreadSafe(method) ? new Subscriber(bus, listener, method) : new Subscriber.SynchronizedSubscriber(bus, listener, method));
    }

    private Subscriber(EventBus bus, Object target, Method method) {
        this.bus = bus;
        this.target = Preconditions.checkNotNull(target);
        this.method = method;
        method.setAccessible(true);
        this.executor = bus.executor();
    }

    final void dispatchEvent(final Object event) {
        this.executor.execute(new Runnable() {
            public void run() {
                try {
                    // 多线程事件订阅处理
                    Subscriber.this.invokeSubscriberMethod(event);
                } catch (InvocationTargetException var2) {
                    Subscriber.this.bus.handleSubscriberException(var2.getCause(), Subscriber.this.context(event));
                }

            }
        });
    }

    @VisibleForTesting
    void invokeSubscriberMethod(Object event) throws InvocationTargetException {
        try {
            // 反射执行对应方法
            this.method.invoke(this.target, Preconditions.checkNotNull(event));
        } catch (IllegalArgumentException var3) {
            throw new Error("Method rejected target/argument: " + event, var3);
        } catch (IllegalAccessException var4) {
            throw new Error("Method became inaccessible: " + event, var4);
        } catch (InvocationTargetException var5) {
            if (var5.getCause() instanceof Error) {
                throw (Error)var5.getCause();
            } else {
                throw var5;
            }
        }
    }

    private SubscriberExceptionContext context(Object event) {
        return new SubscriberExceptionContext(this.bus, event, this.target, this.method);
    }

    public final int hashCode() {
        return (31 + this.method.hashCode()) * 31 + System.identityHashCode(this.target);
    }

    public final boolean equals(@NullableDecl Object obj) {
        if (!(obj instanceof Subscriber)) {
            return false;
        } else {
            Subscriber that = (Subscriber)obj;
            return this.target == that.target && this.method.equals(that.method);
        }
    }

    private static boolean isDeclaredThreadSafe(Method method) {
        return method.getAnnotation(AllowConcurrentEvents.class) != null;
    }

    @VisibleForTesting
    static final class SynchronizedSubscriber extends Subscriber {
        private SynchronizedSubscriber(EventBus bus, Object target, Method method) {
            super(bus, target, method, null);
        }

        void invokeSubscriberMethod(Object event) throws InvocationTargetException {
            // 没有使用 @AllowConcurrentEvents 则在此处加锁
            synchronized(this) {
                super.invokeSubscriberMethod(event);
            }
        }
    }
}

转载于:https://www.cnblogs.com/kakacbing/p/8947935.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值