线程池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);
}
}
}
}