1)定义事件Event算法
public class MyEvent {
public MyEvent(String id, String message) {
this.id = id;
this.message = message;
}
public String id;
public String message;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
复制代码
2)注册订阅者,并实现回调方法缓存
public class MainActivity extends Activity {
private TextView message, next;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
/**
* 初始化布局组件
*/
private void initView() {
message = findViewById(R.id.message);
next = findViewById(R.id.next);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMyEvent(MyEvent event) {
message.setText(event.getMessage());
}
/**
* 初始化数据
*/
private void initData() {
EventBus.getDefault().register(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
}
复制代码
经过EventBus.getDefault().register(this);方法注册订阅者。 3)发送消息bash
public class SecondActivity extends Activity {
private TextView message;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
initView();
}
/**
* 初始化布局组件
*/
private void initView() {
message = findViewById(R.id.message);
message.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EventBus.getDefault().post(new MyEvent("1","这是从SecondActivity发送过来的消息"));
Toast.makeText(SecondActivity.this, "消息发送成功", Toast.LENGTH_SHORT).show();
}
});
}
}
复制代码
经过EventBus.getDefault().post(new MyEvent("1","这是从SecondActivity发送过来的消息"));方法发送消息 app
EventBus的使用先注册订阅者,并实现方法,而后再发送post消息,因此咱们分析源码也按照这个顺序来。 1)实例化EventBusasync
EventBus.getDefault().register(this);
复制代码
该方法首先获取EventBus实例,而后再注册,源码以下所示:ide
/** Convenience singleton for apps using a process-wide EventBus instance. */
public static EventBus getDefault() {
if (defaultInstance == null) {
synchronized (EventBus.class) {
if (defaultInstance == null) {
defaultInstance = new EventBus();
}
}
}
return defaultInstance;
}
复制代码
能够看到,实例化EventBus是个双重锁的单例模式。 函数
EventBus(EventBusBuilder builder) {
logger = builder.getLogger();
subscriptionsByEventType = new HashMap<>();
typesBySubscriber = new HashMap<>();
stickyEvents = new ConcurrentHashMap<>();
mainThreadSupport = builder.getMainThreadSupport();
mainThreadPoster = mainThreadSupport != null ? mainThreadSupport.createPoster(this) : null;
backgroundPoster = new BackgroundPoster(this);
asyncPoster = new AsyncPoster(this);
indexCount = builder.subscriberInfoIndexes != null ? builder.subscriberInfoIndexes.size() : 0;
subscriberMethodFinder = new SubscriberMethodFinder(builder.subscriberInfoIndexes,
builder.strictMethodVerification, builder.ignoreGeneratedIndex);
logSubscriberExceptions = builder.logSubscriberExceptions;
logNoSubscriberMessages = builder.logNoSubscriberMessages;
sendSubscriberExceptionEvent = builder.sendSubscriberExceptionEvent;
sendNoSubscriberEvent = builder.sendNoSubscriberEvent;
throwSubscriberException = builder.throwSubscriberException;
eventInheritance = builder.eventInheritance;
executorService = builder.executorService;
}
复制代码
能够看到,在EventBus的构造函数中,初始化了一大堆变量,这边主要关注前面两个。源码分析
subscriptionsByEventType = new HashMap<>();:key:事件类型(如:MyEvent ),value:新的订阅对象的集合,包括订阅者和订阅者包含的方法。具体赋值逻辑在后面会讲,这边先了解便可。
typesBySubscriber = new HashMap<>();:key:事件的订阅者(MainActivity ),value:事件类型的集合。订阅者跟事件类型是一对多的关系,因此一个界面能够支持多个事件类型。
复制代码
/**
* Registers the given subscriber to receive events. Subscribers must call {@link #unregister(Object)} once they
* are no longer interested in receiving events.
* <p/>
* Subscribers have event handling methods that must be annotated by {@link Subscribe}.
* The {@link Subscribe} annotation also allows configuration like {@link
* ThreadMode} and priority.
*/
public void register(Object subscriber) {
//1.拿到订阅对象的类型;
Class<?> subscriberClass = subscriber.getClass();
//2.经过findSubscriberMethods方法获取该订阅者中的全部订阅方法,由于可能包含多个订阅方法,因此返回集合。
List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);
synchronized (this) {
for (SubscriberMethod subscriberMethod : subscriberMethods) {
//3.经过subscribe方法为每一个订阅方法进行订阅。
subscribe(subscriber, subscriberMethod);
}
}
}
复制代码
List<SubscriberMethod> findSubscriberMethods(Class<?> subscriberClass) {
//先拿订阅对象在本地缓存中查找,提升性能。
List<SubscriberMethod> subscriberMethods = METHOD_CACHE.get(subscriberClass);
if (subscriberMethods != null) {
return subscriberMethods;
}
//默认状况下ignoreGeneratedIndex值是false的
if (ignoreGeneratedIndex) {
//使用反射方法拿到订阅者中的订阅方法
subscriberMethods = findUsingReflection(subscriberClass);
} else {
//使用编译期间生成的SubscriberInfo
subscriberMethods = findUsingInfo(subscriberClass);
}
//若是没有订阅方法,则抛出异常
if (subscriberMethods.isEmpty()) {
throw new EventBusException("Subscriber " + subscriberClass
+ " and its super classes have no public methods with the @Subscribe annotation");
} else {
//本地存储订阅方法,方便下次获取,提升了性能
METHOD_CACHE.put(subscriberClass, subscriberMethods);
return subscriberMethods;
}
}
复制代码
能够看到上面方法作了三件事: 1.先拿订阅对象在本地缓存中查找订阅方法 2.若是本地获取不到,则根据ignoreGeneratedIndex的值决定获取订阅方法的方式 3.本地保存订阅方法。 能够看到获取订阅的关键方法为:findUsingInfo(),那么赶忙看下他的源码,以下:布局
private List<SubscriberMethod> findUsingInfo(Class<?> subscriberClass) {
FindState findState = prepareFindState();
findState.initForSubscriber(subscriberClass);
while (findState.clazz != null) {
// 获取订阅者信息,没有配置MyEventBusIndex返回null
findState.subscriberInfo = getSubscriberInfo(findState);
if (findState.subscriberInfo != null) {
SubscriberMethod[] array = findState.subscriberInfo.getSubscriberMethods();
for (SubscriberMethod subscriberMethod : array) {
if (findState.checkAdd(subscriberMethod.method, subscriberMethod.eventType)) {
findState.subscriberMethods.add(subscriberMethod);
}
}
} else {
// 经过反射来查找订阅方法,因此为了提升性能,咱们仍是要用索引的形式使用EventBus
findUsingReflectionInSingleClass(findState);
}
findState.moveToSuperclass();
}
return getMethodsAndRelease(findState);
}
复制代码
// Must be called in synchronized block
private void subscribe(Object subscriber, SubscriberMethod subscriberMethod) {
//获取到事件类型(如:MyEvent )
Class<?> eventType = subscriberMethod.eventType;
//把订阅者和订阅者方法从新封装成新的对象
Subscription newSubscription = new Subscription(subscriber, subscriberMethod);
//事件类型为key,新的订阅者对象列表为value,存储进subscriptionsByEventType
//该集合很重要,当post消息的时候,就是从该集合中查找订阅者对象列表
CopyOnWriteArrayList<Subscription> subscriptions = subscriptionsByEventType.get(eventType);
if (subscriptions == null) {
//若是订阅者对象列表为空,则初始化出来,并加到subscriptionsByEventType中
subscriptions = new CopyOnWriteArrayList<>();
subscriptionsByEventType.put(eventType, subscriptions);
} else {
if (subscriptions.contains(newSubscription)) {
throw new EventBusException("Subscriber " + subscriber.getClass() + " already registered to event "
+ eventType);
}
}
int size = subscriptions.size();
for (int i = 0; i <= size; i++) {
// 根据订阅方法的优先级,添加到订阅列表
if (i == size || subscriberMethod.priority > subscriptions.get(i).subscriberMethod.priority) {
subscriptions.add(i, newSubscription);
break;
}
}
//订阅者为key,事件类型列表为value,存储进typesBySubscriber
List<Class<?>> subscribedEvents = typesBySubscriber.get(subscriber);
if (subscribedEvents == null) {
subscribedEvents = new ArrayList<>();
typesBySubscriber.put(subscriber, subscribedEvents);
}
subscribedEvents.add(eventType);
if (subscriberMethod.sticky) {
if (eventInheritance) {
// Existing sticky events of all subclasses of eventType have to be considered.
// Note: Iterating over all events may be inefficient with lots of sticky events,
// thus data structure should be changed to allow a more efficient lookup
// (e.g. an additional map storing sub classes of super classes: Class -> List<Class>).
Set<Map.Entry<Class<?>, Object>> entries = stickyEvents.entrySet();
for (Map.Entry<Class<?>, Object> entry : entries) {
Class<?> candidateEventType = entry.getKey();
if (eventType.isAssignableFrom(candidateEventType)) {
Object stickyEvent = entry.getValue();
checkPostStickyEventToSubscription(newSubscription, stickyEvent);
}
}
} else {
Object stickyEvent = stickyEvents.get(eventType);
checkPostStickyEventToSubscription(newSubscription, stickyEvent);
}
}
}
复制代码
能够看到上面方法作了两件事: 1.根据订阅方法的优先级,添加到订阅列表,事件类型为key,订阅列表为value,存储进subscriptionsByEventType 2.订阅者为key,事件类型列表为value,存储进typesBySubscriber post
/** Posts the given event to the event bus. */
public void post(Object event) {
PostingThreadState postingState = currentPostingThreadState.get();
List<Object> eventQueue = postingState.eventQueue;
//将当前要发送的事件加入到队列中
eventQueue.add(event);
if (!postingState.isPosting) {
postingState.isMainThread = isMainThread();
postingState.isPosting = true;
if (postingState.canceled) {
throw new EventBusException("Internal error. Abort state was not reset");
}
try {
//循环从列表中拿event
while (!eventQueue.isEmpty()) {
postSingleEvent(eventQueue.remove(0), postingState);
}
} finally {
postingState.isPosting = false;
postingState.isMainThread = false;
}
}
}
复制代码
循环从eventQueue取event,调用postSingleEvent方法:
private void postSingleEvent(Object event, PostingThreadState postingState) throws Error {
Class<?> eventClass = event.getClass();
boolean subscriptionFound = false;
if (eventInheritance) {
//查找event事件和event子类事件
List<Class<?>> eventTypes = lookupAllEventTypes(eventClass);
int countTypes = eventTypes.size();
for (int h = 0; h < countTypes; h++) {
Class<?> clazz = eventTypes.get(h);
subscriptionFound |= postSingleEventForEventType(event, postingState, clazz);
}
} else {
subscriptionFound = postSingleEventForEventType(event, postingState, eventClass);
}
if (!subscriptionFound) {
if (logNoSubscriberMessages) {
logger.log(Level.FINE, "No subscribers registered for event " + eventClass);
}
if (sendNoSubscriberEvent && eventClass != NoSubscriberEvent.class &&
eventClass != SubscriberExceptionEvent.class) {
post(new NoSubscriberEvent(this, event));
}
}
}
复制代码
遍历每一个event事件或者子类,会再调用postSingleEventForEventType:
private boolean postSingleEventForEventType(Object event, PostingThreadState postingState, Class<?> eventClass) {
CopyOnWriteArrayList<Subscription> subscriptions;
synchronized (this) {
//subscriptionsByEventType有没有很眼熟,在subscribe方法中,事件类型为key,新的订阅者对象列表为value,存储进subscriptionsByEventType
//获取到新的订阅对象列表
subscriptions = subscriptionsByEventType.get(eventClass);
}
if (subscriptions != null && !subscriptions.isEmpty()) {
//循环订阅对象列表
for (Subscription subscription : subscriptions) {
postingState.event = event;
postingState.subscription = subscription;
boolean aborted = false;
try {
postToSubscription(subscription, event, postingState.isMainThread);
aborted = postingState.canceled;
} finally {
postingState.event = null;
postingState.subscription = null;
postingState.canceled = false;
}
if (aborted) {
break;
}
}
return true;
}
return false;
}
复制代码
能够看到,该方法其实就是从subscriptionsByEventType集合中,查找该event对应的订阅者对象列表,而后遍历订阅者对象,调用postToSubscription方法。
private void postToSubscription(Subscription subscription, Object event, boolean isMainThread) {
switch (subscription.subscriberMethod.threadMode) {
case POSTING:
invokeSubscriber(subscription, event);
break;
case MAIN:
if (isMainThread) {
//经过反射,调用订阅者的方法
invokeSubscriber(subscription, event);
} else {
mainThreadPoster.enqueue(subscription, event);
}
break;
case MAIN_ORDERED:
if (mainThreadPoster != null) {
mainThreadPoster.enqueue(subscription, event);
} else {
// temporary: technically not correct as poster not decoupled from subscriber
invokeSubscriber(subscription, event);
}
break;
case BACKGROUND:
if (isMainThread) {
backgroundPoster.enqueue(subscription, event);
} else {
invokeSubscriber(subscription, event);
}
break;
case ASYNC:
asyncPoster.enqueue(subscription, event);
break;
default:
throw new IllegalStateException("Unknown thread mode: " + subscription.subscriberMethod.threadMode);
}
}
复制代码
最后经过反射调用订阅者的方法。 经过上面的分析能够看到发送消息实际上是三重循环: 1.循环从eventQueue取event,调用postSingleEvent方法 2.遍历每一个event事件或者子类,调用postSingleEventForEventType方法 3.遍历订阅者对象,调用postToSubscription方法。 这种方式的算法复杂度仍是很高的,event事件不作继承能够提升发送流程的性能。
/** Unregisters the given subscriber from all event classes. */
public synchronized void unregister(Object subscriber) {
List<Class<?>> subscribedTypes = typesBySubscriber.get(subscriber);
if (subscribedTypes != null) {
for (Class<?> eventType : subscribedTypes) {
unsubscribeByEventType(subscriber, eventType);
}
typesBySubscriber.remove(subscriber);
} else {
logger.log(Level.WARNING, "Subscriber to unregister was not registered before: " + subscriber.getClass());
}
}
复制代码
能够看到,先从typesBySubscriber集合中根据订阅者获取到事件类型列表subscribedTypes,而后循环事件类型,找出该事件类型对应的Subscriptions订阅列表,循环遍历,当Subscription对象中包含该订阅者subscriber对象时,把该Subscription对象从订阅列表中删除,再从从typesBySubscriber中删除对应的订阅者。
若有错误欢迎指出来,一块儿学习。