{"id":111,"date":"2022-03-07T10:24:52","date_gmt":"2022-03-07T10:24:52","guid":{"rendered":"http:\/\/www.devblog.cn\/?p=111"},"modified":"2022-03-08T10:21:41","modified_gmt":"2022-03-08T10:21:41","slug":"%e4%b8%89%e6%96%b9%e5%ba%93-eventbus%e6%ba%90%e7%a0%81%e8%a7%a3%e6%9e%90%ef%bc%88%e4%ba%8c%ef%bc%89","status":"publish","type":"post","link":"http:\/\/www.devblog.cn\/?p=111","title":{"rendered":"\u4e09\u65b9\u5e93-EventBus\u6e90\u7801\u89e3\u6790\uff08\u4e8c\uff09"},"content":{"rendered":"<h1>EventBus\u6e90\u7801\u89e3\u6790\uff08\u4e8c\uff09<\/h1>\n<blockquote>\n<p>\u6e90\u7801\u7248\u672c\uff1a<\/p>\n<ul>\n<li><strong>EventBus<\/strong>\uff1a3.3.1<\/li>\n<\/ul>\n<\/blockquote>\n<h2>EventBus\u7684post<\/h2>\n<p><strong>EventBus &#8211;&gt; post()<\/strong><\/p>\n<pre><code class=\"language-java\">public void post(Object event) {\n    \/\/ 1\u3001\u83b7\u53d6\u5f53\u524d\u7ebf\u7a0b\u7684PostingThreadState\uff0ccurrentPostingThreadState\u662f\u4e00\u4e2aThreadLocal\u5bf9\u8c61\uff0c\u4fdd\u8bc1\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u6709\u4e00\u4e2aPostingThreadState\u5bf9\u8c61\uff0c\u4fdd\u8bc1\u4e86\u7ebf\u7a0b\u5b89\u5168\u3002\n    PostingThreadState postingState = currentPostingThreadState.get();\n    \/\/ 2\u3001\u83b7\u53d6\u5f53\u524d\u7ebf\u7a0b\u7684\u4e8b\u4ef6\u961f\u5217\n    List&lt;Object&gt; eventQueue = postingState.eventQueue;\n    \/\/ 3\u3001\u5c06\u8981\u53d1\u9001\u7684\u4e8b\u4ef6\u52a0\u5165\u5230\u5f53\u524d\u7ebf\u7a0b\u7684\u4e8b\u4ef6\u961f\u5217\u4e2d\n    eventQueue.add(event);\n\n    \/\/ 4\u3001\u5224\u65ad\u662f\u5426\u6b63\u5728\u53d1\u9001\u4e8b\u4ef6\uff0c\u5982\u679c\u6b63\u5728\u53d1\u9001\u4e2d\uff0c\u5219\u4e0d\u518d\u6267\u884c\u91cc\u9762\u7684\u903b\u8f91\uff0c\u56e0\u4e3a\u4e8b\u4ef6\u5df2\u7ecf\u6dfb\u52a0\u5230eventQueue\u4e2d\uff0c\u5b83\u4f1a\u7ee7\u7eed\u8d70\u91cc\u9762while\u5faa\u73af\u7684\u5224\u65ad\u903b\u8f91\u3002\n    if (!postingState.isPosting) {\n        \/\/ 5\u3001\u4e8b\u4ef6\u672a\u88ab\u53d1\u9001\u4e2d\uff0c\u5219\u521d\u59cb\u5316PostingThreadState\uff0c\u904d\u5386\u4e8b\u4ef6\u961f\u5217\u53d1\u9001\u3002\n        \/\/ 6\u3001\u5224\u65ad\u5f53\u524d\u7ebf\u7a0b\u662f\u5426\u662f\u4e3b\u7ebf\u7a0b\n        postingState.isMainThread = isMainThread();\n        \/\/ 7\u3001\u6807\u8bb0\u5f53\u524d\u7ebf\u7a0b\u6b63\u5728\u53d1\u9001\u4e2d\n        postingState.isPosting = true;\n        \/\/ 8\u3001\u5982\u679c\u5f53\u524d\u7ebf\u7a0b\u5df2\u7ecf\u53d6\u6d88\uff0c\u5219\u629b\u51fa\u5f02\u5e38\u3002\n        if (postingState.canceled) {\n            throw new EventBusException(&quot;Internal error. Abort state was not reset&quot;);\n        }\n        try {eventQueue.remove(0)\n            \/\/ 9\u3001\u904d\u5386\u4e8b\u4ef6\u961f\u5217\uff0c\u53ea\u8981\u6709\u4e8b\u4ef6\uff0c\u5c31\u4e00\u76f4\u53d1\u9001\u3002\n            while (!eventQueue.isEmpty()) {\n                \/\/ 10\u3001\u53d1\u9001\u4e8b\u4ef6\uff0ceventQueue.remove(0)\u8bf4\u660epost\u8fdb\u6765\u7684\u4e8b\u4ef6\uff0c\u4f1a\u4f9d\u6b21\u53d1\u9001\u3002\n                postSingleEvent(eventQueue.remove(0), postingState);\n            }\n        } finally {\n            \/\/ 11\u3001\u5f53\u524d\u7ebf\u7a0b\u7684\u6240\u6709\u4e8b\u4ef6\u5b8c\u6210\uff0c\u8fd8\u539f\u72b6\u6001\u3002\n            postingState.isPosting = false;\n            postingState.isMainThread = false;\n        }\n    }\n}<\/code><\/pre>\n<p><code>post()<\/code>\u65b9\u6cd5\uff0c\u4e3a\u4e8b\u4ef6\u7684\u53d1\u9001\uff0c\u517c\u5bb9\u4e86<strong>\u591a\u7ebf\u7a0b\u591a\u53d1\u9001\u591a\u4e8b\u4ef6<\/strong>\uff08\u591a\u4e8b\u4ef6\u540e\u9762\u4ecb\u7ecd\uff09\u7684\u60c5\u51b5\u3002\u5b83\u4f7f\u7528<code>ThreadLocal<\/code>\u5bf9\u8c61\uff0c\u4fdd\u8bc1\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u6709\u4e00\u4e2a<code>PostingThreadState<\/code>\u5bf9\u8c61\uff0c\u4fdd\u8bc1\u4e86\u7ebf\u7a0b\u5b89\u5168\uff0c\u5b83\u901a\u8fc7<code>postingState.isMainThread<\/code>\u8bb0\u5f55\u4e86<strong>\u53d1\u9001\u7ebf\u7a0b<\/strong>\u662f\u5426\u662f<strong>\u4e3b\u7ebf\u7a0b<\/strong>\uff0c\u7136\u540e\u5b83\u4f1a<strong>\u4f9d\u6b21<\/strong>\u8fdb\u884c\u53d1\u9001\u3002<\/p>\n<blockquote>\n<p>\u8bf4\u660e\uff1a<\/p>\n<ol>\n<li><code>postingState.isMainThread<\/code>\uff0c\u8bb0\u5f55\u4e86<strong>\u53d1\u9001\u7ebf\u7a0b<\/strong>\u662f\u5426\u662f<strong>\u4e3b\u7ebf\u7a0b<\/strong>\u3002<\/li>\n<li>\u6b65\u9aa4<strong>10<\/strong>\uff0c<code>eventQueue.remove(0)<\/code>\uff0c\u8bf4\u660e<code>post()<\/code>\u8fdb\u6765\u7684\u4e8b\u4ef6\uff0c\u4f1a<strong>\u4f9d\u6b21\u53d1\u9001<\/strong>\u3002<\/li>\n<\/ol>\n<\/blockquote>\n<p>\u6211\u4eec\u518d\u6765\u770b\u4e00\u4e0b<code>currentPostingThreadState<\/code>\u5c5e\u6027\u548c<code>PostingThreadState<\/code>\u7c7b\u3002<\/p>\n<pre><code class=\"language-java\">private final ThreadLocal&lt;PostingThreadState&gt; currentPostingThreadState = new ThreadLocal&lt;PostingThreadState&gt;() {\n    @Override\n    protected PostingThreadState initialValue() {\n        return new PostingThreadState();\n    }\n};\n\nfinal static class PostingThreadState {\n    final List&lt;Object&gt; eventQueue = new ArrayList&lt;&gt;(); \/\/ \u53d1\u9001\u7ebf\u7a0b\u7684\u4e8b\u4ef6\u961f\u5217\n    boolean isPosting; \/\/ \u53d1\u9001\u7ebf\u7a0b\u662f\u5426\u6b63\u5728\u53d1\u9001\u4e2d\n    boolean isMainThread; \/\/ \u53d1\u9001\u7ebf\u7a0b\u662f\u5426\u5728\u4e3b\u7ebf\u7a0b\n    Subscription subscription; \/\/ \u53d1\u9001\u7ebf\u7a0b\u6b63\u5728\u53d1\u5e03\u7684Subscription\n    Object event; \/\/ \u53d1\u9001\u7ebf\u7a0b\u6b63\u5728\u53d1\u5e03\u7684Event\n    boolean canceled; \/\/ \u53d1\u9001\u7ebf\u7a0b\u662f\u5426\u5df2\u53d6\u6d88\n}<\/code><\/pre>\n<p><code>PostingThreadState<\/code>\u7c7b\u4e3a<strong>\u53d1\u9001\u7ebf\u7a0b<\/strong>\u7684<strong>\u72b6\u6001\u7c7b<\/strong>\uff0c\u72b6\u6001\u4fe1\u606f\u770b\u4e0a\u9762\u6ce8\u91ca\u3002<code>currentPostingThreadState<\/code>\u662f\u4e00\u4e2a<code>ThreadLocal<\/code>\u5bf9\u8c61\uff0c\u4fdd\u8bc1<strong>\u6bcf\u4e2a\u7ebf\u7a0b<\/strong>\u90fd\u6709\u4e00\u4e2a<code>PostingThreadState<\/code>\u5bf9\u8c61\uff0c\u4fdd\u8bc1\u4e86<strong>\u7ebf\u7a0b\u5b89\u5168<\/strong>\u3002<\/p>\n<p><strong>EventBus &#8211;&gt; postSingleEvent<\/strong><\/p>\n<pre><code class=\"language-java\">private void postSingleEvent(Object event, PostingThreadState postingState) throws Error {\n    \/\/ 11\u3001\u83b7\u53d6\u53d1\u9001\u4e8b\u4ef6\u7684Class\n    Class&lt;?&gt; eventClass = event.getClass();\n    \/\/ 12\u3001\u8bb0\u5f55\u662f\u5426\u627e\u5230Subscription\n    boolean subscriptionFound = false;\n    if (eventInheritance) { \/\/ eventInheritance\uff1a\u4e8b\u4ef6\u662f\u5426\u6709\u7ee7\u627f\u6027\uff0c\u9ed8\u8ba4\u4e3atrue\u3002\n        \/\/ 13\u3001\u4e8b\u4ef6\u6709\u7ee7\u627f\u6027\uff0c\u5219\u53d1\u9001[\u53d1\u9001Event\u7684\u6240\u6709\u7236\u7c7b\u4ee5\u53ca\u6240\u6709\u7236\u63a5\u53e3]\u7684Event\u3002\n        \/\/ 14\u3001\u83b7\u53d6\u5230[\u53d1\u9001Event\u7684\u6240\u6709\u7236\u7c7b\u4ee5\u53ca\u6240\u6709\u7236\u63a5\u53e3]\u3002\n        List&lt;Class&lt;?&gt;&gt; eventTypes = lookupAllEventTypes(eventClass);\n        \/\/ 15\u3001\u904d\u5386\u96c6\u5408\uff0c\u53d1\u9001\u5355\u4e2a\u4e8b\u4ef6\u3002\n        int countTypes = eventTypes.size();\n        for (int h = 0; h &lt; countTypes; h++) {\n            Class&lt;?&gt; clazz = eventTypes.get(h);\n            \/\/ 16\u3001\u53d1\u9001\u5355\u4e2a\u4e8b\u4ef6\uff0c\u5e76\u5224\u65ad\u662f\u5426\u627e\u5230Subscription\uff08\u53ea\u8981\u6709\u4e00\u4e2a\u6709\uff0c\u5c31\u4ee3\u8868\u627e\u5230\uff09\u3002\n            subscriptionFound |= postSingleEventForEventType(event, postingState, clazz);\n        }\n    } else {\n        \/\/ 17\u3001\u4e8b\u4ef6\u6ca1\u6709\u7ee7\u627f\u6027\uff0c\u5219\u53d1\u9001[\u53d1\u9001Event]\u7684Event\u3002\n        subscriptionFound = postSingleEventForEventType(event, postingState, eventClass);\n    }\n    if (!subscriptionFound) {\n        \/\/ 18\u3001\u6ca1\u6709\u627e\u5230\uff0c\u5224\u65ad\u662f\u5426\u6253\u5370Log\uff0c\u6216\u8005\u53d1\u9001NoSubscriberEvent\u3002\n        if (logNoSubscriberMessages) {\n            \/\/ 19\u3001\u6ca1\u6709\u8ba2\u9605\u8005\u65f6\uff0c\u6253\u5370\u5f02\u5e38\u4fe1\u606f\u3002\n            logger.log(Level.FINE, &quot;No subscribers registered for event &quot; + eventClass);\n        }\n        if (sendNoSubscriberEvent &amp;&amp; eventClass != NoSubscriberEvent.class &amp;&amp;\n                eventClass != SubscriberExceptionEvent.class) {\n            \/\/ 20\u3001\u6ca1\u6709\u8ba2\u9605\u8005\u65f6\uff0c\u53d1\u9001NoSubscriberEvent\u4e8b\u4ef6\u3002\n            post(new NoSubscriberEvent(this, event));\n        }\n    }\n}<\/code><\/pre>\n<p><code>postSingleEvent()<\/code>\u65b9\u6cd5\uff0c\u4e3a\u4e8b\u4ef6\u7684\u53d1\u9001\uff0c\u517c\u5bb9\u4e86<strong>\u591a\u7ebf\u7a0b\u5355\u53d1\u9001\u591a\u4e8b\u4ef6<\/strong>\u7684\u60c5\u51b5\u3002\u5982\u679c<strong>\u4e8b\u4ef6\u6709\u7ee7\u627f\u6027<\/strong>\uff0c\u5219\u53d1\u9001[<strong>\u53d1\u9001Event\u7684\u6240\u6709\u7236\u7c7b\u4ee5\u53ca\u6240\u6709\u7236\u63a5\u53e3<\/strong>]\u7684<code>Event<\/code>\uff08\u6b64\u4e3a\u591a\u4e8b\u4ef6\uff09\uff0c\u5426\u5219\u5219\u53d1\u9001[<strong>\u53d1\u9001Event<\/strong>]\u7684<code>Event<\/code>\uff0c\u5e76\u5224\u65ad\u6ca1\u6709\u627e\u5230\uff0c\u662f\u5426\u6253\u5370<code>Log<\/code>\uff0c\u6216\u8005\u53d1\u9001<code>NoSubscriberEvent<\/code>\u3002<\/p>\n<p>\u6211\u4eec\u518d\u6765\u770b\u4e00\u4e0b<code>lookupAllEventTypes<\/code>\u65b9\u6cd5\u3002<br \/>\n<strong>EventBus &#8211;&gt; lookupAllEventTypes<\/strong><\/p>\n<pre><code class=\"language-java\">private static List&lt;Class&lt;?&gt;&gt; lookupAllEventTypes(Class&lt;?&gt; eventClass) {\n    \/\/ \u4f7f\u7528\u4e86\u540c\u6b65\u5e76\u4e14\u9501\u552f\u4e00\uff0c\u4fdd\u8bc1\u4e86\u7ebf\u7a0b\u6709\u5e8f\u6267\u884c\u3002\n    synchronized (eventTypesCache) {\n        \/\/ \u4ece\u7f13\u5b58\u4e2d\u83b7\u53d6\u5f53\u524dEvent\u7684\u5df2\u7ef4\u62a4\u7684\u6240\u6709\u4e8b\u4ef6\uff08\u7236\u7c7b\u3001\u7236\u63a5\u53e3\uff09\u96c6\u5408\u3002\n        List&lt;Class&lt;?&gt;&gt; eventTypes = eventTypesCache.get(eventClass);\n        if (eventTypes == null) {\n            \/\/ \u96c6\u5408\u4e3a\u7a7a\uff0c\u8bf4\u660e\u662f\u7b2c\u4e00\u6b21\u83b7\u53d6\uff0c\u5219\u521b\u5efa\u96c6\u5408\uff0c\u5e76\u7ef4\u62a4\u96c6\u5408\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u7f13\u5b58\u4e2d\u3002\n            eventTypes = new ArrayList&lt;&gt;();\n            \/\/ \u5f53\u524dclazz\u9ed8\u8ba4\u4e3aeventClass\n            Class&lt;?&gt; clazz = eventClass;\n            \/\/ \u904d\u5386\u5f53\u524dclazz\uff0c\u53ea\u8981\u4e0d\u4e3a\u7a7a\uff0c\u5c31\u7ee7\u7eed\u904d\u5386\u3002\n            while (clazz != null) {\n                \/\/ \u6dfb\u52a0\u5f53\u524dclazz\uff0c\u7b2c\u4e00\u6b21\u4f1a\u628aeventClass\u6dfb\u52a0\u8fdb\u53bb\u3002\n                eventTypes.add(clazz);\n                \/\/ \u6dfb\u52a0\u5f53\u524dclazz\u7684\u6240\u6709\u63a5\u53e3class\n                addInterfaces(eventTypes, clazz.getInterfaces());\n                \/\/ \u83b7\u53d6\u5f53\u524dclazz\u7684\u7236\u7c7b\uff0c\u4ee5\u7ee7\u7eed\u6dfb\u52a0\u3002\n                clazz = clazz.getSuperclass();\n            }\n            \/\/ \u5c06\u7ef4\u62a4\u597d\u7684\u96c6\u5408\uff0c\u6dfb\u52a0\u5230\u7f13\u5b58\u3002\n            eventTypesCache.put(eventClass, eventTypes);\n        }\n        \/\/ \u6700\u540e\u8fd4\u56de\u6b64\u7ef4\u62a4\u597d\u7684\u6240\u6709\u4e8b\u4ef6\uff08\u7236\u7c7b\u3001\u7236\u63a5\u53e3\uff09\u96c6\u5408\u3002\n        return eventTypes;\n    }\n}\n\n\/\/ \u5faa\u73af\u9012\u5f52\u6dfb\u52a0\u5f53\u524d\u7c7b\u7684\u6240\u6709\u7684\u63a5\u53e3class\u5230eventTypes\u3002\n\/\/ \u4e3e\u4f8b\uff1a\u5f53\u524d\u7c7b\uff0c\u5b9e\u73b0A\u3001B\u4e24\u4e2a\u63a5\u53e3\uff0cA\u5b9e\u73b0C\u3001D\u63a5\u53e3\uff0cB\u5b9e\u73b0E\u63a5\u53e3\uff0cC\u3001D\u3001E\u672a\u5b9e\u73b0\u4efb\u4f55\u63a5\u53e3\uff0c\u6700\u7ec8\u4f1a\u628aA\u3001B\u3001C\u3001D\u3001E\u63a5\u53e3\u5168\u90e8\u6dfb\u52a0\u5230eventTypes\u4e2d\u3002\nstatic void addInterfaces(List&lt;Class&lt;?&gt;&gt; eventTypes, Class&lt;?&gt;[] interfaces) {\n    \/\/ \u904d\u5386\u6240\u6709\u63a5\u53e3\n    for (Class&lt;?&gt; interfaceClass : interfaces) {\n        if (!eventTypes.contains(interfaceClass)) {\n            \/\/ \u6b64\u63a5\u53e3\u672a\u88ab\u5305\u542b\uff0c\u5219\u8fdb\u884c\u6dfb\u52a0\u3002\n            eventTypes.add(interfaceClass);\n            \/\/ \u7ee7\u7eed\u9012\u5f52\u589e\u52a0\u7236\u63a5\u53e3\u7684\u6240\u6709\u63a5\u53e3\uff0c\u4f7f\u7528\u9012\u5f52\u4ee5\u5b8c\u6210\u5176\u6240\u6709\u7236\u63a5\u53e3\u3002\n            addInterfaces(eventTypes, interfaceClass.getInterfaces());\n        }\n    }\n}<\/code><\/pre>\n<p><code>lookupAllEventTypes()<\/code>\u65b9\u6cd5\uff0c\u4e3a\u83b7\u53d6\u5230[<strong>\u53d1\u9001Event\u7684\u6240\u6709\u7236\u7c7b\u4ee5\u53ca\u6240\u6709\u7236\u63a5\u53e3<\/strong>]\u3002<\/p>\n<p><strong>EventBus &#8211;&gt; postSingleEventForEventType<\/strong><\/p>\n<pre><code class=\"language-java\">private boolean postSingleEventForEventType(Object event, PostingThreadState postingState, Class&lt;?&gt; eventClass) {\n    CopyOnWriteArrayList&lt;Subscription&gt; subscriptions;\n    synchronized (this) {\n        \/\/ 21\u3001\u6839\u636e\u4e8b\u4ef6\u83b7\u53d6\u6240\u6709\u8ba2\u9605\u5b83\u7684\u8ba2\u9605\u8005\uff0c\u5728\u540c\u6b65\u65b9\u6cd5\u5185\u6267\u884c\uff0c\u4fdd\u8bc1\u4e86HashMap\u7684get\u65b9\u6cd5\u7ebf\u7a0b\u5b89\u5168\u3002\n        subscriptions = subscriptionsByEventType.get(eventClass);\n    }\n    if (subscriptions != null &amp;&amp; !subscriptions.isEmpty()) {\n        \/\/ 22\u3001\u6709\u8ba2\u9605\u8005\uff0c\u904d\u5386\u96c6\u5408\u8fdb\u884c\u53d1\u9001\uff0c\u8fd4\u56detrue\uff08\u4ee3\u8868\u5df2\u7ecf\u627e\u5230\uff09\u3002\n        \/\/ 23\u3001\u904d\u5386\u6240\u6709\u6b64\u7c7b\u578b\u7684\u8ba2\u9605\u8005\u8fdb\u884c\u53d1\u9001\n        for (Subscription subscription : subscriptions) {\n            \/\/ 24\u3001\u8bb0\u5f55\u53d1\u9001\u7ebf\u7a0b\u6b63\u5728\u53d1\u5e03\u7684Event\n            postingState.event = event;\n            \/\/ 25\u3001\u8bb0\u5f55\u53d1\u9001\u7ebf\u7a0b\u6b63\u5728\u53d1\u5e03\u7684Subscription\n            postingState.subscription = subscription;\n            \/\/ 26\u3001\u662f\u5426\u662f\u4e2d\u65ad\u7684\n            boolean aborted;\n            try {\n                \/\/ 27\u3001\u5c06\u4e8b\u4ef6\u53d1\u9001\u7ed9\u8ba2\u9605\u8005\uff0cpostingState.isMainThread\u4e3apost\u65b9\u6cd5\u8bb0\u5f55\u7684\u3002\n                postToSubscription(subscription, event, postingState.isMainThread);\n                \/\/ 28\u3001\u4e8b\u4ef6\u662f\u5426\u88ab\u53d6\u6d88\n                aborted = postingState.canceled;\n            } finally {\n                \/\/ 29\u3001\u91cd\u7f6epostingState\n                postingState.event = null;\n                postingState.subscription = null;\n                postingState.canceled = false;\n            }\n            if (aborted) {\n                \/\/ 30\u3001\u4e8b\u4ef6\u88ab\u53d6\u6d88\uff0c\u5219\u53d6\u6d88\u6b64\u7c7b\u578b\u7684\u5168\u90e8\u53d1\u5e03\u3002\n                break;\n            }\n        }\n        return true;\n    }\n    \/\/ 31\u3001\u65e0\u8ba2\u9605\u8005\uff0c\u4e0d\u8fdb\u884c\u53d1\u9001\uff0c\u8fd4\u56defalse\uff08\u4ee3\u8868\u6ca1\u6709\u627e\u5230\uff09\u3002\n    return false;\n}<\/code><\/pre>\n<p><code>postSingleEventForEventType()<\/code>\u65b9\u6cd5\uff0c\u4e3a\u4e8b\u4ef6\u7684\u53d1\u9001\uff0c\u517c\u5bb9\u4e86<strong>\u591a\u7ebf\u7a0b\u5355\u53d1\u9001\u5355\u4e8b\u4ef6<\/strong>\u7684\u60c5\u51b5\u3002\u6839\u636e<strong>\u4e8b\u4ef6<\/strong>\u83b7\u53d6<strong>\u6240\u6709<\/strong>\u8ba2\u9605\u5b83\u7684<code>Subscription<\/code>\uff08\u6301\u6709\u8ba2\u9605\u8005\u5bf9\u8c61\u3001\u8ba2\u9605\u65b9\u6cd5\u5bf9\u8c61\uff09\uff0c\u7136\u540e\u8fdb\u884c<strong>\u904d\u5386\u4f9d\u6b21\u53d1\u9001<\/strong>\u3002<\/p>\n<p><strong>EventBus &#8211;&gt; postToSubscription<\/strong><\/p>\n<pre><code class=\"language-java\">private void postToSubscription(Subscription subscription, Object event, boolean isMainThread) {\n    \/\/ 32\u3001\u6839\u636e\u8ba2\u9605\u65b9\u6cd5ThreadMode\u8c03\u7528\u8ba2\u9605\u65b9\u6cd5\n    switch (subscription.subscriberMethod.threadMode) {\n        case POSTING:\n            \/\/ 33\u3001\u53d1\u5e03\u7ebf\u7a0b\uff08\u9ed8\u8ba4\uff09\n            \/\/ 34\u3001\u8c03\u7528\u53cd\u5c04\u76f4\u63a5\u901a\u77e5\u5c06\u4e8b\u4ef6\n            invokeSubscriber(subscription, event);\n            break;\n        case MAIN:\n            \/\/ 35\u3001\u4e3b\u7ebf\u7a0b\n            if (isMainThread) {\n                \/\/ 36\u3001\u5728Android\u4e0a\u5e76\u4e14\u662f\u4e3b\u7ebf\u7a0b\uff0c\u6216\u8005\u4e0d\u5728Android\u4e0a\uff0c\u8c03\u7528\u53cd\u5c04\u76f4\u63a5\u901a\u77e5\u3002\n                invokeSubscriber(subscription, event);\n            } else {\n                \/\/ 37\u3001\u5728Android\u4e0a\u5e76\u4e14\u4e0d\u662f\u4e3b\u7ebf\u7a0b\uff0c\u5c06\u4e8b\u4ef6\u6dfb\u52a0\u5230\u4e3b\u7ebf\u7a0b\u961f\u5217\u4e2d\uff0c\u5728\u4e3b\u7ebf\u7a0b\u8c03\u7528\u3002\n                mainThreadPoster.enqueue(subscription, event);\n            }\n            break;\n        case MAIN_ORDERED:\n            \/\/ 38\u3001\u4e3b\u7ebf\u7a0b\uff08\u6709\u5e8f\uff09\n            if (mainThreadPoster != null) {\n                \/\/ 39\u3001\u5728Android\u4e0a\uff0c\u59cb\u7ec8\u5c06\u4e8b\u4ef6\u6dfb\u52a0\u5230\u4e3b\u7ebf\u7a0b\u961f\u5217\u4e2d\uff0c\u5728\u4e3b\u7ebf\u7a0b\u8c03\u7528\u3002\n                mainThreadPoster.enqueue(subscription, event);\n            } else {\n                \/\/ 40\u3001\u4e0d\u5728Android\u4e0a\uff0c\u76f4\u63a5\u901a\u8fc7\u53cd\u5c04\u8c03\u7528\u3002\n                invokeSubscriber(subscription, event);\n            }\n            break;\n        case BACKGROUND:\n            \/\/ 41\u3001\u540e\u53f0\u7ebf\u7a0b\n            if (isMainThread) {\n                \/\/ 42\u3001\u5728Android\u4e0a\u5e76\u4e14\u662f\u4e3b\u7ebf\u7a0b\uff0c\u6216\u8005\u4e0d\u5728Android\u4e0a\uff0c\u5219\u5c06\u4e8b\u4ef6\u6dfb\u52a0\u5230\u540e\u53f0\u7ebf\u7a0b\u961f\u5217\u4e2d\uff0c\u5728\u540e\u53f0\u7ebf\u7a0b\u8c03\u7528\u3002\n                backgroundPoster.enqueue(subscription, event);\n            } else {\n                \/\/ 43\u3001\u5728Android\u4e0a\u5e76\u4e14\u4e0d\u662f\u4e3b\u7ebf\u7a0b\uff0c\u76f4\u63a5\u901a\u8fc7\u53cd\u5c04\u8c03\u7528\u3002\n                invokeSubscriber(subscription, event);\n            }\n            break;\n        case ASYNC:\n            \/\/ 44\u3001\u5f02\u6b65\u7ebf\u7a0b\n            \/\/ 45\u3001\u5c06\u4e8b\u4ef6\u6dfb\u52a0\u5230\u5f02\u6b65\u7ebf\u7a0b\u961f\u5217\u4e2d\uff0c\u5728\u5f02\u6b65\u7ebf\u7a0b\u8c03\u7528\u3002\n            asyncPoster.enqueue(subscription, event);\n            break;\n        default:\n            throw new IllegalStateException(&quot;Unknown thread mode: &quot; + subscription.subscriberMethod.threadMode);\n    }\n}<\/code><\/pre>\n<p><code>postToSubscription()<\/code>\u65b9\u6cd5\uff0c\u4e3a\u4e8b\u4ef6\u7684\u901a\u77e5\uff0c\u517c\u5bb9\u4e86<strong>\u591a\u7ebf\u7a0b\u5355\u53d1\u9001\u5355\u4e8b\u4ef6<\/strong>\u7684\u60c5\u51b5\u3002\u6839\u636e<strong>\u8ba2\u9605\u65b9\u6cd5<\/strong>\u7684<code>ThreadMode<\/code>\u8c03\u7528\u8ba2\u9605\u65b9\u6cd5\uff0c\u5982\u679c\u9700\u8981\u7ebf\u7a0b\u5207\u6362\uff0c\u5219<strong>\u5207\u6362\u7ebf\u7a0b<\/strong>\u8fdb\u884c\u8c03\u7528\uff1b\u5426\u5219\uff0c\u76f4\u63a5\u8c03\u7528\u3002<\/p>\n<blockquote>\n<p>\u8bf4\u660e\uff1a<\/p>\n<ol>\n<li><code>mainThreadPoster<\/code>\uff08\u5373<code>HandlerPoster<\/code>\uff09\u3001<code>backgroundPoster<\/code>\u3001<code>asyncPoster<\/code>\u8fd93\u4e2a<code>Poster<\/code>\uff0c\u6700\u7ec8\u90fd\u4f1a\u8c03\u7528<code>invokeSubscriber()<\/code>\u65b9\u6cd5\u8fdb\u884c\u901a\u77e5\uff0c\u8be6\u7ec6\u4ecb\u7ecd\u770b\u540e\u9762\u7684-<code>Poster<\/code>\u3002<\/li>\n<li><code>EventBus<\/code>\u7684\u6ce8\u518c\uff0c\u5982\u679c\u662f<strong>\u7c98\u6027\u65b9\u6cd5<\/strong>\u5e76\u4e14<strong>\u5df2\u7ecf\u53d1\u9001<\/strong>\u4e86\u6b64\u4e8b\u4ef6\uff0c\u4e5f\u4f1a\u8c03\u7528<code>postToSubscription()<\/code>\u65b9\u6cd5\u8fdb\u884c\u901a\u77e5\u3002<\/li>\n<\/ol>\n<\/blockquote>\n<p><strong>EventBus &#8211;&gt; invokeSubscriber<\/strong><\/p>\n<pre><code class=\"language-java\">void invokeSubscriber(Subscription subscription, Object event) {\n    try {\n        \/\/ 46\u3001\u53cd\u5c04\u8c03\u7528\u8ba2\u9605\u8005\u5bf9\u8c61\u7684\u8ba2\u9605\u65b9\u6cd5\uff0c\u5e76\u4f20\u5165event\u53c2\u6570\u3002\n        subscription.subscriberMethod.method.invoke(subscription.subscriber, event);\n    } catch (InvocationTargetException e) {\n        \/\/ 47\u3001\u5904\u7406\u8c03\u7528\u8ba2\u9605\u65b9\u6cd5\u5f02\u5e38\n        handleSubscriberException(subscription, event, e.getCause());\n    } catch (IllegalAccessException e) {\n        throw new IllegalStateException(&quot;Unexpected exception&quot;, e);\n    }\n}\n\nprivate void handleSubscriberException(Subscription subscription, Object event, Throwable cause) {\n    if (event instanceof SubscriberExceptionEvent) {\n        \/\/ SubscriberExceptionEvent\u4e8b\u4ef6\uff0c\u5224\u65ad\u8c03\u7528\u8ba2\u9605\u65b9\u6cd5\u5f02\u5e38\u65f6\u662f\u5426\u6253\u5370Log\uff08\u4e0d\u53d1\u9001\u4e8b\u4ef6\u9632\u6b62\u9012\u5f52\uff09\u3002\n        if (logSubscriberExceptions) {\n            \/\/ \u8c03\u7528\u8ba2\u9605\u65b9\u6cd5\u5f02\u5e38\u65f6\uff0c\u6253\u5370\u5f02\u5e38\u4fe1\u606f\u3002\n            \/\/ Don&#039;t send another SubscriberExceptionEvent to avoid infinite event recursion, just log\n            logger.log(Level.SEVERE, &quot;SubscriberExceptionEvent subscriber &quot; + subscription.subscriber.getClass()\n                    + &quot; threw an exception&quot;, cause);\n            SubscriberExceptionEvent exEvent = (SubscriberExceptionEvent) event;\n            logger.log(Level.SEVERE, &quot;Initial event &quot; + exEvent.causingEvent + &quot; caused exception in &quot;\n                    + exEvent.causingSubscriber, exEvent.throwable);\n        }\n    } else {\n        \/\/ \u666e\u901a\u4e8b\u4ef6\uff0c\u5224\u65ad\u8c03\u7528\u8ba2\u9605\u65b9\u6cd5\u5f02\u5e38\u65f6\u662f\u5426\u629b\u51fa\u5f02\u5e38\u3001\u6253\u5370Log\u3001\u53d1\u9001SubscriberExceptionEvent\u3002\n        if (throwSubscriberException) {\n            \/\/ \u8c03\u7528\u8ba2\u9605\u65b9\u6cd5\u5f02\u5e38\u65f6\uff0c\u629b\u51faSubscriberException\u5f02\u5e38\u3002\n            throw new EventBusException(&quot;Invoking subscriber failed&quot;, cause);\n        }\n        if (logSubscriberExceptions) {\n            \/\/ \u8c03\u7528\u8ba2\u9605\u65b9\u6cd5\u5f02\u5e38\u65f6\uff0c\u6253\u5370\u5f02\u5e38\u4fe1\u606f\u3002\n            logger.log(Level.SEVERE, &quot;Could not dispatch event: &quot; + event.getClass() + &quot; to subscribing class &quot;\n                    + subscription.subscriber.getClass(), cause);\n        }\n        if (sendSubscriberExceptionEvent) {\n            \/\/ \u8c03\u7528\u8ba2\u9605\u65b9\u6cd5\u5f02\u5e38\u65f6\uff0c\u53d1\u9001SubscriberExceptionEvent\u4e8b\u4ef6\u3002\n            SubscriberExceptionEvent exEvent = new SubscriberExceptionEvent(this, cause, event,\n                    subscription.subscriber);\n            post(exEvent);\n        }\n    }\n}<\/code><\/pre>\n<p><code>invokeSubscriber()<\/code>\u65b9\u6cd5\uff0c\u4e3a<strong>\u53cd\u5c04<\/strong>\u8c03\u7528<strong>\u8ba2\u9605\u8005\u5bf9\u8c61<\/strong>\u7684<strong>\u8ba2\u9605\u65b9\u6cd5<\/strong>\uff0c\u5e76\u4f20\u5165<code>event<\/code>\u53c2\u6570\u3002<code>handleSubscriberException()<\/code>\u65b9\u6cd5\uff0c\u4e3a\u5904\u7406<strong>\u8c03\u7528\u8ba2\u9605\u65b9\u6cd5\u5f02\u5e38<\/strong>\uff0c\u5224\u65ad\u662f\u5426<strong>\u629b\u51fa\u5f02\u5e38<\/strong>\u3001<strong>\u6253\u5370<\/strong><code>Log<\/code>\u3001<strong>\u53d1\u9001<\/strong><code>SubscriberExceptionEvent<\/code>\u3002<\/p>\n<h3>\u7ebf\u7a0b\u6a21\u578b<\/h3>\n<h4>POSTING\uff08\u53d1\u5e03\u7ebf\u7a0b\uff09<\/h4>\n<p>\u4e8b\u4ef6\u7684<strong>\u8ba2\u9605<\/strong>\u548c\u4e8b\u4ef6\u7684<strong>\u53d1\u5e03<\/strong>\u5904\u4e8e<strong>\u540c\u4e00\u7ebf\u7a0b<\/strong>\uff08<code>\u6b65\u9aa434<\/code>\uff09\u3002\u8fd9\u662f<strong>\u9ed8\u8ba4<\/strong>\u8bbe\u7f6e\u3002\u4e8b\u4ef6\u4f20\u9012\u610f\u5473\u7740\u6700\u5c11\u7684\u5f00\u9500\uff0c\u56e0\u4e3a\u5b83\u5b8c\u5168<strong>\u907f\u514d<\/strong>\u4e86\u7ebf\u7a0b\u5207\u6362\u3002\u56e0\u6b64\uff0c\u5bf9\u4e8e\u5df2\u77e5\u5728<strong>\u975e\u5e38\u77ed\u7684\u65f6\u95f4\u5185\u5b8c\u6210<\/strong>\u800c\u4e0d\u9700\u8981<strong>\u4e3b\u7ebf\u7a0b<\/strong>\u7684\u7b80\u5355\u4efb\u52a1\uff0c\u8fd9\u662f<strong>\u63a8\u8350<\/strong>\u7684\u6a21\u5f0f\u3002\u4f7f\u7528\u6b64\u6a21\u5f0f\u7684\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u5fc5\u987b\u5feb\u901f\u8fd4\u56de\uff0c\u4ee5<strong>\u907f\u514d\u963b\u585e<\/strong>\u53ef\u80fd\u662f<strong>\u4e3b\u7ebf\u7a0b<\/strong>\u7684<strong>\u53d1\u5e03\u7ebf\u7a0b<\/strong>\u3002<\/p>\n<blockquote>\n<p>\u8bf4\u660e\uff1a<\/p>\n<ol>\n<li><strong>\u4e8b\u4ef6\u7684\u53d1\u5e03\uff08\u53ef\u80fd\u4f1a\u963b\u585eAndroid\u4e3b\u7ebf\u7a0b\uff09<\/strong>\n<ul>\n<li>\u4e8b\u4ef6\u7684<strong>\u63a5\u6536<\/strong>\u548c<strong>\u53d1\u5e03<\/strong>\u5728<strong>\u540c\u4e00\u4e2a\u7ebf\u7a0b<\/strong>\uff0c\u7531\u4e8e<strong>\u6ca1\u6709<\/strong>\u4f7f\u7528<strong>\u7ebf\u7a0b\u6c60<\/strong>\uff0c\u6240\u4ee5\u53d1\u5e03<strong>\u4e00\u5b9a<\/strong>\u4f1a<strong>\u963b\u585e\u7ebf\u7a0b<\/strong>\u3002 <\/li>\n<\/ul>\n<\/li>\n<li><strong>\u4e8b\u4ef6\u7684\u63a5\u6536\uff08\u53ef\u80fd\u4f1a\u963b\u585eAndroid\u4e3b\u7ebf\u7a0b\uff09<\/strong>\n<ul>\n<li>\u4e8b\u4ef6\u7684<strong>\u63a5\u6536<\/strong>\u548c<strong>\u53d1\u5e03<\/strong>\u5728<strong>\u540c\u4e00\u4e2a\u7ebf\u7a0b<\/strong>\uff0c\u7531\u4e8e\u53ef\u80fd\u662f<strong>\u4e3b\u7ebf\u7a0b<\/strong>\u53d1\u9001\uff0c\u6240\u4ee5\u63a5\u6536<strong>\u53ef\u80fd<\/strong>\u4f1a<strong>\u963b\u585e\u4e3b\u7ebf\u7a0b<\/strong>\uff0c\u6240\u4ee5<strong>\u63a5\u6536<\/strong>\u91cc<strong>\u4e0d\u63a8\u8350<\/strong>\u6267\u884c<strong>\u8017\u65f6<\/strong>\u64cd\u4f5c\u3002 <\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<\/blockquote>\n<h4>MAIN\uff08\u4e3b\u7ebf\u7a0b\uff09<\/h4>\n<p>\u5728<code>Android<\/code>\u4e0a\uff0c<strong>\u8ba2\u9605\u8005<\/strong>\u5c06\u5728<code>Android<\/code>\u7684<strong>\u4e3b\u7ebf\u7a0b<\/strong>\uff08UI\u7ebf\u7a0b\uff09\u4e2d\u88ab\u8c03\u7528\u3002\u5982\u679c<strong>\u53d1\u5e03\u7ebf\u7a0b<\/strong>\u662f<strong>\u4e3b\u7ebf\u7a0b<\/strong>\uff0c\u5219\u5c06<strong>\u76f4\u63a5\u8c03\u7528<\/strong>\u8ba2\u9605\u65b9\u6cd5\uff0c\u4ece\u800c<strong>\u963b\u585e\u53d1\u5e03\u7ebf\u7a0b<\/strong>\uff08<code>\u6b65\u9aa436<\/code>\uff09\u3002\u5426\u5219\uff0c\u4e8b\u4ef6\u5c06<strong>\u6392\u961f\u7b49\u5f85<\/strong>\u4f20\u9012\uff08<strong>\u975e\u963b\u585e<\/strong>\uff09\uff08<code>\u6b65\u9aa437<\/code>\uff09\u3002\u4f7f\u7528\u6b64\u6a21\u5f0f\u7684\u8ba2\u9605\u8005<strong>\u5fc5\u987b<\/strong>\u5feb\u901f\u8fd4\u56de\u4ee5<strong>\u907f\u514d\u963b\u585e<\/strong>\u4e3b\u7ebf\u7a0b\u3002\u5982\u679c\u4e0d\u662f\u5728<code>Android<\/code>\u4e0a\uff0c\u5219\u884c\u4e3a\u4e0e<code>POSTING<\/code>\u76f8\u540c\uff08<code>\u6b65\u9aa436<\/code>\uff09\u3002<\/p>\n<blockquote>\n<p>\u8bf4\u660e\uff1a<\/p>\n<ol>\n<li><strong>\u4e8b\u4ef6\u7684\u53d1\u5e03\uff08\u4e00\u5b9a\u4f1a\u963b\u585eAndroid\u4e3b\u7ebf\u7a0b\uff09<\/strong>\n<ul>\n<li>\u5728<code>Android<\/code>\u4e0a\u5e76\u4e14\u662f<code>\u4e3b\u7ebf\u7a0b<\/code>\uff0c\u6216\u8005<strong>\u4e0d\u5728<\/strong><code>Android<\/code>\u4e0a\uff0c\u5219\u884c\u4e3a\u4e0e<code>POSTING<\/code>\u76f8\u540c\uff08<code>\u6b65\u9aa436<\/code>\uff09\uff0c\u6240\u4ee5\u53d1\u5e03<strong>\u4e00\u5b9a<\/strong>\u4f1a<strong>\u963b\u585e\u7ebf\u7a0b<\/strong>\u3002<\/li>\n<li>\u5728<code>Android<\/code>\u4e0a\u5e76\u4e14<strong>\u4e0d\u662f<\/strong><code>\u4e3b\u7ebf\u7a0b<\/code>\uff0c\u5c06\u4e8b\u4ef6\u63d2\u5165\u5230<strong>\u4e3b\u7ebf\u7a0b\u961f\u5217<\/strong>\u4e2d\uff08<code>\u6b65\u9aa437<\/code>\uff09\uff0c\u7531\u4e8e\u4f7f\u7528\u4e86<strong>\u7ebf\u7a0b\u6c60<\/strong>\uff0c\u6240\u4ee5\u53d1\u5e03<strong>\u4e00\u5b9a<\/strong>\u4e0d\u4f1a<strong>\u963b\u585e\u7ebf\u7a0b<\/strong>\u3002<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u4e8b\u4ef6\u7684\u63a5\u6536\uff08\u4e00\u5b9a\u4f1a\u963b\u585eAndroid\u4e3b\u7ebf\u7a0b\uff09<\/strong>\n<ul>\n<li>\u4e8b\u4ef6\u7684<strong>\u63a5\u6536<\/strong>\u5728<strong>\u4e3b\u7ebf\u7a0b<\/strong>\uff0c\u6240\u4ee5\u63a5\u6536<strong>\u4e00\u5b9a<\/strong>\u4f1a<strong>\u963b\u585e\u4e3b\u7ebf\u7a0b<\/strong>\uff0c\u6240\u4ee5<strong>\u63a5\u6536<\/strong>\u91cc<strong>\u4e0d\u80fd<\/strong>\u6267\u884c<strong>\u8017\u65f6<\/strong>\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u4e8b\u4ef6\u7684\u6709\u5e8f\u6027\uff08\u53ef\u80fd\u4e0d\u662f\u6709\u5e8f\u7684\uff09<\/strong>\n<ul>\n<li>\u5728<code>Android<\/code>\u4e0a\uff0c\u5982\u679c\u5148<strong>\u975e\u4e3b\u7ebf\u7a0b<\/strong>\u53d1\u5e03\uff08<strong>\u6392\u961f\u7b49\u5f85<\/strong>\uff0c\u53ef\u80fd\u4f1a\u7b49\u5f85\u5f88\u957f\u65f6\u95f4\u624d\u4f1a\u6267\u884c\u5230\u81ea\u5df1\uff09\uff0c\u540e<strong>\u4e3b\u7ebf\u7a0b<\/strong>\u53d1\u5e03\uff08<strong>\u76f4\u63a5\u8c03\u7528<\/strong>\uff09\uff0c\u6240\u4ee5\u5728<code>Android<\/code>\u4e0a\u4e8b\u4ef6\u5b83<strong>\u53ef\u80fd<\/strong>\u662f<strong>\u4e0d\u662f\u6709\u5e8f<\/strong>\u7684\u3002<\/li>\n<li>\u5728\u975e<code>Android<\/code>\u4e0a\uff0c\u7531\u4e8e\u662f<strong>\u76f4\u63a5\u8c03\u7528<\/strong>\uff0c\u6240\u4ee5\u4e8b\u4ef6<strong>\u4e00\u5b9a<\/strong>\u662f<strong>\u6709\u5e8f<\/strong>\u7684\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<\/blockquote>\n<h4>MAIN_ORDERED\uff08\u4e3b\u7ebf\u7a0b-\u6709\u5e8f\uff09<\/h4>\n<p>\u5728<code>Android<\/code>\u4e0a\uff0c<strong>\u8ba2\u9605\u8005<\/strong>\u5c06\u5728<code>Android<\/code>\u7684<strong>\u4e3b\u7ebf\u7a0b<\/strong>\uff08UI\u7ebf\u7a0b\uff09\u4e2d\u88ab\u8c03\u7528\u3002\u4e0e<code>MAIN<\/code>\u4e0d\u540c\u7684\u662f\uff0c\u4e8b\u4ef6\u603b\u662f<strong>\u6392\u961f\u7b49\u5f85<\/strong>\u4f20\u9012\uff08<code>\u6b65\u9aa439<\/code>\uff09\u3002\u8fd9\u786e\u4fdd\u4e86<code>post<\/code>\u8c03\u7528\u662f<strong>\u975e\u963b\u585e<\/strong>\u7684\u3002<\/p>\n<blockquote>\n<p>\u8bf4\u660e\uff1a<\/p>\n<ol>\n<li><strong>\u4e8b\u4ef6\u7684\u53d1\u5e03\uff08\u4e00\u5b9a\u4e0d\u4f1a\u963b\u585eAndroid\u4e3b\u7ebf\u7a0b\uff09<\/strong>\n<ul>\n<li>\u5728<code>Android<\/code>\u4e0a\uff0c\u59cb\u7ec8\u5c06<strong>\u4e8b\u4ef6<\/strong>\u6dfb\u52a0\u5230<strong>\u4e3b\u7ebf\u7a0b\u961f\u5217<\/strong>\u4e2d\uff0c\u5728\u4e3b\u7ebf\u7a0b\u8c03\u7528\uff0c\u7531\u4e8e\u4f7f\u7528<code>Handler<\/code>\u53d1\u5e03\uff0c\u6240\u4ee5\u53d1\u5e03<strong>\u4e00\u5b9a<\/strong>\u4e0d\u4f1a<strong>\u963b\u585e\u7ebf\u7a0b<\/strong>\u3002<\/li>\n<li>\u4e0d\u5728<code>Android<\/code>\u4e0a\uff0c\u5219\u884c\u4e3a\u4e0e<code>POSTING<\/code>\u76f8\u540c\uff0c\u6240\u4ee5\u53d1\u5e03<strong>\u4e00\u5b9a<\/strong>\u4f1a<strong>\u963b\u585e\u7ebf\u7a0b<\/strong>\u3002<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u4e8b\u4ef6\u7684\u63a5\u6536\uff08\u4e00\u5b9a\u4f1a\u963b\u585eAndroid\u4e3b\u7ebf\u7a0b\uff09<\/strong>\n<ul>\n<li>\u4e8b\u4ef6\u7684<strong>\u63a5\u6536<\/strong>\u5728<strong>\u4e3b\u7ebf\u7a0b<\/strong>\uff0c\u6240\u4ee5\u63a5\u6536<strong>\u4e00\u5b9a<\/strong>\u4f1a<strong>\u963b\u585e\u4e3b\u7ebf\u7a0b<\/strong>\uff0c\u6240\u4ee5<strong>\u63a5\u6536<\/strong>\u91cc<strong>\u4e0d\u80fd<\/strong>\u6267\u884c<strong>\u8017\u65f6<\/strong>\u64cd\u4f5c\uff08\u540c<code>MAIN<\/code>\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u4e8b\u4ef6\u7684\u6709\u5e8f\u6027\uff08\u4e00\u5b9a\u662f\u6709\u5e8f\u7684\uff09<\/strong>\n<ul>\n<li>\u5728<code>Android<\/code>\u4e0a\uff0c<strong>\u59cb\u7ec8<\/strong>\u6dfb\u52a0\u5230<strong>\u4e3b\u7ebf\u7a0b\u961f\u5217<\/strong>\uff0c\u6240\u4ee5\u4e8b\u4ef6<strong>\u4e00\u5b9a<\/strong>\u662f<strong>\u6709\u5e8f<\/strong>\u7684\uff08<code>\u6b65\u9aa439<\/code>\uff09\u3002<\/li>\n<li>\u5728\u975e<code>Android<\/code>\u4e0a\uff0c\u7531\u4e8e\u662f<strong>\u76f4\u63a5\u8c03\u7528<\/strong>\uff0c\u6240\u4ee5\u4e8b\u4ef6<strong>\u4e00\u5b9a<\/strong>\u662f<strong>\u6709\u5e8f<\/strong>\u7684\uff08<code>\u6b65\u9aa440<\/code>\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<\/blockquote>\n<h4>BACKGROUND\uff08\u540e\u53f0\uff09<\/h4>\n<p>\u5728<code>Android<\/code>\u4e0a\uff0c<strong>\u8ba2\u9605\u8005<\/strong>\u5c06\u5728<strong>\u540e\u53f0\u7ebf\u7a0b<\/strong>\u4e2d\u88ab\u8c03\u7528\u3002\u5982\u679c<strong>\u53d1\u5e03\u7ebf\u7a0b<\/strong>\u4e0d\u662f<strong>\u4e3b\u7ebf\u7a0b<\/strong>\uff0c\u5219\u5c06\u5728<strong>\u53d1\u5e03\u7ebf\u7a0b<\/strong>\u4e2d\u76f4\u63a5\u8c03\u7528\u8ba2\u9605\u65b9\u6cd5\uff08<code>\u6b65\u9aa443<\/code>\uff09\u3002\u5982\u679c<strong>\u53d1\u5e03\u7ebf\u7a0b<\/strong>\u662f<strong>\u4e3b\u7ebf\u7a0b<\/strong>\uff0c\u5219<code>EventBus<\/code>\u4f7f\u7528<strong>\u4e00\u4e2a\u5355\u72ec<\/strong>\u7684<strong>\u540e\u53f0\u7ebf\u7a0b<\/strong>\uff0c\u5b83\u5c06\u6309<strong>\u987a\u5e8f<\/strong>\u4f20\u9012\u5176\u6240\u6709\u4e8b\u4ef6\uff08<code>\u6b65\u9aa442<\/code>\uff09\u3002\u4f7f\u7528\u6b64\u6a21\u5f0f\u7684\u8ba2\u9605\u8005\u5e94\u8be5\u5c1d\u8bd5\u5feb\u901f\u8fd4\u56de\uff0c\u4ee5<strong>\u907f\u514d\u963b\u585e\u540e\u53f0\u7ebf\u7a0b<\/strong>\u3002\u5982\u679c\u4e0d\u5728<code>Android<\/code>\u4e0a\uff0c\u5219\u59cb\u7ec8\u4f7f\u7528<strong>\u540e\u53f0\u7ebf\u7a0b<\/strong>\uff08<code>\u6b65\u9aa442<\/code>\uff09\u3002<\/p>\n<blockquote>\n<p>\u8bf4\u660e\uff1a<\/p>\n<ol>\n<li><strong>\u4e8b\u4ef6\u7684\u53d1\u5e03\uff08\u4e00\u5b9a\u4e0d\u4f1a\u963b\u585eAndroid\u4e3b\u7ebf\u7a0b\uff09<\/strong>\n<ul>\n<li>\u5728<code>Android<\/code>\u4e0a\u5e76\u4e14\u662f<code>\u4e3b\u7ebf\u7a0b<\/code>\uff0c\u6216\u8005<strong>\u4e0d\u5728<\/strong><code>Android<\/code>\u4e0a\uff0c\u5c06\u4e8b\u4ef6\u63d2\u5165\u5230<strong>\u540e\u53f0\u7ebf\u7a0b\u961f\u5217<\/strong>\u4e2d\uff08<code>\u6b65\u9aa442<\/code>\uff09\uff0c\u7531\u4e8e\u4f7f\u7528\u4e86<strong>\u7ebf\u7a0b\u6c60<\/strong>\uff0c\u6240\u4ee5\u53d1\u5e03<strong>\u4e00\u5b9a<\/strong>\u4e0d\u4f1a<strong>\u963b\u585e\u7ebf\u7a0b<\/strong>\u3002<\/li>\n<li>\u5728<code>Android<\/code>\u4e0a\u5e76\u4e14<strong>\u4e0d\u662f<\/strong><code>\u4e3b\u7ebf\u7a0b<\/code>\uff0c\u5219\u884c\u4e3a\u4e0e<code>POSTING<\/code>\u76f8\u540c\uff08<code>\u6b65\u9aa443<\/code>\uff09\uff0c\u6240\u4ee5\u53d1\u5e03<strong>\u4e00\u5b9a<\/strong>\u4f1a<strong>\u963b\u585e\u7ebf\u7a0b<\/strong>\u3002<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u4e8b\u4ef6\u7684\u63a5\u6536\uff08\u53ef\u80fd\u4f1a\u963b\u585e\u7ebf\u7a0b\u6c60\u7ebf\u7a0b\uff09<\/strong>\n<ul>\n<li>\u5728<code>Android<\/code>\u4e0a\u5e76\u4e14\u662f<code>\u4e3b\u7ebf\u7a0b<\/code>\uff0c\u4f7f\u7528<code>backgroundPoster<\/code>\u6267\u884c\uff08<code>\u6b65\u9aa442<\/code>\uff09\uff0c\u7531\u4e8e<code>backgroundPoster<\/code>\u91cc\u5224\u65ad\u4e86\u5982\u679c<strong>\u6b63\u5728\u6267\u884c\u4e2d<\/strong>\uff0c\u5219<strong>\u4e0d\u518d\u521b\u5efa\u7ebf\u7a0b<\/strong>\u6267\u884c\uff0c\u800c\u662f\u7528<strong>\u4e0a\u6b21\u7ebf\u7a0b<\/strong>\u4e00\u5757\u6267\u884c\uff08\u8be6\u7ec6\u770b\u4e0b\u9762\u7684-<code>Poster<\/code>&#8211;<code>backgroundPoster<\/code>\uff09\uff0c\u6240\u4ee5\u63a5\u6536<strong>\u53ef\u80fd<\/strong>\u4f1a<strong>\u963b\u585e\u540e\u53f0\u7ebf\u7a0b<\/strong>\uff0c\u6240\u4ee5<strong>\u63a5\u6536<\/strong>\u91cc<strong>\u4e0d\u63a8\u8350<\/strong>\u6267\u884c<strong>\u8017\u65f6<\/strong>\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<\/blockquote>\n<h4>ASYNC\uff08\u5f02\u6b65\uff09<\/h4>\n<p><strong>\u8ba2\u9605\u8005<\/strong>\u5c06\u5728<strong>\u5355\u72ec<\/strong>\u7684<strong>\u7ebf\u7a0b<\/strong>\u4e2d\u8c03\u7528\u3002\u8fd9\u59cb\u7ec8<strong>\u72ec\u7acb<\/strong>\u4e8e<strong>\u53d1\u5e03\u7ebf\u7a0b<\/strong>\u548c<strong>\u4e3b\u7ebf\u7a0b<\/strong>\u3002\u4f7f\u7528\u6b64\u6a21\u5f0f\u53d1\u5e03\u4e8b\u4ef6<strong>\u4ece\u4e0d\u7b49\u5f85<\/strong>\u8ba2\u9605\u65b9\u6cd5\u3002\u5982\u679c\u8ba2\u9605\u65b9\u6cd5\u7684\u6267\u884c\u53ef\u80fd\u9700\u8981\u4e00\u4e9b\u65f6\u95f4\uff08\u4f8b\u5982\uff0c\u7528\u4e8e\u7f51\u7edc\u8bbf\u95ee\uff09\uff0c\u5219\u5e94\u4f7f\u7528\u6b64\u6a21\u5f0f\u3002\u907f\u514d\u540c\u65f6\u89e6\u53d1\u5927\u91cf\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u5f02\u6b65\u8ba2\u9605\u65b9\u6cd5\u4ee5<strong>\u9650\u5236\u5e76\u53d1\u7ebf\u7a0b\u7684\u6570\u91cf<\/strong>\u3002<code>EventBus<\/code>\u4f7f\u7528<strong>\u7ebf\u7a0b\u6c60<\/strong>\u9ad8\u6548\u5730<strong>\u91cd\u7528<\/strong>\u5df2\u5b8c\u6210\u5f02\u6b65\u8ba2\u9605\u8005\u901a\u77e5\u4e2d\u7684<strong>\u7ebf\u7a0b<\/strong>\u3002<\/p>\n<blockquote>\n<p>\u8bf4\u660e\uff1a<\/p>\n<ol>\n<li><strong>\u4e8b\u4ef6\u7684\u53d1\u5e03\uff08\u4e00\u5b9a\u4e0d\u4f1a\u963b\u585eAndroid\u4e3b\u7ebf\u7a0b\uff09<\/strong>\n<ul>\n<li><strong>\u8ba2\u9605\u8005<\/strong>\u5c06\u5728<strong>\u5355\u72ec<\/strong>\u7684<strong>\u7ebf\u7a0b<\/strong>\u4e2d\u8c03\u7528\uff08<code>\u6b65\u9aa445<\/code>\uff09\uff0c\u7531\u4e8e\u4f7f\u7528\u4e86<strong>\u7ebf\u7a0b\u6c60<\/strong>\uff0c\u6240\u4ee5\u53d1\u5e03<strong>\u4e00\u5b9a<\/strong>\u4e0d\u4f1a<strong>\u963b\u585e\u7ebf\u7a0b<\/strong>\u3002<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u4e8b\u4ef6\u7684\u63a5\u6536\uff08\u4e0d\u4f1a\u963b\u585e\u7ebf\u7a0b\u6c60\u7ebf\u7a0b\uff09<\/strong>\n<ul>\n<li><strong>\u8ba2\u9605\u8005<\/strong>\u5c06\u5728<strong>\u5355\u72ec<\/strong>\u7684<strong>\u7ebf\u7a0b<\/strong>\u4e2d\u8c03\u7528\uff0c\u4f7f\u7528<code>asyncPoster<\/code>\u6267\u884c\uff08<code>\u6b65\u9aa445<\/code>\uff09\uff0c\u7531\u4e8e<code>asyncPoster<\/code>\u91cc\u6267\u884c<strong>\u6bcf\u6b21<\/strong>\u90fd\u5728<strong>\u7ebf\u7a0b\u6c60<\/strong>\u91cc\u9762\u4f7f\u7528<strong>\u65b0\u7684\u7ebf\u7a0b<\/strong>\u6267\u884c\uff08\u8be6\u7ec6\u770b\u4e0b\u9762\u7684-<code>Poster<\/code>&#8211;<code>asyncPoster<\/code>\uff09\uff0c\u6240\u4ee5<strong>\u63a5\u6536<\/strong>\u91cc<strong>\u80fd<\/strong>\u6267\u884c<strong>\u8017\u65f6<\/strong>\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<\/blockquote>\n<h3>\u5c0f\u7ed3<\/h3>\n<blockquote>\n<ol>\n<li><code>EventBus<\/code>\u7684<code>post()<\/code>\uff0c\u83b7\u53d6<strong>\u5f53\u524d\u7ebf\u7a0b<\/strong>\u7684<strong>\u4e8b\u4ef6\u961f\u5217<\/strong>\uff0c\u5c06\u8981\u53d1\u9001\u7684\u4e8b\u4ef6<strong>\u52a0\u5165<\/strong>\u5230\u5176\u4e2d\u3002<\/li>\n<li><strong>\u904d\u5386<\/strong>\u4e8b\u4ef6\u961f\u5217\uff0c\u53ea\u8981\u6709\u4e8b\u4ef6\uff0c\u5c31<strong>\u4e00\u76f4\u53d1\u9001<\/strong>\u3002<\/li>\n<li>\u5224\u65ad\u4e8b\u4ef6\u662f\u5426\u6709<strong>\u7ee7\u627f\u6027<\/strong>\uff0c\u5982\u679c\u6709\u5219\u6839\u636e<strong>\u4e8b\u4ef6\u7c7b\u578b<\/strong>\uff0c\u627e\u5230\u5f53\u524d<code>Event<\/code>\u7684<strong>\u6240\u6709\u7236\u7c7b<\/strong>\u4ee5\u53ca<strong>\u6240\u6709\u7236\u63a5\u53e3<\/strong>\u7684<code>class<\/code>\u96c6\u5408\uff0c<strong>\u904d\u5386<\/strong>\u8fd9\u4e2a\u96c6\u5408\uff0c\u4f9d\u6b21\u53d1\u9001<strong>\u5355\u4e2a<\/strong>\u4e8b\u4ef6\uff1b\u5426\u5219\uff0c\u5219\u53ea\u53d1\u9001<strong>\u5f53\u524d<\/strong><code>Event<\/code><strong>\u4e00\u4e2a<\/strong>\u4e8b\u4ef6\u3002<\/li>\n<li>\u6839\u636e<strong>\u4e8b\u4ef6<\/strong>\u83b7\u53d6<strong>\u6240\u6709\u8ba2\u9605\u5b83\u7684<\/strong><code>Subscription<\/code>\u96c6\u5408\uff0c<strong>\u904d\u5386<\/strong>\u96c6\u5408\uff0c\u5c06\u4e8b\u4ef6\u53d1\u9001\u7ed9<strong>\u8ba2\u9605\u8005<\/strong>\u7684<strong>\u8ba2\u9605\u65b9\u6cd5<\/strong>\u3002<\/li>\n<li>\u53d1\u9001\u7ed9\u8ba2\u9605\u8005\u65f6\uff0c\u6839\u636e\u8ba2\u9605\u65b9\u6cd5\u7684<strong>\u7ebf\u7a0b\u6a21\u578b<\/strong>\u8c03\u7528\u8ba2\u9605\u65b9\u6cd5\uff0c\u5982\u679c\u9700\u8981\u7ebf\u7a0b\u5207\u6362\uff0c\u5219<strong>\u5207\u6362\u7ebf\u7a0b<\/strong>\u8fdb\u884c\u8c03\u7528\uff1b\u5426\u5219\uff0c<strong>\u76f4\u63a5\u8c03\u7528<\/strong>\u3002<\/li>\n<\/ol>\n<\/blockquote>\n<h2>EventBus\u7684postSticky<\/h2>\n<p><strong>EventBus &#8211;&gt; postSticky()<\/strong><\/p>\n<pre><code class=\"language-java\">public void postSticky(Object event) {\n    synchronized (stickyEvents) {\n        \/\/ 1\u3001\u5c06\u4e8b\u4ef6\u6dfb\u52a0\u5230\u7c98\u6027\u4e8b\u4ef6\u96c6\u5408\u4e2d\n        stickyEvents.put(event.getClass(), event);\n    }\n    \/\/ 2\u3001\u53d1\u9001\u4e8b\u4ef6\n    post(event);\n}<\/code><\/pre>\n<blockquote>\n<p>\u8bf4\u660e\uff1a<\/p>\n<ol>\n<li><code>EventBus<\/code>\u7684<code>post()<\/code>\uff0c\u53ea\u4f1a\u53d1\u9001\u7ed9<strong>\u5df2\u7ecf\u6ce8\u518c<\/strong>\u7684<strong>\u8ba2\u9605\u8005<\/strong>\u3002<\/li>\n<li><code>EventBus<\/code>\u7684<code>postSticky()<\/code>\uff0c\u4f1a\u53d1\u9001\u7ed9<strong>\u5df2\u7ecf\u6ce8\u518c<\/strong>\u7684<strong>\u8ba2\u9605\u8005<\/strong>\u3001<strong>\u4e4b\u540e\u8981\u6ce8\u518c<\/strong>\u7684<strong>\u8ba2\u9605\u8005<\/strong>\u7684<strong>\u7c98\u6027\u8ba2\u9605\u65b9\u6cd5<\/strong>\u3002<\/li>\n<\/ol>\n<\/blockquote>\n<h3>\u5c0f\u7ed3<\/h3>\n<blockquote>\n<ol>\n<li><code>EventBus<\/code>\u7684<code>postSticky()<\/code>\uff0c\u5c06<strong>\u4e8b\u4ef6<\/strong>\u52a0\u5165\u5230<code>stickyEvents<\/code>\u96c6\u5408\u4e2d\uff0c\u5f53\u6709<strong>\u65b0\u7684<\/strong>\u8ba2\u9605\u8005<strong>\u6ce8\u518c<\/strong>\u540e\uff0c\u5982\u679c<strong>\u8be5\u8ba2\u9605\u8005<\/strong>\u8ba2\u9605\u4e86<strong>\u8be5\u4e8b\u4ef6<\/strong>\u7684<strong>\u7c98\u6027\u8ba2\u9605\u65b9\u6cd5<\/strong>\uff0c\u5219\u4f1a<strong>\u53d1\u9001<\/strong>\u7ed9<strong>\u6b64\u65b9\u6cd5<\/strong>\u3002<\/li>\n<li>\u5c06<strong>\u4e8b\u4ef6<\/strong>\u53d1\u9001\u7ed9<strong>\u5df2\u7ecf\u6ce8\u518c<\/strong>\u7684<strong>\u8ba2\u9605\u8005<\/strong>\u3002<\/li>\n<\/ol>\n<\/blockquote>\n<h2>EventBus\u7684\u6ce8\u9500<\/h2>\n<p><strong>EventBus &#8211;&gt; unregister()<\/strong><\/p>\n<pre><code class=\"language-java\">public synchronized void unregister(Object subscriber) {\n    \/\/ 1\u3001\u83b7\u53d6\u8ba2\u9605\u8005\u8ba2\u9605\u7684\u6240\u6709\u4e8b\u4ef6\n    List&lt;Class&lt;?&gt;&gt; subscribedTypes = typesBySubscriber.get(subscriber);\n    if (subscribedTypes != null) {\n        \/\/ 2\u3001\u904d\u5386\u6b64\u8ba2\u9605\u8005\u8ba2\u9605\u7684\u6240\u6709\u4e8b\u4ef6\u96c6\u5408\n        for (Class&lt;?&gt; eventType : subscribedTypes) {\n            \/\/ 3\u3001\u901a\u8fc7eventType\uff0c\u5c06\u6b64\u4e8b\u4ef6\u7684Subscription\u96c6\u5408\u4e2d\u542b\u6709\u8be5\u8ba2\u9605\u8005\u7684Subscription\uff0c\u4ece\u96c6\u5408\u4e2d\u79fb\u9664\u3002\n            unsubscribeByEventType(subscriber, eventType);\n        }\n        \/\/ 4\u3001\u5c06\u6b64\u8ba2\u9605\u8005\u4ecetypesBySubscriber\u4e2d\u79fb\u9664\n        typesBySubscriber.remove(subscriber);\n    } else {\n        \/\/ 5\u3001\u672a\u6ce8\u518c\uff0c\u8c03\u7528\u4e86\u6ce8\u9500\uff0cLog\u6253\u5370\u8b66\u544a\u3002\n        logger.log(Level.WARNING, &quot;Subscriber to unregister was not registered before: &quot; + subscriber.getClass());\n    }\n}<\/code><\/pre>\n<p><code>unregister()<\/code>\u65b9\u6cd5\uff0c\u4e3a<strong>\u6ce8\u9500\u8ba2\u9605\u8005<\/strong>\u3002\u4ece<code>typesBySubscriber<\/code>\u4e2d\u79fb\u9664<strong>\u81ea\u5df1<\/strong>\uff0c\u4ece<code>subscriptionsByEventType<\/code>\u4e2d\u79fb\u9664<code>subscriber<\/code>\u5185\u7684<strong>\u591a\u4e2a\u4e8b\u4ef6<\/strong>\u7c7b\u578b\u3002<\/p>\n<p><strong>EventBus &#8211;&gt; unsubscribeByEventType()<\/strong><\/p>\n<pre><code class=\"language-java\">private void unsubscribeByEventType(Object subscriber, Class&lt;?&gt; eventType) {\n    \/\/ 6\u3001\u83b7\u53d6\u8be5\u4e8b\u4ef6\u7684\u6240\u6709\u8ba2\u9605\u8005\n    List&lt;Subscription&gt; subscriptions = subscriptionsByEventType.get(eventType);\n    if (subscriptions != null) {\n        int size = subscriptions.size();\n        \/\/ 7\u3001\u904d\u5386\u4e0a\u9762\u7684\u96c6\u5408\uff0c\u627e\u5230\u6b64\u8ba2\u9605\u8005\u7684Subscription\uff0c\u7136\u540e\u4ece\u6b64\u96c6\u5408\u4e2d\u79fb\u9664\u3002\n        for (int i = 0; i &lt; size; i++) {\n            Subscription subscription = subscriptions.get(i);\n            if (subscription.subscriber == subscriber) {\n                \/\/ 8\u3001\u627e\u5230\uff0c\u4ece\u6b64\u96c6\u5408\u4e2d\u79fb\u9664\u3002\n                subscription.active = false;\n                subscriptions.remove(i);\n                i--;\n                size--;\n            }\n        }\n    }\n}<\/code><\/pre>\n<p><code>unsubscribeByEventType()<\/code>\u65b9\u6cd5\uff0c\u4e3a\u4ece<code>subscriptionsByEventType<\/code>\u4e2d\u79fb\u9664<code>subscriber<\/code>\u5185\u7684<strong>\u5355\u4e2a\u4e8b\u4ef6<\/strong>\u7c7b\u578b\u3002<\/p>\n<blockquote>\n<p>\u8bf4\u660e\uff1a<\/p>\n<ol>\n<li>\u53ef\u4ee5<strong>\u5148\u904d\u5386<\/strong><code>subscriptionsByEventType<\/code>\uff0c\u7136\u540e<strong>\u518d\u904d\u5386<\/strong><code>subscriptionsByEventType<\/code>\u7684<strong>\u503c<\/strong>\uff0c\u6700\u540e<strong>\u5339\u914d<\/strong><code>subscriber<\/code>\uff0c\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u627e\u5230<strong>\u8981\u79fb\u9664<\/strong>\u7684<code>Subscription<\/code>\uff0c\u4f46\u662f<strong>\u8fd9\u79cd<\/strong>\u5b9e\u73b0\u8981\u6bd4<strong>\u4e0a\u9762<\/strong>\u5b9e\u73b0<strong>\u6548\u7387\u4f4e<\/strong>\u3002<\/li>\n<\/ol>\n<\/blockquote>\n<h3>\u5c0f\u7ed3<\/h3>\n<blockquote>\n<ol>\n<li><code>EventBus<\/code>\u7684<strong>\u6ce8\u9500<\/strong>\uff0c\u83b7\u53d6<strong>\u8ba2\u9605\u8005<\/strong>\u8ba2\u9605\u7684<strong>\u6240\u6709\u4e8b\u4ef6<\/strong>\uff0c<strong>\u904d\u5386<\/strong>\u6240\u6709\u4e8b\u4ef6\uff0c\u901a\u8fc7<strong>\u4e8b\u4ef6<\/strong>\u4ece<code>subscriptionsByEventType<\/code>\u4e2d\uff0c\u83b7\u53d6\u8be5<strong>\u4e8b\u4ef6<\/strong>\u7684<strong>\u6240\u6709<\/strong><code>Subscription<\/code>\uff0c\u904d\u5386<code>Subscription<\/code>\u96c6\u5408\uff0c\u627e\u5230<strong>\u6b64\u8ba2\u9605\u8005<\/strong>\u7684<code>Subscription<\/code>\uff0c\u7136\u540e\u4ece<strong>\u6b64\u96c6\u5408<\/strong>\u4e2d<strong>\u79fb\u9664<\/strong>\u3002<\/li>\n<li>\u5c06\u6b64<strong>\u8ba2\u9605\u8005<\/strong>\u4ece<code>typesBySubscriber<\/code>\u4e2d<strong>\u79fb\u9664<\/strong>\u3002<\/li>\n<\/ol>\n<\/blockquote>\n<h2>Poster<\/h2>\n<p><strong>Poster<\/strong><\/p>\n<pre><code class=\"language-java\">public interface Poster {\n\n    void enqueue(Subscription subscription, Object event);\n}<\/code><\/pre>\n<p><code>Poster<\/code>\u63a5\u53e3\uff0c\u5b83\u6709\u4e00\u4e2a<code>enqueue<\/code>\u65b9\u6cd5\uff0c\u5c06<code>subscription<\/code>\u3001<code>event<\/code>\u5165\u961f\u6267\u884c\u3002\u5b83\u67093\u4e2a<strong>\u5b9e\u73b0\u7c7b<\/strong>\uff0c\u4e3a<code>HandlerPoster<\/code>\u3001<code>BackgroundPoster<\/code>\u3001<code>AsyncPoster<\/code>\uff0c\u6211\u4eec\u6765\u5206\u522b\u770b\u4e00\u4e0b\u3002<\/p>\n<h3>mainThreadPoster<\/h3>\n<p><strong>HandlerPoster<\/strong><\/p>\n<pre><code class=\"language-java\">public class HandlerPoster extends Handler implements Poster {\n\n    private final PendingPostQueue queue;\n    private final int maxMillisInsideHandleMessage;\n    private final EventBus eventBus;\n    private boolean handlerActive;\n\n    public HandlerPoster(EventBus eventBus, Looper looper, int maxMillisInsideHandleMessage) {\n        \/\/ \u8fd9\u91cc\u4f20\u5165\u7684\u662f\u4e3b\u7ebf\u7a0b\u7684Looper\u5bf9\u8c61\uff0c\u6240\u4ee5\u8fd9\u4e2aHandler\u5bf9\u8c61\u662f\u4e3b\u7ebf\u7a0b\u7684Handler\n        super(looper);\n        this.eventBus = eventBus;\n        this.maxMillisInsideHandleMessage = maxMillisInsideHandleMessage;\n        \/\/ \u521b\u5efa\u4e00\u4e2a\u4e8b\u4ef6\u961f\u5217\n        queue = new PendingPostQueue();\n    }\n\n    public void enqueue(Subscription subscription, Object event) {\n        \/\/ \u6839\u636e\u4f20\u5165\u7684\u53c2\u6570\u5c01\u88c5\u4e00\u4e2aPendingPost\u5bf9\u8c61\n        PendingPost pendingPost = PendingPost.obtainPendingPost(subscription, event);\n        synchronized (this) {\n            \/\/ \u5c06PendingPost\u52a0\u5165\u5230\u961f\u5217\u4e2d\n            \/\/ \u53d1\u4e00\u4e2a\uff0c\u6267\u884c\u4e00\u4e2a\uff0c\u518d\u53d1\u9001\uff0c\u5982\u679c\u5df2\u7ecf\u6b63\u5728\u6267\u884c\u4e2d\uff0c\u5219\u4e0d\u518d\u7acb\u5373\u6267\u884c\uff0c\u800c\u662f\u548c\u4e0a\u6b21\u4e00\u5757\u6267\u884c\n            queue.enqueue(pendingPost);\n            if (!handlerActive) {\n                handlerActive = true;\n                \/\/ \u8c03\u7528Handler\u7684sendMessage\uff0c\u53d1\u9001\u4e8b\u4ef6\u56de\u5230\u4e3b\u7ebf\u7a0b\uff0c\u6700\u7ec8\u4f1a\u8c03\u7528\u4e0b\u9762\u7684handleMessage\u65b9\u6cd5\n                if (!sendMessage(obtainMessage())) {\n                    throw new EventBusException(&quot;Could not send handler message&quot;);\n                }\n            }\n        }\n    }\n\n    @Override\n    public void handleMessage(Message msg) {\n        boolean rescheduled = false;\n        try {\n            long started = SystemClock.uptimeMillis();\n            \/\/ \u65e0\u9650\u5faa\u73af\n            while (true) {\n                PendingPost pendingPost = queue.poll(); \/\/ \u83b7\u53d6\u5e76\u79fb\u9664\u7b2c\u4e00\u4e2a\n                \/\/ \u8fdb\u884c\u4e24\u6b21\u68c0\u67e5\uff0c\u786e\u4fddpendingPost\u4e0d\u4e3anull\uff0c\u5982\u679c\u4e3anull\u76f4\u63a5\u8df3\u51fa\u5faa\u73af\n                if (pendingPost == null) {\n                    synchronized (this) {\n                        \/\/ Check again, this time in synchronized\n                        pendingPost = queue.poll();\n                        if (pendingPost == null) {\n                            \/\/ \u4e00\u76f4\u5faa\u73af\u5230queue\u7ed3\u675f\uff0c\u624d\u8fd4\u56de\n                            handlerActive = false;\n                            return;\n                        }\n                    }\n                }\n                \/\/ \u4f7f\u7528\u53cd\u5c04\u7684\u65b9\u6cd5\u8c03\u7528\u8ba2\u9605\u8005\u7684\u8ba2\u9605\u65b9\u6cd5\u3002\n                eventBus.invokeSubscriber(pendingPost);\n                long timeInMethod = SystemClock.uptimeMillis() - started;\n                if (timeInMethod &gt;= maxMillisInsideHandleMessage) {\n                    if (!sendMessage(obtainMessage())) {\n                        throw new EventBusException(&quot;Could not send handler message&quot;);\n                    }\n                    rescheduled = true;\n                    return;\n                }\n            }\n        } finally {\n            handlerActive = rescheduled;\n        }\n    }\n}<\/code><\/pre>\n<p><code>HandlerPoster<\/code>\u7c7b\u7ee7\u627f<code>Handler<\/code>\uff0c\u5e76\u5b9e\u73b0<code>Poster<\/code>\u63a5\u53e3\uff0c<code>enqueue<\/code>\u65b9\u6cd5\u5c06<code>PendingPost<\/code>\u52a0\u5165\u5230<code>queue<\/code>\u961f\u5217\uff0c\u7136\u540e<strong>\u53d1\u9001\u7a7a\u6d88\u606f<\/strong>\uff0c\u6700\u7ec8\u8d70\u5230<code>handleMessage<\/code>\u65b9\u6cd5\u5185\uff0c\u5176\u5185\u90e8\u5224\u65ad\uff0c\u53ea\u8981<code>queue<\/code>\u961f\u5217<strong>\u6709\u6570\u636e<\/strong>\u5c31\u6267\u884c\uff0c\u6700\u540e\u4f7f\u7528<strong>\u53cd\u5c04<\/strong>\u8c03\u7528<strong>\u8ba2\u9605\u8005<\/strong>\u7684<strong>\u8ba2\u9605\u65b9\u6cd5<\/strong>\u3002<\/p>\n<h3>backgroundPoster<\/h3>\n<p><strong>BackgroundPoster<\/strong><\/p>\n<pre><code class=\"language-java\">final class BackgroundPoster implements Runnable, Poster {\n\n    private final PendingPostQueue queue;\n    private final EventBus eventBus;\n\n    private volatile boolean executorRunning;\n\n    BackgroundPoster(EventBus eventBus) {\n        this.eventBus = eventBus;\n        \/\/ \u521d\u59cb\u5316\u961f\u5217\n        queue = new PendingPostQueue();\n    }\n\n    public void enqueue(Subscription subscription, Object event) {\n        \/\/ \u5c01\u88c5PendingPost\u5bf9\u8c61\n        PendingPost pendingPost = PendingPost.obtainPendingPost(subscription, event);\n        synchronized (this) {\n            \/\/ \u5c06PendingPost\u5bf9\u8c61\u52a0\u5165\u5230\u961f\u5217\u4e2d\n            \/\/ \u53d1\u4e00\u4e2a\uff0c\u6267\u884c\u4e00\u4e2a\uff0c\u518d\u53d1\u9001\uff0c\u5982\u679c\u5df2\u7ecf\u6b63\u5728\u6267\u884c\u4e2d\uff0c\u5219\u4e0d\u518d\u7acb\u5373\u6267\u884c\uff0c\u800c\u662f\u548c\u4e0a\u6b21\u4e00\u5757\u6267\u884c\n            queue.enqueue(pendingPost);\n            if (!executorRunning) {\n                executorRunning = true;\n                \/\/ \u8fd9\u91cc\u4f7f\u7528\u5230\u4e4b\u524d\u521d\u59cb\u5316\u7684\u7ebf\u7a0b\u6c60\n                eventBus.getExecutorService().execute(this);\n            }\n        }\n    }\n\n    \/\/ \u7ebf\u7a0b\u6c60\u7684\u6267\u884c\u56de\u8c03\n    @Override\n    public void run() {\n        \/\/ \u5b9e\u73b0\u540cHandlerPoster\n        try {\n            try {\n                \/\/ \u65e0\u9650\u5faa\u73af\n                while (true) {\n                    \/\/\u3000\u83b7\u53d6\u961f\u5217\u4e2d\u7684PendingPost\uff0c\u8fdb\u884c\u53cc\u91cd\u68c0\u67e5\uff0c\u5982\u679c\u4e3anull\u76f4\u63a5\u8fd4\u56de\uff0c\u7ed3\u675f\u5faa\u73af\n                    PendingPost pendingPost = queue.poll(1000);\n                    if (pendingPost == null) {\n                        synchronized (this) {\n                            \/\/ Check again, this time in synchronized\n                            pendingPost = queue.poll();\n                            if (pendingPost == null) {\n                                \/\/ \u4e00\u76f4\u5faa\u73af\u5230queue\u7ed3\u675f\uff0c\u624d\u8fd4\u56de\n                                executorRunning = false;\n                                return;\n                            }\n                        }\n                    }\n                    \/\/\u4f7f\u7528\u53cd\u5c04\u7684\u65b9\u5f0f\u8c03\u7528\u8ba2\u9605\u8005\u7684\u8ba2\u9605\u65b9\u6cd5\n                    eventBus.invokeSubscriber(pendingPost);\n                }\n            } catch (InterruptedException e) {\n                eventBus.getLogger().log(Level.WARNING, Thread.currentThread().getName() + &quot; was interruppted&quot;, e);\n            }\n        } finally {\n            executorRunning = false;\n        }\n    }\n\n}<\/code><\/pre>\n<p><code>BackgroundPoster<\/code>\u7c7b\u5b9e\u73b0<code>Runnable<\/code>\u63a5\u53e3\uff0c\u5e76\u5b9e\u73b0<code>Poster<\/code>\u63a5\u53e3\uff0c<code>enqueue<\/code>\u65b9\u6cd5\u5c06<code>PendingPost<\/code>\u52a0\u5165\u5230<code>queue<\/code>\u961f\u5217\uff0c\u7136\u540e\u4f7f\u7528<strong>\u7ebf\u7a0b\u6c60<\/strong>\u6267\u884c\u6b64<code>Runnable<\/code>\uff0c\u6700\u7ec8\u8d70\u5230<code>run<\/code>\u65b9\u6cd5\u5185\uff0c\u5176\u5185\u90e8\u5224\u65ad\uff0c\u53ea\u8981<code>queue<\/code>\u961f\u5217<strong>\u6709\u6570\u636e<\/strong>\u5c31\u6267\u884c\uff0c\u6700\u540e\u4f7f\u7528<strong>\u53cd\u5c04<\/strong>\u8c03\u7528<strong>\u8ba2\u9605\u8005<\/strong>\u7684<strong>\u8ba2\u9605\u65b9\u6cd5<\/strong>\u3002<\/p>\n<h3>asyncPoster<\/h3>\n<p><strong>AsyncPoster<\/strong><\/p>\n<pre><code class=\"language-java\">class AsyncPoster implements Runnable, Poster {\n\n    private final PendingPostQueue queue;\n    private final EventBus eventBus;\n\n    AsyncPoster(EventBus eventBus) {\n        this.eventBus = eventBus;\n        queue = new PendingPostQueue();\n    }\n\n    public void enqueue(Subscription subscription, Object event) {\n        PendingPost pendingPost = PendingPost.obtainPendingPost(subscription, event);\n        \/\/ \u53d1\u4e00\u4e2a\uff0c\u6267\u884c\u4e00\u4e2a\n        queue.enqueue(pendingPost);\n        eventBus.getExecutorService().execute(this);\n    }\n\n    @Override\n    public void run() {\n        PendingPost pendingPost = queue.poll();\n        if(pendingPost == null) {\n            throw new IllegalStateException(&quot;No pending post available&quot;);\n        }\n        eventBus.invokeSubscriber(pendingPost);\n    }\n\n}<\/code><\/pre>\n<p><code>AsyncPoster<\/code>\u7c7b\u5b9e\u73b0<code>Runnable<\/code>\u63a5\u53e3\uff0c\u5e76\u5b9e\u73b0<code>Poster<\/code>\u63a5\u53e3\uff0c<code>enqueue<\/code>\u65b9\u6cd5\u5c06<code>PendingPost<\/code>\u52a0\u5165\u5230<code>queue<\/code>\u961f\u5217\uff0c\u7136\u540e\u4f7f\u7528<strong>\u7ebf\u7a0b\u6c60<\/strong>\u6267\u884c\u6b64<code>Runnable<\/code>\uff0c\u6700\u7ec8\u8d70\u5230<code>run<\/code>\u65b9\u6cd5\u5185\uff0c\u5176\u5185\u90e8\uff0c\u83b7\u53d6<code>queue<\/code>\u961f\u5217<strong>\u6700\u65b0\u6570\u636e<\/strong>\u7136\u540e<strong>\u6267\u884c<\/strong>\uff0c\u6700\u540e\u4f7f\u7528<strong>\u53cd\u5c04<\/strong>\u8c03\u7528<strong>\u8ba2\u9605\u8005<\/strong>\u7684<strong>\u8ba2\u9605\u65b9\u6cd5<\/strong>\u3002<\/p>\n<blockquote>\n<p>\u8bf4\u660e\uff1a<\/p>\n<ol>\n<li><code>BackgroundPoster<\/code>\u7c7b\u548c<code>AsyncPoster<\/code>\u7c7b<strong>\u7ebf\u7a0b\u6267\u884c<\/strong>\u533a\u522b\uff1a<code>BackgroundPoster<\/code>\u7c7b\uff0c\u5982\u679c<strong>\u6b63\u5728\u6267\u884c\u4e2d<\/strong>\uff0c\u5219\u7528<strong>\u4e0a\u4e00\u6b21\u7684\u7ebf\u7a0b<\/strong>\u6267\u884c\uff0c\u5426\u5219\u7528<strong>\u65b0\u7ebf\u7a0b<\/strong>\u6267\u884c\u3002<code>AsyncPoster<\/code>\u7c7b\uff0c\u59cb\u7ec8\u7528<strong>\u65b0\u7684\u7ebf\u7a0b<\/strong>\u6267\u884c\u3002<\/li>\n<\/ol>\n<\/blockquote>\n<h3>\u5c0f\u7ed3<\/h3>\n<blockquote>\n<ol>\n<li><code>Poster<\/code>\u63a5\u53e3\uff0c\u5b83\u67093\u4e2a\u5b9e\u73b0\u7c7b\uff0c\u4e3a<code>HandlerPoster<\/code>\u3001<code>BackgroundPoster<\/code>\u3001<code>AsyncPoster<\/code>\u3002<\/li>\n<li>\u8fd9\u51e0\u4e2a\u7c7b\uff0c<code>enqueue<\/code>\u65b9\u6cd5\u5c06<code>PendingPost<\/code>\uff08\u6301\u6709<code>subscription<\/code>\u5bf9\u8c61\u3001<code>event<\/code>\u5bf9\u8c61\uff09\u52a0\u5165\u5230<code>queue<\/code>\u961f\u5217\uff0c\u7136\u540e<strong>\u5207\u6362<\/strong>\u5230<strong>\u6307\u5b9a\u7684\u7ebf\u7a0b<\/strong>\u6267\u884c\uff0c\u6700\u540e\u4f7f\u7528<strong>\u53cd\u5c04<\/strong>\u8c03\u7528<strong>\u8ba2\u9605\u8005<\/strong>\u7684<strong>\u8ba2\u9605\u65b9\u6cd5<\/strong>\u3002<\/li>\n<\/ol>\n<\/blockquote>\n<h2>SubscriberMethodFinder<\/h2>\n<p><strong>SubscriberMethodFinder<\/strong><\/p>\n<pre><code class=\"language-java\">class SubscriberMethodFinder {\n    private static final int BRIDGE = 0x40;\n    private static final int SYNTHETIC = 0x1000;\n\n    private static final int MODIFIERS_IGNORE = Modifier.ABSTRACT | Modifier.STATIC | BRIDGE | SYNTHETIC;\n    private static final Map&lt;Class&lt;?&gt;, List&lt;SubscriberMethod&gt;&gt; METHOD_CACHE = new ConcurrentHashMap&lt;&gt;();\n\n    private List&lt;SubscriberInfoIndex&gt; subscriberInfoIndexes;\n    private final boolean strictMethodVerification;\n    private final boolean ignoreGeneratedIndex;\n\n    private static final int POOL_SIZE = 4;\n    private static final FindState[] FIND_STATE_POOL = new FindState[POOL_SIZE];\n\n    SubscriberMethodFinder(List&lt;SubscriberInfoIndex&gt; subscriberInfoIndexes, boolean strictMethodVerification,\n                           boolean ignoreGeneratedIndex) {\n        this.subscriberInfoIndexes = subscriberInfoIndexes;\n        this.strictMethodVerification = strictMethodVerification;\n        this.ignoreGeneratedIndex = ignoreGeneratedIndex;\n    }\n\n    List&lt;SubscriberMethod&gt; findSubscriberMethods(Class&lt;?&gt; subscriberClass) {\n        \/\/ 1\u3001\u5148\u4ece\u4e4b\u524d\u7f13\u5b58\u7684\u96c6\u5408\u4e2d\u83b7\u53d6\n        List&lt;SubscriberMethod&gt; subscriberMethods = METHOD_CACHE.get(subscriberClass);\n        if (subscriberMethods != null) {\n            \/\/ 2\u3001\u5982\u679c\u4e4b\u524d\u7f13\u5b58\u4e86\uff0c\u76f4\u63a5\u8fd4\u56de\n            return subscriberMethods;\n        }\n\n        if (ignoreGeneratedIndex) {\n            \/\/ \u5ffd\u7565\u6ce8\u89e3\u5904\u7406\u5668\u751f\u6210\u7684\u7d22\u5f15\uff0c\u6240\u4ee5\u76f4\u63a5\u7528\u53cd\u5c04\u83b7\u53d6\uff0cignoreGeneratedIndex\u9ed8\u8ba4\u4e3afalse\u3002\n            subscriberMethods = findUsingReflection(subscriberClass);\n        } else {\n            \/\/ 3\u3001\u83b7\u53d6\u6240\u6709\u8ba2\u9605\u65b9\u6cd5\u96c6\u5408\n            subscriberMethods = findUsingInfo(subscriberClass);\n        }\n        if (subscriberMethods.isEmpty()) {\n            throw new EventBusException(&quot;Subscriber &quot; + subscriberClass\n                    + &quot; and its super classes have no public methods with the @Subscribe annotation&quot;);\n        } else {\n            \/\/ 4\u3001\u653e\u5165\u7f13\u5b58\u96c6\u5408\u4e2d\n            METHOD_CACHE.put(subscriberClass, subscriberMethods);\n            return subscriberMethods;\n        }\n    }\n\n    private List&lt;SubscriberMethod&gt; findUsingInfo(Class&lt;?&gt; subscriberClass) {\n        \/\/ 5\u3001\u4ece\u6570\u7ec4\u4e2d\u83b7\u53d6FindState\u5bf9\u8c61\uff0c\u5982\u679c\u6709\u76f4\u63a5\u8fd4\u56de\uff0c\u5982\u679c\u6ca1\u6709\u521b\u5efa\u4e00\u4e2a\u65b0\u7684FindState\u5bf9\u8c61\u3002\n        FindState findState = prepareFindState();\n        \/\/ 6\u3001\u6839\u636e\u4e8b\u4ef6\u8ba2\u9605\u8005\u521d\u59cb\u5316findState\n        findState.initForSubscriber(subscriberClass);\n        while (findState.clazz != null) {\n            \/\/ 7\u3001\u83b7\u53d6subscriberInfo\uff0c\u521d\u59cb\u5316\u4e3anull\u3002\n            findState.subscriberInfo = getSubscriberInfo(findState);\n            if (findState.subscriberInfo != null) {\n                \/\/ \u901a\u8fc7\u6ce8\u89e3\u5904\u7406\u5668\u5904\u7406\n                \/\/ \u83b7\u53d6\u5230\u6240\u6709\u8ba2\u9605\u8005\u65b9\u6cd5\n                SubscriberMethod[] array = findState.subscriberInfo.getSubscriberMethods();\n                for (SubscriberMethod subscriberMethod : array) {\n                    if (findState.checkAdd(subscriberMethod.method, subscriberMethod.eventType)) {\n                        \/\/ \u901a\u8fc7\u68c0\u67e5\uff0c\u7ed9findState.subscriberMethods\u589e\u52a0\u8ba2\u9605\u8005\u65b9\u6cd5\n                        findState.subscriberMethods.add(subscriberMethod);\n                    }\n                }\n            } else {\n                \/\/ 8\u3001\u901a\u8fc7\u53cd\u5c04\u7684\u65b9\u5f0f\u83b7\u53d6\u8ba2\u9605\u8005\u4e2d\u7684Method\n                findUsingReflectionInSingleClass(findState);\n            }\n            \/\/ \u79fb\u52a8\u5230\u7236\u7c7b\uff0c\u4ee5\u4fbf\u540e\u7eed\u64cd\u4f5c\u3002\n            findState.moveToSuperclass();\n        }\n        \/\/ \u91ca\u653e\n        return getMethodsAndRelease(findState);\n    }\n\n    \/\/ \u4ecefindState\u4e2d\u83b7\u53d6\u8ba2\u9605\u8005\u6240\u6709\u65b9\u6cd5\u5e76\u91ca\u653e\n    private List&lt;SubscriberMethod&gt; getMethodsAndRelease(FindState findState) {\n        \/\/ \u83b7\u53d6\u8ba2\u9605\u8005\u6240\u6709\u8ba2\u9605\u65b9\u6cd5\u96c6\u5408\n        List&lt;SubscriberMethod&gt; subscriberMethods = new ArrayList&lt;&gt;(findState.subscriberMethods);\n        \/\/ findState\u8fdb\u884c\u56de\u6536\n        findState.recycle();\n        \/\/ \u5c06\u56de\u6536\u540e\u7684findState\uff0c\u6062\u590d\u5230FIND_STATE_POOL\u4e2d\u3002\n        synchronized (FIND_STATE_POOL) {\n            for (int i = 0; i &lt; POOL_SIZE; i++) {\n                if (FIND_STATE_POOL[i] == null) {\n                    FIND_STATE_POOL[i] = findState;\n                    break;\n                }\n            }\n        }\n        \/\/ \u8fd4\u56de\u96c6\u5408\n        return subscriberMethods;\n    }\n\n    \/\/ \u51c6\u5907FindState\u5bf9\u8c61\uff0c\u5148\u4ece\u7f13\u5b58\u4e2d\u83b7\u53d6\uff0c\u5982\u679c\u6709\u5219\u83b7\u53d6\u5e76\u5728\u7f13\u5b58\u4e2d\u79fb\u9664\uff1b\u5982\u679c\u6ca1\u6709\u5219\u8fdb\u884c\u521b\u5efa\u5e76\u8fd4\u56de\u3002\n    private FindState prepareFindState() {\n        synchronized (FIND_STATE_POOL) {\n            for (int i = 0; i &lt; POOL_SIZE; i++) {\n                FindState state = FIND_STATE_POOL[i];\n                if (state != null) {\n                    FIND_STATE_POOL[i] = null;\n                    return state;\n                }\n            }\n        }\n        return new FindState();\n    }\n\n    \/\/ \u83b7\u53d6\u8ba2\u9605\u8005\u4fe1\u606f\n    private SubscriberInfo getSubscriberInfo(FindState findState) {\n        \/\/ \u5904\u7406SubscriberInfoIndex\u6dfb\u52a0\u624b\u52a8\u521b\u5efa\u7684AbstractSubscriberInfo\uff08\u4e00\u822c\u4e0d\u4f1a\u624b\u52a8\u521b\u5efa\uff09\u3002\n        \/\/ -findState.subscriberInfo != null\uff1a\u8bf4\u660e\u8c03\u7528\u8005\u8c03\u7528findState.subscriberInfo = getSubscriberInfo(findState)\u7684getSubscriberInfo(findState)\u8fd4\u56de\u7684\u4e0d\u4e3anull\uff0c\u5373subscriberInfoIndexes\u6709\u503c\u3002\n        \/\/ -findState.subscriberInfo.getSuperSubscriberInfo() != null\uff1a\u8bf4\u660e\u624b\u52a8\u521b\u5efa\u7684AbstractSubscriberInfo\uff0c\u800c\u4e0d\u662f\u6ce8\u89e3\u5904\u7406\u5668\u751f\u6210\u6dfb\u52a0\u7684SimpleSubscriberInfo\u3002\n        if (findState.subscriberInfo != null &amp;&amp; findState.subscriberInfo.getSuperSubscriberInfo() != null) {\n            SubscriberInfo superclassInfo = findState.subscriberInfo.getSuperSubscriberInfo();\n            if (findState.clazz == superclassInfo.getSubscriberClass()) {\n                \/\/ \u8981\u83b7\u53d6\u7684Class\u7b49\u4e8e\u7236\u7c7b\u7684Class\uff0c\u5219\u8fd4\u56de\u7236\u7c7b\u7684Class\u3002\n                return superclassInfo;\n            }\n        }\n        \/\/ \u5904\u7406addIndex()\u6dfb\u52a0\u7684SubscriberInfoIndex\u3002\n        if (subscriberInfoIndexes != null) {\n            for (SubscriberInfoIndex index : subscriberInfoIndexes) {\n                SubscriberInfo info = index.getSubscriberInfo(findState.clazz);\n                if (info != null) {\n                    return info;\n                }\n            }\n        }\n        return null;\n    }\n\n    \/\/ \u901a\u8fc7\u53cd\u5c04\u83b7\u53d6\u8ba2\u9605\u8005\u65b9\u6cd5\n    private List&lt;SubscriberMethod&gt; findUsingReflection(Class&lt;?&gt; subscriberClass) {\n        \/\/ \u83b7\u53d6FindState\uff0c\u6709\u590d\u7528\u3002\n        FindState findState = prepareFindState();\n        \/\/ \u521d\u59cb\u5316FindState\uff0c\u56e0\u4e3a\u6709\u590d\u7528\uff0c\u6240\u4ee5\u5f97\u521d\u59cb\u5316\u3002\n        findState.initForSubscriber(subscriberClass);\n        while (findState.clazz != null) {\n            \/\/ \u53cd\u5c04\u83b7\u53d6\uff0c\u5982\u679c\u627e\u5230\u5219\u5b58\u5230findState.subscriberMethods\u91cc\u3002\n            findUsingReflectionInSingleClass(findState);\n            \/\/ \u79fb\u52a8\u5230\u7236\u7c7b\uff0c\u4f1a\u66f4\u6539findState.clazz\n            findState.moveToSuperclass();\n        }\n        return getMethodsAndRelease(findState);\n    }\n\n    private void findUsingReflectionInSingleClass(FindState findState) {\n        Method[] methods;\n        try {\n            \/\/ 9\u3001\u8ba2\u9605\u8005\u4e2d\u6240\u6709\u58f0\u660e\u7684\u65b9\u6cd5\uff0c\u653e\u5165\u6570\u7ec4\u4e2d\u3002\n            \/\/ getDeclaredMethods\uff0c\u83b7\u53d6\u7684\u662f\u5f53\u524d\u7c7b\u7684\u6240\u6709\u65b9\u6cd5\u3002\n            \/\/ getMethods\uff0c\u83b7\u53d6\u7684\u662f\u5f53\u524d\u7c7b\u53ca\u5176\u6240\u6709\u7236\u7c7b\u7684\u6240\u6709\u516c\u5171\u65b9\u6cd5\u3002\n            methods = findState.clazz.getDeclaredMethods();\n            \/\/ \u56e0\u4e3agetDeclaredMethods\uff0c\u83b7\u53d6\u7684\u662f\u5f53\u524d\u7c7b\u7684\u6240\u6709\u65b9\u6cd5\uff0c\u6240\u4ee5findState.skipSuperClasses\u4e3afalse\uff0c\u4e3a\u4e0d\u8df3\u8fc7\u7236\u7c7b\uff0c\u5373\u7ee7\u7eed\u67e5\u627e\u7236\u7c7b\u3002\n        } catch (Throwable th) {\n            \/\/ Workaround for java.lang.NoClassDefFoundError, see https:\/\/github.com\/greenrobot\/EventBus\/issues\/149\n            try {\n                \/\/ 10\u3001\u83b7\u53d6\u8ba2\u9605\u8005\u4e2d\u58f0\u660e\u7684public\u65b9\u6cd5\uff0c\u8bbe\u7f6e\u8df3\u8fc7\u7236\u7c7b\n                \/\/ \u56e0\u4e3agetMethods\uff0c\u83b7\u53d6\u7684\u662f\u5f53\u524d\u7c7b\u53ca\u5176\u6240\u6709\u7236\u7c7b\u7684\u6240\u6709\u516c\u5171\u65b9\u6cd5\uff0c\u6240\u4ee5findState.skipSuperClasses\u4e3atrue\uff0c\u4e3a\u8df3\u8fc7\u7236\u7c7b\uff0c\u5373\u4e0d\u67e5\u627e\u7236\u7c7b\u3002\n                methods = findState.clazz.getMethods();\n            } catch (LinkageError error) { \/\/ super class of NoClassDefFoundError to be a bit more broad...\n                String msg = &quot;Could not inspect methods of &quot; + findState.clazz.getName();\n                if (ignoreGeneratedIndex) {\n                    \/\/ \u5ffd\u7565\u6ce8\u89e3\u751f\u6210\u5668\u751f\u6210\u7684\u7d22\u5f15\uff0c\u63d0\u793a\uff1a\u8bf7\u8003\u8651\u4f7f\u7528EventBus\u6ce8\u91ca\u5904\u7406\u5668\u6765\u907f\u514d\u53cd\u5c04\u3002\n                    msg += &quot;. Please consider using EventBus annotation processor to avoid reflection.&quot;;\n                } else {\n                    \/\/ \u5ffd\u7565\u6ce8\u89e3\u751f\u6210\u5668\u751f\u6210\u7684\u7d22\u5f15\uff0c\u63d0\u793a\uff1a\u8bf7\u4f7f\u8fd9\u4e2a\u7c7b\u5bf9EventBus\u6ce8\u91ca\u5904\u7406\u5668\u53ef\u89c1\uff0c\u4ee5\u907f\u514d\u53cd\u5c04\u3002\n                    msg += &quot;. Please make this class visible to EventBus annotation processor to avoid reflection.&quot;;\n                }\n                throw new EventBusException(msg, error);\n            }\n            findState.skipSuperClasses = true;\n        }\n        \/\/ \u904d\u5386\u8fd9\u4e9b\u65b9\u6cd5\n        for (Method method : methods) {\n            \/\/ 11\u3001\u83b7\u53d6\u65b9\u6cd5\u7684\u4fee\u9970\u7b26:public\u3001private\u7b49\u7b49\n            int modifiers = method.getModifiers();\n            \/\/ 12\u3001\u8ba2\u9605\u65b9\u6cd5\u4e3apublic\u540c\u65f6\u4e0d\u662fabstract\u3001static\u3001bridge\u3001synthetic\u3002\n            if ((modifiers &amp; Modifier.PUBLIC) != 0 &amp;&amp; (modifiers &amp; MODIFIERS_IGNORE) == 0) {\n                \/\/ 13\u3001\u65b9\u6cd5\u53c2\u6570\u7c7b\u578b\u6570\u7ec4\n                Class&lt;?&gt;[] parameterTypes = method.getParameterTypes();\n                if (parameterTypes.length == 1) {\n                    \/\/ 14\u3001\u83b7\u53d6\u65b9\u6cd5\u7684\u6ce8\u89e3\n                    Subscribe subscribeAnnotation = method.getAnnotation(Subscribe.class);\n                    \/\/ 15\u3001\u5982\u679c\u6709\u6ce8\u89e3\n                    if (subscribeAnnotation != null) {\n                        Class&lt;?&gt; eventType = parameterTypes[0];\n                        \/\/ 16\u3001\u5c06method\u548ceventType\u653e\u5165\u5230findState\u8fdb\u884c\u68c0\u67e5\n                        if (findState.checkAdd(method, eventType)) {\n                            \/\/ 17\u3001\u83b7\u53d6\u6ce8\u89e3\u4e2d\u7684threadMode\u5bf9\u8c61\n                            ThreadMode threadMode = subscribeAnnotation.threadMode();\n                            \/\/ 18\u3001\u65b0\u5efa\u4e00\u4e2aSubscriberMethod\u5bf9\u8c61\uff0c\u540c\u65f6\u52a0\u5165\u5230findState\u4e2d\n                            findState.subscriberMethods.add(new SubscriberMethod(method, eventType, threadMode,\n                                    subscribeAnnotation.priority(), subscribeAnnotation.sticky()));\n                        }\n                    }\n                } else if (strictMethodVerification &amp;&amp; method.isAnnotationPresent(Subscribe.class)) {\n                    \/\/ \u975e1\u4e2a\u53c2\u6570\uff0c\u5e76\u4e14\u662f\u4e25\u683c\u6a21\u5f0f\uff0c\u5e76\u4e14\u65b9\u6cd5\u4e0a\u6709Subscribe\u6ce8\u89e3\uff0c\u5219\u629b\u5f02\u5e38\u8b66\u544a\u3002\n                    String methodName = method.getDeclaringClass().getName() + &quot;.&quot; + method.getName();\n                    throw new EventBusException(&quot;@Subscribe method &quot; + methodName +\n                            &quot;must have exactly 1 parameter but has &quot; + parameterTypes.length);\n                }\n            } else if (strictMethodVerification &amp;&amp; method.isAnnotationPresent(Subscribe.class)) {\n                \/\/ \u65b9\u6cd5\u7684\u4fee\u9970\u7b26\u4e0d\u7b26\u5408\uff0c\u5e76\u4e14\u662f\u4e25\u683c\u6a21\u5f0f\uff0c\u5e76\u4e14\u65b9\u6cd5\u4e0a\u6709Subscribe\u6ce8\u89e3\uff0c\u5219\u629b\u5f02\u5e38\u8b66\u544a\u3002\n                String methodName = method.getDeclaringClass().getName() + &quot;.&quot; + method.getName();\n                throw new EventBusException(methodName +\n                        &quot; is a illegal @Subscribe method: must be public, non-static, and non-abstract&quot;);\n            }\n        }\n    }\n}<\/code><\/pre>\n<p><code>SubscriberMethodFinder<\/code>\u7c7b<code>findSubscriberMethods<\/code>\u65b9\u6cd5\uff0c\u627e\u5230<strong>\u6b64\u8ba2\u9605\u8005<\/strong>\u7684<strong>\u6240\u6709\u8ba2\u9605\u65b9\u6cd5<\/strong>\u3002\u5b83\u5148\u4ece<strong>\u7f13\u5b58<\/strong>\u4e2d\u83b7\u53d6\uff0c\u5982\u679c<strong>\u7f13\u5b58<\/strong>\u4e2d\u6709\uff0c\u76f4\u63a5<strong>\u8fd4\u56de<\/strong>\uff1b\u5982\u679c<strong>\u7f13\u5b58<\/strong>\u4e2d\u6ca1\u6709\uff0c\u901a\u8fc7<strong>\u53cd\u5c04<\/strong>\u6216<strong>\u6ce8\u89e3\u5904\u7406\u5668<\/strong>\u7684\u65b9\u5f0f\u83b7\u53d6\u5230<strong>\u6b64\u8ba2\u9605\u8005<\/strong>\u7684<strong>\u6240\u6709\u8ba2\u9605\u65b9\u6cd5<\/strong>\uff0c\u5e76<strong>\u653e\u5165<\/strong>\u5230<strong>\u96c6\u5408<\/strong>\u4e2d\u8fdb\u884c<strong>\u8fd4\u56de<\/strong>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>EventBus\u6e90\u7801\u89e3\u6790\uff08\u4e8c\uff09 \u6e90\u7801\u7248\u672c\uff1a EventBus\uff1a3.3.1 EventBus\u7684post Even [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[25,10],"tags":[30,29,20],"class_list":["post-111","post","type-post","status-publish","format-standard","hentry","category-source-other","category-source","tag-eventbus","tag-29","tag-20"],"modified_by":"admin","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/www.devblog.cn\/index.php?rest_route=\/wp\/v2\/posts\/111","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.devblog.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.devblog.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.devblog.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.devblog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=111"}],"version-history":[{"count":3,"href":"http:\/\/www.devblog.cn\/index.php?rest_route=\/wp\/v2\/posts\/111\/revisions"}],"predecessor-version":[{"id":116,"href":"http:\/\/www.devblog.cn\/index.php?rest_route=\/wp\/v2\/posts\/111\/revisions\/116"}],"wp:attachment":[{"href":"http:\/\/www.devblog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.devblog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=111"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.devblog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}