{"id":113,"date":"2022-03-07T10:20:48","date_gmt":"2022-03-07T10:20:48","guid":{"rendered":"http:\/\/www.devblog.cn\/?p=113"},"modified":"2022-03-08T10:22:51","modified_gmt":"2022-03-08T10:22:51","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%b8%80%ef%bc%89","status":"publish","type":"post","link":"http:\/\/www.devblog.cn\/?p=113","title":{"rendered":"\u4e09\u65b9\u5e93-EventBus\u6e90\u7801\u89e3\u6790\uff08\u4e00\uff09"},"content":{"rendered":"<h1>EventBus\u6e90\u7801\u89e3\u6790\uff08\u4e00\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<h1>\u4f7f\u7528<\/h1>\n<h2>\u5b9a\u4e49\u4e8b\u4ef6<\/h2>\n<pre><code class=\"language-kotlin\">class MessageEvent(val message: String)<\/code><\/pre>\n<p><strong>\u4e8b\u4ef6<\/strong>\uff08<code>Event<\/code>\uff09\u662f\u666e\u901a\u7684\u5bf9\u8c61\uff0c\u6ca1\u6709\u4efb\u4f55\u7279\u5b9a\u8981\u6c42\u3002<\/p>\n<h2>\u8ba2\u9605\u65b9\u6cd5<\/h2>\n<pre><code class=\"language-kotlin\">@Subscribe(threadMode = ThreadMode.MAIN)\nfun onMessageEvent(event: MessageEvent) {\n    Toast.makeText(applicationContext, event.message, Toast.LENGTH_SHORT).show()\n}<\/code><\/pre>\n<p>\u521b\u5efa\u4e00\u4e2a\u63a5\u6536\u4e8b\u4ef6\u7684\u65b9\u6cd5<code>onMessageEvent<\/code>\u65b9\u6cd5\uff0c\u4f7f\u7528<strong>\u6ce8\u89e3<\/strong><code>@Subscribe<\/code>\u8fdb\u884c\u6807\u6ce8\uff0c\u540c\u65f6\u6807\u8bb0<strong>\u4e8b\u4ef6\u63a5\u6536\u7684\u7ebf\u7a0b<\/strong>\u4e3a<strong>\u4e3b\u7ebf\u7a0b<\/strong>\u3002<\/p>\n<h2>\u6ce8\u518c\u4e0e\u6ce8\u9500<\/h2>\n<pre><code class=\"language-kotlin\">override fun onStart() {\n    super.onStart()\n    EventBus.getDefault().register(this)\n}\n\noverride fun onStop() {\n    EventBus.getDefault().unregister(this)\n    super.onStop()\n}<\/code><\/pre>\n<p>\u5728\u8ba2\u9605\u8005\u5185\u8c03\u7528<code>EventBus.getDefault().register(this)<\/code>\u65b9\u6cd5\u8fdb\u884c<strong>\u6ce8\u518c<\/strong>\uff0c\u8c03\u7528<code>EventBus.getDefault().unregister(this)<\/code>\u65b9\u6cd5\u8fdb\u884c<strong>\u53cd\u6ce8\u518c<\/strong>\u3002<\/p>\n<p>\u5728<code>Android<\/code>\u4e2d\uff0c\u5728<code>Activity<\/code>\u548c<code>Fragment<\/code>\u4e2d\uff0c\u60a8\u901a\u5e38\u5e94\u8be5\u6839\u636e\u5b83\u4eec\u7684<strong>\u751f\u547d\u5468\u671f<\/strong>\u8fdb\u884c\u6ce8\u518c\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u60c5\u51b5\uff0c<code>onStart\/onStop<\/code> \u5de5\u4f5c\u6b63\u5e38\uff1a<\/p>\n<h2>\u53d1\u9001\u4e8b\u4ef6<\/h2>\n<h3>\u666e\u901a\u4e8b\u4ef6<\/h3>\n<pre><code class=\"language-kotlin\">EventBus.getDefault().post(MessageEvent(&quot;Hello everyone!&quot;))<\/code><\/pre>\n<h3>\u7c98\u6027\u4e8b\u4ef6<\/h3>\n<pre><code class=\"language-kotlin\">EventBus.getDefault().postSticky(MessageEvent(&quot;Hello everyone!&quot;))<\/code><\/pre>\n<h1>\u6e90\u7801<\/h1>\n<h2>Subscribe\u6ce8\u89e3<\/h2>\n<p><strong>Subscribe\u7c7b<\/strong><\/p>\n<pre><code class=\"language-java\">@Documented\n@Retention(RetentionPolicy.RUNTIME)\n@Target({ElementType.METHOD})\npublic @interface Subscribe {\n    \/\/ \u7ebf\u7a0b\u6a21\u578b\uff0c\u9ed8\u8ba4\u4e3aPOSTING\uff08\u53d1\u5e03\u7ebf\u7a0b\uff09\u3002\n    ThreadMode threadMode() default ThreadMode.POSTING;\n\n    \/\/ \u662f\u5426\u662f\u7c98\u6027\u4e8b\u4ef6\n    boolean sticky() default false;\n\n    \/\/ \u4f18\u5148\u7ea7\n    int priority() default 0;\n}<\/code><\/pre>\n<p><code>Subscribe<\/code>\u4e3a<strong>\u6ce8\u89e3<\/strong>\u7c7b\uff0c\u5176<strong>\u53ea\u80fd<\/strong>\u58f0\u660e\u5728<strong>\u65b9\u6cd5<\/strong>\u4e0a\uff0c\u5176\u67093\u4e2a\u53ef\u914d\u7f6e\u9009\u9879\u3002<\/p>\n<ul>\n<li><code>threadMode<\/code>\uff0c<strong>\u7ebf\u7a0b<\/strong>\u6a21\u578b\uff0c\u9ed8\u8ba4\u4e3a<code>POSTING<\/code>\uff08\u53d1\u5e03\u7ebf\u7a0b\uff09\u3002<\/li>\n<li><code>sticky<\/code>\uff0c\u662f\u5426\u662f<strong>\u7c98\u6027<\/strong>\u4e8b\u4ef6\uff0c\u9ed8\u8ba4\u4e3a<code>false<\/code>\u3002<\/li>\n<li><code>priority<\/code>\uff0c<strong>\u4f18\u5148\u7ea7<\/strong>\uff0c\u9ed8\u8ba4\u4e3a<strong>0<\/strong>\u3002<\/li>\n<\/ul>\n<p><strong>ThreadMode\u7c7b<\/strong><\/p>\n<pre><code class=\"language-java\">public enum ThreadMode {\n    POSTING,\n    MAIN,\n    MAIN_ORDERED,\n    BACKGROUND,\n    ASYNC\n}<\/code><\/pre>\n<p><code>ThreadMode<\/code>\u4e3a<strong>\u679a\u4e3e<\/strong>\u7c7b\uff0c\u4e3a<code>\u7ebf\u7a0b\u6a21\u578b<\/code>\uff0c\u4e00\u5171\u67095\u79cd\u3002<\/p>\n<ul>\n<li><code>POSTING<\/code>\uff0c<strong>\u53d1\u5e03\u7ebf\u7a0b<\/strong>\u3002<\/li>\n<li><code>MAIN<\/code>\uff0c<strong>\u4e3b\u7ebf\u7a0b<\/strong>\u3002<\/li>\n<li><code>MAIN_ORDERED<\/code>\uff0c<strong>\u4e3b\u7ebf\u7a0b\uff08\u6709\u5e8f\uff09<\/strong>\u3002<\/li>\n<li><code>BACKGROUND<\/code>\uff0c<strong>\u540e\u53f0\u7ebf\u7a0b<\/strong>\u3002<\/li>\n<li><code>POASYNCTING<\/code>\uff0c<strong>\u5f02\u6b65\u7ebf\u7a0b<\/strong>\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u91cc\u53ea\u662f\u7b80\u5355\u4ecb\u7ecd\uff0c\u8be6\u7ec6\u4ecb\u7ecd\u770b\u540e\u9762\u7684-<code>EventBus<\/code>\u7684<code>post<\/code>\u3002<\/p>\n<h2>EventBus\u7684\u521b\u5efa<\/h2>\n<h3>EventBus.getDefault()<\/h3>\n<p><strong>EventBus &#8211;&gt; getDefault\u65b9\u6cd5<\/strong><\/p>\n<pre><code class=\"language-java\">public class EventBus {\n    static volatile EventBus defaultInstance;\n    private static final EventBusBuilder DEFAULT_BUILDER = new EventBusBuilder();\n\n    \/\/ \u83b7\u53d6\u9ed8\u8ba4\u7684EventBus\u5bf9\u8c61\uff0c\u5355\u4f8b\u6a21\u5f0f\u3002\n    public static EventBus getDefault() {\n        EventBus instance = defaultInstance;\n        if (instance == null) {\n            synchronized (EventBus.class) {\n                instance = EventBus.defaultInstance;\n                if (instance == null) {\n                    \/\/ \u8c03\u7528\u65e0\u53c2\u6784\u9020\u65b9\u6cd5\uff0c\u521b\u5efaEventBus\u3002\n                    instance = EventBus.defaultInstance = new EventBus();\n                }\n            }\n        }\n        return instance;\n    }\n}<\/code><\/pre>\n<p><code>EventBus.getDefault()<\/code>\u65b9\u6cd5\uff0c\u4f7f\u7528<strong>\u5355\u4f8b\u6a21\u5f0f<\/strong>\u521b\u5efa<code>EventBus<\/code>\u5bf9\u8c61\uff0c<code>EventBus<\/code>\u7684\u521b\u5efa\u770b<code>new EventBus()<\/code>\u521b\u5efa\u65b9\u5f0f\u3002<\/p>\n<h3>new EventBus()<\/h3>\n<p><strong>EventBus &#8211;&gt; \u6784\u9020\u65b9\u6cd5<\/strong><\/p>\n<pre><code class=\"language-java\">public class EventBus {\n    private static final EventBusBuilder DEFAULT_BUILDER = new EventBusBuilder();\n\n    public EventBus() {\n        \/\/ \u8c03\u7528EventBusBuilder\u4e3a\u53c2\u7684\u6784\u9020\u65b9\u6cd5\uff0c\u4f20\u5165\u4e00\u4e2a\u9ed8\u8ba4\u7684EventBusBuilder\u5bf9\u8c61\u3002\n        this(DEFAULT_BUILDER);\n    }\n\n    EventBus(EventBusBuilder builder) {\n    \/\/ \u540e\u9762\u4ecb\u7ecd\uff0c\u770bEventBus\u7684\u521d\u59cb\u5316\u3002\n        ...\n    }\n}<\/code><\/pre>\n<p><code>new EventBus()<\/code>\uff0c\u4f7f\u7528\u7684\u662f<strong>\u9ed8\u8ba4<\/strong><code>EventBusBuilder<\/code>\u7684\u914d\u7f6e\uff0c<code>EventBusBuilder<\/code>\u7684\u521b\u5efa\u770b<code>EventBus.builder()<\/code>\u521b\u5efa\u65b9\u5f0f\u3002<\/p>\n<h3>EventBus.builder()<\/h3>\n<p><strong>EventBus &#8211;&gt; builder\u65b9\u6cd5<\/strong><\/p>\n<pre><code class=\"language-java\">public static EventBusBuilder builder() {\n    return new EventBusBuilder();\n}<\/code><\/pre>\n<p><code>EventBus.builder()<\/code>\u65b9\u6cd5\uff0c\u521b\u5efa<code>EventBusBuilder<\/code>\u5e76\u8fd4\u56de\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u770b\u4e00\u4e0b<code>EventBusBuilder<\/code>\u7c7b\u521b\u5efa<code>EventBus<\/code>\u7684\u65b9\u6cd5\u3002<\/p>\n<h4>installDefaultEventBus<\/h4>\n<p><strong>EventBusBuilder &#8211;&gt; installDefaultEventBus\u65b9\u6cd5<\/strong><\/p>\n<pre><code class=\"language-java\">\/\/ \u5b89\u88c5\u9ed8\u8ba4\u7684EventBus\npublic EventBus installDefaultEventBus() {\n    synchronized (EventBus.class) {\n        if (EventBus.defaultInstance != null) {\n            throw new EventBusException(&quot;Default instance already exists.&quot; +\n                    &quot; It may be only set once before it&#039;s used the first time to ensure consistent behavior.&quot;);\n        }\n        \/\/ \u8c03\u7528build\u65b9\u6cd5\u521b\u5efaEventBus\uff0c\u5e76\u7ed9\u9ed8\u8ba4\u5b9e\u4f8b\u8d4b\u503c\u3002\n        EventBus.defaultInstance = build();\n        \/\/ \u8fd4\u56de\u9ed8\u8ba4\u5b9e\u4f8b\u3002\n        return EventBus.defaultInstance;\n    }\n}<\/code><\/pre>\n<p><code>installDefaultEventBus()<\/code>\u65b9\u6cd5\uff0c\u901a\u8fc7<code>build<\/code>\u521b\u5efa<code>EventBus<\/code>\u5bf9\u8c61\uff0c\u5e76\u8d4b\u503c\u7ed9<code>EventBus.defaultInstance<\/code>\uff0c\u5e76\u8fd4\u56de\u5176<strong>\u5b9e\u4f8b<\/strong>\u3002<\/p>\n<blockquote>\n<p>\u8bf4\u660e\uff1a<\/p>\n<ol>\n<li><code>installDefaultEventBus()<\/code>\u65b9\u6cd5\uff0c\u5fc5\u987b\u5728<strong>\u7b2c\u4e00\u6b21<\/strong>\u8c03\u7528<code>EventBus.getDefault()<\/code><strong>\u4e4b\u524d<\/strong>\u8c03\u7528\uff0c\u5426\u5219\u629b\u5f02\u5e38\u3002<\/li>\n<li><code>installDefaultEventBus()<\/code>\u65b9\u6cd5\uff0c\u53ea\u80fd<strong>\u8c03\u7528\u4e00\u6b21<\/strong>\uff0c\u518d\u6b21\u8c03\u7528\u5219\u629b\u5f02\u5e38\u3002<\/li>\n<\/ol>\n<\/blockquote>\n<h4>build<\/h4>\n<p><strong>EventBusBuilder &#8211;&gt; build\u65b9\u6cd5<\/strong><\/p>\n<pre><code class=\"language-java\">\/\/ \u57fa\u4e8e\u5f53\u524d\u914d\u7f6e\u6784\u5efa\u4e00\u4e2aEventBus\u3002\npublic EventBus build() {\n    return new EventBus(this);\n}<\/code><\/pre>\n<p><code>build()<\/code>\u65b9\u6cd5\uff0c\u521b\u5efa<code>EventBus<\/code>\u5bf9\u8c61\uff0c\u5e76\u4f20\u5165\u5f53\u524d<code>EventBusBuilder<\/code>\u5b9e\u4f8b\uff0c\u6211\u4eec\u5148\u6765\u770b\u4e00\u4e0b<code>EventBusBuilder<\/code>\u7c7b\uff0c\u7136\u540e\u518d\u6765\u770b\u4e00\u4e0b<code>EventBus<\/code>\u7684\u521d\u59cb\u5316\u3002<\/p>\n<h3>\u5c0f\u7ed3<\/h3>\n<blockquote>\n<ol>\n<li><code>EventBus<\/code>\u53ef\u901a\u8fc73\u79cd\u65b9\u5f0f\u521b\u5efa\uff0c<code>EventBus.getDefault()<\/code>\u3001<code>new EventBus()<\/code>\u3001<code>EventBus.builder().build()<\/code>\uff0c\u5b83\u4eec\u6700\u7ec8\u90fd\u662f\u901a\u8fc7<code>EventBus(EventBusBuilder)<\/code>\u521b\u5efa\u3002<\/li>\n<\/ol>\n<\/blockquote>\n<h2>EventBusBuilder<\/h2>\n<p><code>EventBusBuilder<\/code>\u4e3a<strong>\u6784\u5efa\u8005\u6a21\u5f0f<\/strong>\u6784\u5efa<code>EventBus<\/code>\uff0c\u5176\u63d0\u4f9b\u4e86\u5168\u90e8<strong>\u53ef\u914d\u7f6e<\/strong>\u7684\u65b9\u6cd5\uff0c\u4ee5\u53ca\u4e00\u4e9b\u5176\u5b83<strong>\u83b7\u53d6<\/strong>\u7684\u65b9\u6cd5\uff0c\u6211\u4eec\u6765\u5206\u522b\u770b\u4e00\u4e0b\u3002<\/p>\n<h3>\u914d\u7f6e\u9879<\/h3>\n<p><strong>EventBusBuilder\u7c7b<\/strong><\/p>\n<pre><code class=\"language-java\">public class EventBusBuilder {\n    \/\/ \u9ed8\u8ba4\u7ebf\u7a0b\u6c60\n    private final static ExecutorService DEFAULT_EXECUTOR_SERVICE = Executors.newCachedThreadPool();\n\n    boolean logSubscriberExceptions = true;\n    boolean logNoSubscriberMessages = true;\n    boolean sendSubscriberExceptionEvent = true;\n    boolean sendNoSubscriberEvent = true;\n    boolean throwSubscriberException;\n    boolean eventInheritance = true;\n    boolean ignoreGeneratedIndex;\n    boolean strictMethodVerification;\n    ExecutorService executorService = DEFAULT_EXECUTOR_SERVICE;\n    List&lt;Class&lt;?&gt;&gt; skipMethodVerificationForClasses;\n    List&lt;SubscriberInfoIndex&gt; subscriberInfoIndexes;\n    Logger logger;\n    MainThreadSupport mainThreadSupport;\n\n    EventBusBuilder() {\n    }\n\n    \/\/ \u8c03\u7528\u8ba2\u9605\u65b9\u6cd5\u5f02\u5e38\u65f6\uff0c\u662f\u5426\u6253\u5370\u5f02\u5e38\u4fe1\u606f\uff0c\u9ed8\u8ba4\u4e3atrue\u3002\n    public EventBusBuilder logSubscriberExceptions(boolean logSubscriberExceptions) {\n        this.logSubscriberExceptions = logSubscriberExceptions;\n        return this;\n    }\n\n    \/\/ \u6ca1\u6709\u8ba2\u9605\u8005\u65f6\uff0c\u662f\u5426\u6253\u5370\u5f02\u5e38\u4fe1\u606f\uff0c\u9ed8\u8ba4\u4e3atrue\u3002\n    public EventBusBuilder logNoSubscriberMessages(boolean logNoSubscriberMessages) {\n        this.logNoSubscriberMessages = logNoSubscriberMessages;\n        return this;\n    }\n\n    \/\/ \u8c03\u7528\u8ba2\u9605\u65b9\u6cd5\u5f02\u5e38\u65f6\uff0c\u662f\u5426\u53d1\u9001SubscriberExceptionEvent\u4e8b\u4ef6\uff0c\u9ed8\u8ba4\u4e3atrue\u3002\n    public EventBusBuilder sendSubscriberExceptionEvent(boolean sendSubscriberExceptionEvent) {\n        this.sendSubscriberExceptionEvent = sendSubscriberExceptionEvent;\n        return this;\n    }\n\n    \/\/ \u6ca1\u6709\u8ba2\u9605\u8005\u65f6\uff0c\u662f\u5426\u53d1\u9001NoSubscriberEvent\u4e8b\u4ef6\uff0c\u9ed8\u8ba4\u4e3atrue\u3002\n    public EventBusBuilder sendNoSubscriberEvent(boolean sendNoSubscriberEvent) {\n        this.sendNoSubscriberEvent = sendNoSubscriberEvent;\n        return this;\n    }\n\n    \/\/ \u8c03\u7528\u8ba2\u9605\u65b9\u6cd5\u5f02\u5e38\u65f6\uff0c\u662f\u5426\u629b\u51faSubscriberException\u5f02\u5e38\uff0c\u9ed8\u8ba4\u4e3afalse\u3002\n    public EventBusBuilder throwSubscriberException(boolean throwSubscriberException) {\n        this.throwSubscriberException = throwSubscriberException;\n        return this;\n    }\n\n    \/\/ \u4e8b\u4ef6\u662f\u5426\u6709\u7ee7\u627f\u6027\uff0c\u9ed8\u8ba4\u4e3atrue\u3002\n    public EventBusBuilder eventInheritance(boolean eventInheritance) {\n        this.eventInheritance = eventInheritance;\n        return this;\n    }\n\n    \/\/ \u4e3aEventBus\u63d0\u4f9b\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7ebf\u7a0b\u6c60\uff0c\u7528\u4e8e\u5f02\u6b65\u548c\u540e\u53f0\u4e8b\u4ef6\u4f20\u9012\u3002\n    public EventBusBuilder executorService(ExecutorService executorService) {\n        this.executorService = executorService;\n        return this;\n    }\n\n    \/\/ \u8df3\u8fc7\u65b9\u6cd5\u7b7e\u540d\u9a8c\u8bc1\uff08\u76ee\u524d\u672a\u4f7f\u7528\uff09\n    public EventBusBuilder skipMethodVerificationFor(Class&lt;?&gt; clazz) {\n        if (skipMethodVerificationForClasses == null) {\n            skipMethodVerificationForClasses = new ArrayList&lt;&gt;();\n        }\n        skipMethodVerificationForClasses.add(clazz);\n        return this;\n    }\n\n    \/\/ \u662f\u5426\u5ffd\u7565\u6ce8\u89e3\u5904\u7406\u5668\u751f\u6210\u7684\u7d22\u5f15\uff0c\u9ed8\u8ba4\u4e3afalse\u3002\n    public EventBusBuilder ignoreGeneratedIndex(boolean ignoreGeneratedIndex) {\n        this.ignoreGeneratedIndex = ignoreGeneratedIndex;\n        return this;\n    }\n\n    \/\/ \u662f\u5426\u4e25\u683c\u9a8c\u8bc1\u8ba2\u9605\u65b9\u6cd5\uff0c\u9ed8\u8ba4\u4e3afalse\u3002\n    public EventBusBuilder strictMethodVerification(boolean strictMethodVerification) {\n        this.strictMethodVerification = strictMethodVerification;\n        return this;\n    }\n\n    \/\/ \u6dfb\u52a0\u6ce8\u89e3\u5904\u7406\u5668\u751f\u6210\u7684\u7d22\u5f15\n    public EventBusBuilder addIndex(SubscriberInfoIndex index) {\n        if (subscriberInfoIndexes == null) {\n            subscriberInfoIndexes = new ArrayList&lt;&gt;();\n        }\n        subscriberInfoIndexes.add(index);\n        return this;\n    }\n\n    \/\/ \u8bbe\u7f6e\u65e5\u5fd7\u5904\u7406\u8005\n    public EventBusBuilder logger(Logger logger) {\n        this.logger = logger;\n        return this;\n    }\n}<\/code><\/pre>\n<p><strong>\u914d\u7f6e\u9879<\/strong>\u7684<strong>\u9ed8\u8ba4\u503c<\/strong>\uff0c\u4e3a<strong>\u5c5e\u6027<\/strong>\u7684<strong>\u9ed8\u8ba4\u503c<\/strong>\u3002<\/p>\n<h4>1\u3001\u5f02\u5e38\u5904\u7406\u914d\u7f6e<\/h4>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">\u914d\u7f6e\u9879<\/th>\n<th style=\"text-align: left;\">\u63cf\u8ff0<\/th>\n<th style=\"text-align: center;\">\u9ed8\u8ba4\u503c<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\"><strong>logSubscriberExceptions<\/strong><\/td>\n<td style=\"text-align: left;\"><strong>\u8c03\u7528\u8ba2\u9605\u65b9\u6cd5\u5f02\u5e38\u65f6<\/strong>\uff0c\u662f\u5426\u6253\u5370\u5f02\u5e38\u4fe1\u606f<\/td>\n<td style=\"text-align: center;\"><strong>true<\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\"><strong>sendSubscriberExceptionEvent<\/strong><\/td>\n<td style=\"text-align: left;\"><strong>\u8c03\u7528\u8ba2\u9605\u65b9\u6cd5\u5f02\u5e38\u65f6<\/strong>\uff0c\u662f\u5426\u53d1\u9001<code>SubscriberExceptionEvent<\/code>\u4e8b\u4ef6<\/td>\n<td style=\"text-align: center;\"><strong>true<\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\"><strong>throwSubscriberException<\/strong><\/td>\n<td style=\"text-align: left;\"><strong>\u8c03\u7528\u8ba2\u9605\u65b9\u6cd5\u5f02\u5e38\u65f6<\/strong>\uff0c\u662f\u5426\u629b\u51fa<code>SubscriberException<\/code>\u5f02\u5e38<\/td>\n<td style=\"text-align: center;\"><strong>false<\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\"><strong>logNoSubscriberMessages<\/strong><\/td>\n<td style=\"text-align: left;\"><strong>\u6ca1\u6709\u8ba2\u9605\u8005\u65f6<\/strong>\uff0c\u662f\u5426\u6253\u5370\u5f02\u5e38\u4fe1\u606f<\/td>\n<td style=\"text-align: center;\"><strong>true<\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\"><strong>sendSubscriberExceptionEvent<\/strong><\/td>\n<td style=\"text-align: left;\"><strong>\u6ca1\u6709\u8ba2\u9605\u8005\u65f6<\/strong>\uff0c\u662f\u5426\u53d1\u9001<code>NoSubscriberEvent<\/code>\u4e8b\u4ef6<\/td>\n<td style=\"text-align: center;\"><strong>true<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>2\u3001\u6ce8\u89e3\u5904\u7406\u5668\u914d\u7f6e<\/h4>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">\u914d\u7f6e\u9879<\/th>\n<th style=\"text-align: center;\">\u63cf\u8ff0<\/th>\n<th style=\"text-align: center;\">\u9ed8\u8ba4\u503c<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\"><strong>ignoreGeneratedIndex<\/strong><\/td>\n<td style=\"text-align: center;\">\u662f\u5426\u5ffd\u7565<strong>\u6ce8\u89e3\u5904\u7406\u5668<\/strong>\u751f\u6210\u7684\u7d22\u5f15<\/td>\n<td style=\"text-align: center;\"><strong>false<\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\"><strong>addIndex<\/strong><\/td>\n<td style=\"text-align: center;\">\u6dfb\u52a0<strong>\u6ce8\u89e3\u5904\u7406\u5668<\/strong>\u751f\u6210\u7684\u7d22\u5f15<\/td>\n<td style=\"text-align: center;\"><strong>\u65e0<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>3\u3001\u5176\u5b83\u914d\u7f6e<\/h4>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">\u914d\u7f6e\u9879<\/th>\n<th style=\"text-align: center;\">\u63cf\u8ff0<\/th>\n<th style=\"text-align: center;\">\u9ed8\u8ba4\u503c<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\"><strong>eventInheritance<\/strong><\/td>\n<td style=\"text-align: center;\">\u4e8b\u4ef6\u662f\u5426\u6709<strong>\u7ee7\u627f\u6027<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>true<\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\"><strong>executorService(ExecutorService)<\/strong><\/td>\n<td style=\"text-align: center;\">\u4e3a<code>EventBus<\/code>\u63d0\u4f9b\u4e00\u4e2a<strong>\u81ea\u5b9a\u4e49\u7ebf\u7a0b\u6c60<\/strong>\uff0c\u7528\u4e8e\u5f02\u6b65\u548c\u540e\u53f0\u4e8b\u4ef6\u4f20\u9012\u3002<\/td>\n<td style=\"text-align: center;\"><strong>Executors.newCachedThreadPool()<\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\"><strong>strictMethodVerification<\/strong><\/td>\n<td style=\"text-align: center;\">\u662f\u5426<strong>\u4e25\u683c\u9a8c\u8bc1<\/strong>\u8ba2\u9605\u65b9\u6cd5<\/td>\n<td style=\"text-align: center;\"><strong>false<\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\"><strong>logger<\/strong><\/td>\n<td style=\"text-align: center;\">\u8bbe\u7f6e<strong>\u65e5\u5fd7\u5904\u7406\u8005<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>\u65e0<\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\"><strong>skipMethodVerificationFor(Class&lt;?&gt;)<\/strong><\/td>\n<td style=\"text-align: center;\">\u8df3\u8fc7\u65b9\u6cd5\u9a8c\u8bc1\uff08<strong>\u76ee\u524d\u672a\u4f7f\u7528<\/strong>\uff09<\/td>\n<td style=\"text-align: center;\"><strong>\u65e0<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>getLogger<\/h3>\n<p><strong>EventBusBuilder &#8211;&gt; getLogger\u65b9\u6cd5<\/strong><\/p>\n<pre><code class=\"language-java\">\/\/ \u83b7\u53d6\u65e5\u5fd7\u5904\u7406\u8005\nLogger getLogger() {\n    if (logger != null) {\n        return logger;\n    } else {\n        return Logger.Default.get();\n    }\n}<\/code><\/pre>\n<p><code>getLogger()<\/code>\u65b9\u6cd5\uff0c\u5982\u679c\u6709\u8c03\u7528<code>logger()<\/code>\u65b9\u6cd5\u8bbe\u7f6e\u8fc7<code>Logger<\/code>\uff0c\u5219\u7528\u8bbe\u7f6e\u8fc7\u7684<code>Logger<\/code>\uff0c\u5426\u5219\u7528<strong>\u9ed8\u8ba4<\/strong>\u7684<code>Logger.Default.get()<\/code>\u7684<code>Logger<\/code>\uff0c\u6211\u4eec\u6765\u770b\u4e00\u4e0b<code>Logger<\/code>\u7c7b\u3002<\/p>\n<p><strong>Logger\u7c7b<\/strong><\/p>\n<pre><code class=\"language-java\">public interface Logger {\n\n    void log(Level level, String msg);\n\n    void log(Level level, String msg, Throwable th);\n\n    class JavaLogger implements Logger {\n        protected final java.util.logging.Logger logger;\n\n        public JavaLogger(String tag) {\n            logger = java.util.logging.Logger.getLogger(tag);\n        }\n\n        @Override\n        public void log(Level level, String msg) {\n            \/\/ TODO Replace logged method with caller method\n            logger.log(level, msg);\n        }\n\n        @Override\n        public void log(Level level, String msg, Throwable th) {\n            \/\/ TODO Replace logged method with caller method\n            logger.log(level, msg, th);\n        }\n\n    }\n\n    class SystemOutLogger implements Logger {\n\n        @Override\n        public void log(Level level, String msg) {\n            System.out.println(&quot;[&quot; + level + &quot;] &quot; + msg);\n        }\n\n        @Override\n        public void log(Level level, String msg, Throwable th) {\n            System.out.println(&quot;[&quot; + level + &quot;] &quot; + msg);\n            th.printStackTrace(System.out);\n        }\n\n    }\n\n    class Default {\n        public static Logger get() {\n        \/\/ \u5224\u65adAndroidComponents\u662f\u5426\u53ef\u7528\uff0c\u5982\u679c\u53ef\u7528\u5219\u7528AndroidComponents\u7684logger\u3002\n            if (AndroidComponents.areAvailable()) {\n                return AndroidComponents.get().logger;\n            }\n\n            return new SystemOutLogger();\n        }\n    }\n\n}<\/code><\/pre>\n<p><code>Logger<\/code>\u7c7b\uff0c\u4e3a<strong>\u65e5\u5fd7\u5904\u7406\u8005<\/strong>\uff0c\u662f\u4e00\u4e2a\u63a5\u53e3\uff0c\u5b83\u6709\u4e24\u4e2a<code>log()<\/code>\u65b9\u6cd5\uff0c<code>JavaLogger<\/code>\u3001<code>SystemOutLogger<\/code>\u4e3a\u5176<strong>\u5b9e\u73b0\u7c7b<\/strong>\u3002<\/p>\n<p><code>Logger.Default.get()<\/code>\u65b9\u6cd5\uff0c\u901a\u8fc7<code>AndroidComponents.areAvailable()<\/code>\u65b9\u6cd5\uff0c\u5224\u65ad<code>AndroidComponents<\/code>\u662f\u5426\u53ef\u7528\uff0c\u5982\u679c\u53ef\u7528\u5219\u7528<code>AndroidComponents<\/code>\u5b9e\u73b0\u7c7b\u7684<code>logger<\/code>\uff0c\u5426\u5219\u7528<code>SystemOutLogger<\/code>\uff0c\u6211\u4eec\u6765\u770b\u4e00\u4e0b<code>AndroidComponents<\/code>\u7c7b\u3002<\/p>\n<p><strong>AndroidComponents\u7c7b<\/strong><\/p>\n<pre><code class=\"language-java\">public abstract class AndroidComponents {\n    \/\/ AndroidComponents\u7c7b\u5b9e\u73b0\u8005\n    private static final AndroidComponents implementation;\n\n    static {\n        \/\/ \u5b9e\u73b0\u8005\u5b9e\u4f8b\uff0c\u5982\u679cAndroid SDK\u53ef\u7528\uff0c\u5219\u521b\u5efaAndroidComponents\u5b9e\u73b0\u8005\u5b9e\u4f8b\u5e76\u8d4b\u503c\u7ed9\u5b83\uff0c\u5426\u5219\u8d4b\u503c\u4e3anull\u3002\n        implementation = AndroidDependenciesDetector.isAndroidSDKAvailable()\n            ? AndroidDependenciesDetector.instantiateAndroidComponents()\n            : null;\n    }\n\n    \/\/ AndroidComponents\u662f\u5426\u53ef\u7528\n    public static boolean areAvailable() {\n        return implementation != null;\n    }\n\n    \/\/ \u83b7\u53d6AndroidComponents\u5b9e\u73b0\u7c7b\u5b9e\u4f8b\n    public static AndroidComponents get() {\n        return implementation;\n    }\n\n    public final Logger logger;\n    public final MainThreadSupport defaultMainThreadSupport;\n\n    \/\/ \u521b\u5efaAndroidComponents\uff0c\u9700\u8981Logger\u3001MainThreadSupport\u3002\n    public AndroidComponents(Logger logger, MainThreadSupport defaultMainThreadSupport) {\n        this.logger = logger;\n        this.defaultMainThreadSupport = defaultMainThreadSupport;\n    }\n}<\/code><\/pre>\n<p><code>AndroidComponents<\/code>\u7c7b\uff0c\u4e3a<code>Android<\/code><strong>\u7ec4\u4ef6<\/strong>\uff0c\u5b83\u6301\u6709<code>Logger<\/code>\u3001<code>MainThreadSupport<\/code>\uff0c\u4ee5\u53ca\u542b\u6709<code>areAvailable()<\/code>\u3001<code>get()<\/code>\u4e24\u4e2a<strong>\u9759\u6001<\/strong>\u65b9\u6cd5\u3002<\/p>\n<p><code>implementation<\/code>\u5c5e\u6027\u4e3a<code>AndroidComponents<\/code>\u7c7b<strong>\u5b9e\u73b0\u7c7b<\/strong>\u5b9e\u4f8b\uff0c\u901a\u8fc7<code>AndroidDependenciesDetector<\/code>\u5224\u65ad\uff0c\u5982\u679c<code>Android SDK<\/code>\u53ef\u7528\uff0c\u5219\u521b\u5efa<code>AndroidComponents<\/code><strong>\u5b9e\u73b0\u7c7b<\/strong>\u5b9e\u4f8b\u5e76\u8d4b\u503c\u7ed9\u5b83\uff0c\u5426\u5219\u8d4b\u503c\u4e3a<code>null<\/code>\uff0c\u6211\u4eec\u6765\u770b\u4e00\u4e0b<code>AndroidDependenciesDetector<\/code>\u7c7b\u3002<\/p>\n<p><strong>AndroidDependenciesDetector\u7c7b<\/strong><\/p>\n<pre><code class=\"language-java\">public class AndroidDependenciesDetector {\n\n    \/\/ \u5224\u65adAndroid SDK\u662f\u5426\u662f\u53ef\u7528\u7684\uff0c\u5176\u5185\u90e8\u5224\u65ad\u4e3aLooper.getMainLooper()\u662f\u5426\u6709\u503c\u3002\n    public static boolean isAndroidSDKAvailable() {\n\n        try {\n            \/\/ \u53cd\u5c04\u83b7\u53d6Looper\u7c7bClass\n            Class&lt;?&gt; looperClass = Class.forName(&quot;android.os.Looper&quot;);\n            \/\/ \u53cd\u5c04\u83b7\u53d6Looper\u7c7bgetMainLooper\u65b9\u6cd5Method\n            Method getMainLooper = looperClass.getDeclaredMethod(&quot;getMainLooper&quot;);\n            \/\/ \u53cd\u5c04\u83b7\u53d6Looper\u7c7bgetMainLooper\u65b9\u6cd5\u8fd4\u56de\u503c\n            Object mainLooper = getMainLooper.invoke(null);\n            \/\/ \u5224\u65adLooper\u7c7bgetMainLooper\u65b9\u6cd5\u8fd4\u56de\u503c\u662f\u5426\u4e3a\u7a7a\n            return mainLooper != null;\n        }\n        catch (ClassNotFoundException ignored) {}\n        catch (NoSuchMethodException ignored) {}\n        catch (IllegalAccessException ignored) {}\n        catch (InvocationTargetException ignored) {}\n\n        return false;\n    }\n\n    private static final String ANDROID_COMPONENTS_IMPLEMENTATION_CLASS_NAME = &quot;org.greenrobot.eventbus.android.AndroidComponentsImpl&quot;;\n\n    \/\/ \u5224\u65adAndroidComponents\u662f\u5426\u662f\u53ef\u7528\u7684\uff0c\u5176\u5185\u90e8\u5224\u65ad\u4e3a\u662f\u5426\u6709AndroidComponentsImpl\u7c7b\u3002\n    public static boolean areAndroidComponentsAvailable() {\n\n        try {\n            Class.forName(ANDROID_COMPONENTS_IMPLEMENTATION_CLASS_NAME);\n            return true;\n        }\n        catch (ClassNotFoundException ex) {\n            return false;\n        }\n    }\n\n    \/\/ \u521b\u5efaAndroidComponents\u5b9e\u73b0\u7c7b\uff0c\u5176\u5185\u90e8\u4e3a\u53cd\u5c04\u521b\u5efaAndroidComponentsImpl\u7c7b\u3002\n    public static AndroidComponents instantiateAndroidComponents() {\n\n        try {\n            Class&lt;?&gt; impl = Class.forName(ANDROID_COMPONENTS_IMPLEMENTATION_CLASS_NAME);\n            return (AndroidComponents) impl.getConstructor().newInstance();\n        }\n        catch (Throwable ex) {\n            return null;\n        }\n    }\n}<\/code><\/pre>\n<p><code>AndroidDependenciesDetector<\/code>\u7c7b\uff0c\u5b83\u4e3a<code>Android<\/code><strong>\u4f9d\u8d56\u53d1\u73b0\u8005<\/strong>\uff0c\u5b83\u542b\u6709\u4ee5\u4e0b3\u4e2a<strong>\u9759\u6001\u65b9\u6cd5<\/strong>\u3002<\/p>\n<ul>\n<li><code>isAndroidSDKAvailable()<\/code>\uff0c\u5224\u65ad<code>Android SDK<\/code>\u662f\u5426\u662f\u53ef\u7528\u7684\uff0c\u5176\u5185\u90e8\u5224\u65ad\u4e3a<code>Looper.getMainLooper()<\/code>\u662f\u5426\u6709\u503c\u3002<\/li>\n<li><code>areAndroidComponentsAvailable()<\/code>\uff0c\u5224\u65ad<code>AndroidComponents<\/code>\u662f\u5426\u662f\u53ef\u7528\u7684\uff0c\u5176\u5185\u90e8\u5224\u65ad\u4e3a\u662f\u5426\u6709<code>AndroidComponentsImpl<\/code>\u7c7b\u3002<\/li>\n<li><code>instantiateAndroidComponents()<\/code>\uff0c\u521b\u5efa<code>AndroidComponents<\/code>\u5b9e\u73b0\u7c7b\uff0c\u5176\u5185\u90e8\u4e3a\u53cd\u5c04\u521b\u5efa<code>AndroidComponentsImpl<\/code>\u7c7b\u3002<\/li>\n<\/ul>\n<blockquote>\n<p>\u8bf4\u660e\uff1a<\/p>\n<ol>\n<li><code>isAndroidSDKAvailable()<\/code>\uff0c\u901a\u8fc7\u5b83\u53ef\u4ee5\u5224\u65ad\u662f\u5426\u662f\u5728<code>Android<\/code>\u5e73\u53f0\u3002<\/li>\n<li><code>areAndroidComponentsAvailable()<\/code>\uff0c\u901a\u8fc7\u5b83\u53ef\u4ee5\u5224\u65ad\u662f\u5426\u662f\u53ea\u5f15\u7528\u4e86<code>eventbus-java<\/code>\u5e93\uff0c\u672a\u5f15\u7528\u4e86<code>eventbus<\/code>\u5e93\uff08\u8bf4\u660e\uff1a<code>eventbus-java<\/code>\u5e93\u4e3a<code>Java<\/code>\u5e93\uff0c<code>eventbus<\/code>\u5e93\u4e3a<code>Android<\/code>\u5e93\uff0c\u5f15\u7528<code>eventbus<\/code>\u9ed8\u8ba4\u4f1a\u5f15\u7528<code>eventbus-java<\/code>\uff09\u3002<\/li>\n<\/ol>\n<\/blockquote>\n<p>\u6211\u4eec\u518d\u6765\u770b\u4e00\u4e0b<code>AndroidComponentsImpl<\/code>\u7c7b\u3002<\/p>\n<p><strong>AndroidComponentsImpl\u7c7b<\/strong><\/p>\n<pre><code class=\"language-java\">public class AndroidComponentsImpl extends AndroidComponents {\n\n    public AndroidComponentsImpl() {\n        super(new AndroidLogger(&quot;EventBus&quot;), new DefaultAndroidMainThreadSupport());\n    }\n}<\/code><\/pre>\n<p><code>AndroidComponentsImpl<\/code>\u7c7b\uff0c\u5b83\u662f<code>AndroidComponents<\/code>\u7684\u5b9e\u73b0\u7c7b\uff0c\u5176<code>Logger<\/code>\u4e3a<code>AndroidLogger<\/code>\uff0c\u5176<code>MainThreadSupport<\/code>\u4e3a<code>DefaultAndroidMainThreadSupport<\/code>\u3002<br \/>\n<code>AndroidLogger<\/code>\u7c7b\uff0c\u5176\u5185\u90e8\u4f7f\u7528<code>Log<\/code>\u8fdb\u884c\u6253\u5370\uff0c\u6e90\u7801\u81ea\u884c\u67e5\u770b\uff0c\u6211\u4eec\u63a5\u4e0b\u6765\u770b\u4e00\u4e0b<code>DefaultAndroidMainThreadSupport<\/code>\u7c7b\u3002<\/p>\n<p><strong>DefaultAndroidMainThreadSupport\u7c7b<\/strong><\/p>\n<pre><code class=\"language-java\">public class DefaultAndroidMainThreadSupport implements MainThreadSupport {\n\n    @Override\n    public boolean isMainThread() {\n        return Looper.getMainLooper() == Looper.myLooper();\n    }\n\n    @Override\n    public Poster createPoster(EventBus eventBus) {\n        return new HandlerPoster(eventBus, Looper.getMainLooper(), 10);\n    }\n}<\/code><\/pre>\n<p><code>DefaultAndroidMainThreadSupport<\/code>\u7c7b\uff0c\u4e3a\u9ed8\u8ba4\u7684<code>Android<\/code><strong>\u4e3b\u7ebf\u7a0b<\/strong>\u652f\u6301\u7c7b\uff0c\u5176\u901a\u8fc7<code>isMainThread()<\/code>\u5224\u65ad\u662f\u5426\u662f\u5728<strong>\u4e3b\u7ebf\u7a0b<\/strong>\uff0c\u901a\u8fc7<code>createPoster()<\/code>\u521b\u5efa<strong>\u4e3b\u7ebf\u7a0b<\/strong><code>Poster<\/code>\uff0c\u4e3a<code>HandlerPoster<\/code>\u3002<\/p>\n<blockquote>\n<p>\u8bf4\u660e\uff1a<\/p>\n<ol>\n<li><code>isMainThread()<\/code>\u65b9\u6cd5\uff0c\u7528<code>Looper.myLooper() == Looper.getMainLooper()<\/code>\u6bd4\u8f83\u7684\u8bdd\uff0c\u4f1a<strong>\u6d6a\u8d39\u8d44\u6e90<\/strong>\u3002\u56e0\u4e3a<code>Looper.myLooper()<\/code>\u65b9\u6cd5\u83b7\u53d6<code>Looper<\/code>\uff0c\u5982\u679c\u5728<strong>\u65b0\u7ebf\u7a0b<\/strong>\u8c03\u7528\u7684\u8bdd\uff0c<code>ThreadLocal<\/code>\u4f1a\u521b\u5efa<code>ThreadLocalMap<\/code>\u3002\u5efa\u8bae\u6539\u6210\u7528\uff1a<code>Looper.getMainLooper().getThread() == Thread.currentThread()<\/code>\u3002<\/li>\n<\/ol>\n<\/blockquote>\n<h3>getMainThreadSupport<\/h3>\n<p><strong>EventBusBuilder &#8211;&gt; getMainThreadSupport\u65b9\u6cd5<\/strong><\/p>\n<pre><code class=\"language-java\">\/\/ \u83b7\u53d6MainThread\u652f\u6301\nMainThreadSupport getMainThreadSupport() {\n    if (mainThreadSupport != null) {\n        \/\/ mainThreadSupport\u672a\u4f7f\u7528\n        return mainThreadSupport;\n    } else if (AndroidComponents.areAvailable()) {\n        \/\/ AndroidComponents\u53ef\u7528\uff0c\u5219\u83b7\u53d6\u5230\u5176MainThreadSupport\uff0c\u5373DefaultAndroidMainThreadSupport\u3002\n        return AndroidComponents.get().defaultMainThreadSupport;\n    } else {\n        return null;\n    }\n}<\/code><\/pre>\n<p><code>getMainThreadSupport()<\/code>\u65b9\u6cd5\uff0c\u5982\u679c<code>AndroidComponents<\/code>\u53ef\u7528\uff0c\u5219\u83b7\u53d6\u5230\u5176\uff08\u5373<code>AndroidComponentsImpl<\/code>\uff09<code>MainThreadSupport<\/code>\uff0c\u5373<code>DefaultAndroidMainThreadSupport<\/code>\u3002<\/p>\n<h3>\u5c0f\u7ed3<\/h3>\n<blockquote>\n<ol>\n<li><code>EventBusBuilder<\/code>\u4e3a<strong>\u6784\u5efa\u8005\u6a21\u5f0f<\/strong>\u6784\u5efa<code>EventBus<\/code>\uff0c\u5176\u63d0\u4f9b\u4e86\u5168\u90e8<strong>\u53ef\u914d\u7f6e<\/strong>\u7684\u65b9\u6cd5\u3002<\/li>\n<li><code>getLogger()<\/code>\u65b9\u6cd5\uff0c\u5982\u679c\u662f<code>Android<\/code>\u5e73\u53f0\uff0c\u5219\u4e3a<code>AndroidLogger<\/code>\uff0c\u5426\u5219\u4e3a<code>SystemOutLogger<\/code>\u3002<\/li>\n<li><code>getMainThreadSupport()<\/code>\u65b9\u6cd5\uff0c\u5982\u679c\u662f<code>Android<\/code>\u5e73\u53f0\uff0c\u5219\u4e3a<code>DefaultAndroidMainThreadSupport<\/code>\uff0c\u5426\u5219\u4e3a<code>null<\/code>\u3002<\/li>\n<\/ol>\n<\/blockquote>\n<p><code>EventBusBuilder<\/code>\u76f8\u5173\u7684\u6211\u4eec\u5df2\u7ecf\u4ecb\u7ecd\u5b8c\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u770b\u4e00\u4e0b<code>EventBus<\/code>\u7684\u521d\u59cb\u5316\u3002<\/p>\n<h2>EventBus\u7684\u521d\u59cb\u5316<\/h2>\n<p><code>EventBus<\/code>\u7684<strong>\u6240\u6709\u65b9\u5f0f\u521b\u5efa<\/strong>\uff0c\u6700\u7ec8\u90fd\u4f1a\u8d70\u5230<code>EventBus(EventBusBuilder)<\/code>\u6784\u9020\u65b9\u6cd5\uff0c\u6211\u4eec\u63a5\u4e0b\u6765\u770b\u4e00\u4e0b<code>EventBus(EventBusBuilder)<\/code>\u6784\u9020\u65b9\u6cd5\u3002<\/p>\n<p><strong>EventBus &#8211;&gt; \u6784\u9020\u65b9\u6cd5<\/strong><\/p>\n<pre><code class=\"language-java\">private final Map&lt;Class&lt;?&gt;, CopyOnWriteArrayList&lt;Subscription&gt;&gt; subscriptionsByEventType;\nprivate final Map&lt;Object, List&lt;Class&lt;?&gt;&gt;&gt; typesBySubscriber;\nprivate final Map&lt;Class&lt;?&gt;, Object&gt; stickyEvents;\n\nprivate final MainThreadSupport mainThreadSupport;\nprivate final Poster mainThreadPoster;\nprivate final BackgroundPoster backgroundPoster;\nprivate final AsyncPoster asyncPoster;\nprivate final SubscriberMethodFinder subscriberMethodFinder;\nprivate final ExecutorService executorService;\n\nprivate final boolean throwSubscriberException;\nprivate final boolean logSubscriberExceptions;\nprivate final boolean logNoSubscriberMessages;\nprivate final boolean sendSubscriberExceptionEvent;\nprivate final boolean sendNoSubscriberEvent;\nprivate final boolean eventInheritance;\n\nprivate final int indexCount;\nprivate final Logger logger;\n\nEventBus(EventBusBuilder builder) {\n    \/\/ \u65e5\u5fd7\u5904\u7406\u8005\n    logger = builder.getLogger();\n\n    \/\/ =============\u4ee5\u4e0b3\u4e2a\u96c6\u5408\u5f88\u91cd\u8981==================\n    \/\/ [\u4e8b\u4ef6 --&gt; \u8be5\u4e8b\u4ef6\u7684\u8ba2\u9605\u5173\u7cfb\u7684List] \u7684Map\u3002\n    \/\/ 1.key\uff1aEvent\u7684Class\uff0cvalue\uff1aSubscription\uff08\u6301\u6709\u8ba2\u9605\u8005\u5bf9\u8c61\u3001\u8ba2\u9605\u65b9\u6cd5\u7684\u5bf9\u8c61\uff09\u7684List\u3002\n    \/\/ 2.\u5373\u901a\u8fc7\u5b83\uff0c\u65b9\u4fbf\u901a\u8fc7Event\u4e8b\u4ef6\uff0c\u627e\u5230\u6b64\u4e8b\u4ef6\u6240\u6709\u7684Subscription\uff08\u6301\u6709\u8ba2\u9605\u8005\u5bf9\u8c61\u3001\u8ba2\u9605\u65b9\u6cd5\u7684\u5bf9\u8c61\uff0c\u65b9\u4fbf\u53cd\u5c04\u8c03\u7528\uff09\u3002\n    \/\/ 3.\u4e3e\u4f8b\uff1a\u6709\u4e00\u4e2aEvent\u4e3aMessageEvent\uff0c\u5b83\u88abA\u3001B\u3001C\u4e09\u4e2a\u7c7b\u8fdb\u884c\u4e86\u8ba2\u9605\uff0c\u5219\u5b83\u91cc\u9762\u6709\u4e00\u6761\u6570\u636e\uff0c\n    \/\/ key\u4e3aMessageEvent\u7684Class\uff0cvalue\u4e3a\u67093\u6761\u6570\u636e\uff08A\u3001B\u3001C\u4e09\u4e2a\u7c7b\u7684Subscription\uff09\u7684List\u3002\n    subscriptionsByEventType = new HashMap&lt;&gt;();\n    \/\/ [\u8ba2\u9605\u8005 --&gt; \u8be5\u8ba2\u9605\u8005\u8ba2\u9605\u7684\u4e8b\u4ef6\u7684List] \u7684Map\u3002\n    \/\/ 1.key\uff1a\u8ba2\u9605\u8005\u5bf9\u8c61Object\uff0cvalue\uff1aEvent\u7684Class\u7684List\u3002\n    \/\/ 2.\u5373\u901a\u8fc7\u5b83\uff0c\u65b9\u4fbf\u901a\u8fc7\u8ba2\u9605\u8005\u5bf9\u8c61\uff0c\u627e\u5230\u5176\u8ba2\u9605\u7684\u6240\u6709Event\u7c7b\u578b\u3002\n    \/\/ 3.\u4e3e\u4f8b\uff1a\u67092\u4e2aEvent\u4e3aMessageEvent\u3001SayHelloEvent\uff0c\u5b83\u88abA\u7c7b\u90fd\u8fdb\u884c\u4e86\u8ba2\u9605\uff0c\u5219\u5b83\u91cc\u9762\u6709\u4e00\u6761\u6570\u636e\uff0c\n    \/\/ key\u4e3aA\u7c7b\u5bf9\u8c61\uff0cvalue\u4e3a\u67092\u6761\u6570\u636e\uff08MessageEvent\u3001SayHelloEvent\u4e24\u4e2a\u7c7b\u7684Class\uff09\u7684List\u3002\n    typesBySubscriber = new HashMap&lt;&gt;();\n    \/\/ [\u7c98\u6027\u4e8b\u4ef6 --&gt; \u7c98\u6027\u4e8b\u4ef6\u5bf9\u8c61] \u7684Map\u3002\n    \/\/ 1.key\uff1aEvent\u7684Class\uff0cvalue\uff1aEvent\u7684\u5bf9\u8c61\u3002\n    \/\/ 2.\u5373\u901a\u8fc7\u5b83\uff0c\u65b9\u4fbf\u901a\u8fc7Event\uff08\u7c98\u6027\u4e8b\u4ef6\uff09\u7684Class\uff0c\u627e\u5230\u5176\uff08\u7c98\u6027\u4e8b\u4ef6\uff09\u5b9e\u4f8b\u3002\n    stickyEvents = new ConcurrentHashMap&lt;&gt;();\n\n    \/\/ =============\u4ee5\u4e0b\u5728\u4e8b\u4ef6\u53d1\u9001\u4e2d\u5f88\u91cd\u8981==================\n    \/\/ \u4e3b\u7ebf\u7a0b\u652f\u6301\uff0cAndroid\u5e73\u53f0\u4e3aDefaultAndroidMainThreadSupport\uff0c\u5426\u5219\u4e3anull\u3002\n    mainThreadSupport = builder.getMainThreadSupport();\n    \/\/ \u4e3b\u7ebf\u7a0b\u4e8b\u4ef6\u53d1\u9001\u8005\uff0cAndroid\u5e73\u53f0\u4e3aHandlerPoster\uff0c\u5426\u5219\u4e3anull\u3002\n    mainThreadPoster = mainThreadSupport != null ? mainThreadSupport.createPoster(this) : null;\n    \/\/ Background\u4e8b\u4ef6\u53d1\u9001\u8005\n    backgroundPoster = new BackgroundPoster(this);\n    \/\/ \u5f02\u6b65\u4e8b\u4ef6\u53d1\u9001\u8005\n    asyncPoster = new AsyncPoster(this);\n\n    \/\/ =============\u4ee5\u4e0b\u4e3a\u83b7\u53d6EventBusBuilder\u7684\u914d\u7f6e==================\n    \/\/ \u6dfb\u52a0\u6ce8\u89e3\u5904\u7406\u5668\u751f\u6210\u7684\u7d22\u5f15SubscriberInfoIndex\u7684\u4e2a\u6570\n    indexCount = builder.subscriberInfoIndexes != null ? builder.subscriberInfoIndexes.size() : 0;\n    \/\/ \u8ba2\u9605\u65b9\u6cd5\u67e5\u627e\u5bf9\u8c61\uff08\u540e\u9762\u4f1a\u8be6\u7ec6\u4ecb\u7ecd\u6b64\u7c7b\u53ca\u5176\u6784\u9020\u53c2\u6570\uff09\n    subscriberMethodFinder = new SubscriberMethodFinder(builder.subscriberInfoIndexes,\n            builder.strictMethodVerification, builder.ignoreGeneratedIndex);\n    \/\/ \u8c03\u7528\u8ba2\u9605\u65b9\u6cd5\u5f02\u5e38\u65f6\uff0c\u662f\u5426\u6253\u5370\u5f02\u5e38\u4fe1\u606f\uff0c\u9ed8\u8ba4\u4e3atrue\u3002\n    logSubscriberExceptions = builder.logSubscriberExceptions;\n    \/\/ \u6ca1\u6709\u8ba2\u9605\u8005\u65f6\uff0c\u662f\u5426\u6253\u5370\u5f02\u5e38\u4fe1\u606f\uff0c\u9ed8\u8ba4\u4e3atrue\u3002\n    logNoSubscriberMessages = builder.logNoSubscriberMessages;\n    \/\/ \u8c03\u7528\u8ba2\u9605\u65b9\u6cd5\u5f02\u5e38\u65f6\uff0c\u662f\u5426\u53d1\u9001SubscriberExceptionEvent\u4e8b\u4ef6\uff0c\u9ed8\u8ba4\u4e3atrue\u3002\n    sendSubscriberExceptionEvent = builder.sendSubscriberExceptionEvent;\n    \/\/ \u6ca1\u6709\u8ba2\u9605\u8005\u65f6\uff0c\u662f\u5426\u53d1\u9001NoSubscriberEvent\u4e8b\u4ef6\uff0c\u9ed8\u8ba4\u4e3atrue\u3002\n    sendNoSubscriberEvent = builder.sendNoSubscriberEvent;\n    \/\/ \u8c03\u7528\u8ba2\u9605\u65b9\u6cd5\u5f02\u5e38\u65f6\uff0c\u662f\u5426\u629b\u51faSubscriberException\u5f02\u5e38\uff0c\u9ed8\u8ba4\u4e3afalse\u3002\n    throwSubscriberException = builder.throwSubscriberException;\n    \/\/ \u4e8b\u4ef6\u662f\u5426\u6709\u7ee7\u627f\u6027\uff0c\u9ed8\u8ba4\u4e3atrue\u3002\n    eventInheritance = builder.eventInheritance;\n    \/\/ \u7ebf\u7a0b\u6c60\uff0c\u7528\u4e8e\u5f02\u6b65\u548c\u540e\u53f0\u4e8b\u4ef6\u4f20\u9012\uff0c\u9ed8\u8ba4\u4e3aExecutors.newCachedThreadPool()\u3002\n    executorService = builder.executorService;\n}<\/code><\/pre>\n<h3>\u5c0f\u7ed3<\/h3>\n<blockquote>\n<ol>\n<li><code>EventBus<\/code>\u7684<strong>\u521d\u59cb\u5316<\/strong>\uff0c\u521b\u5efa\u4e863\u4e2a\u5f88\u91cd\u8981\u7684\u96c6\u5408\uff0c<code>subscriptionsByEventType<\/code>\u3001<code>typesBySubscriber<\/code>\u3001<code>stickyEvents<\/code>\uff0c\u8be6\u7ec6\u89e3\u91ca\u770b\u4e0a\u9762\u6ce8\u91ca\u3002<\/li>\n<li>\u521b\u5efa\u4e863\u4e2a<code>Poster<\/code>\uff0c<code>mainThreadPoster<\/code>\uff08\u5373<code>HandlerPoster<\/code>\uff09\u3001<code>backgroundPoster<\/code>\u3001<code>asyncPoster<\/code>\uff0c\u8be6\u7ec6\u4ecb\u7ecd\u770b\u540e\u9762\u7684-<code>Poster<\/code>\u3002<\/li>\n<li>\u83b7\u53d6<code>EventBusBuilder<\/code>\u7684\u914d\u7f6e\uff0c\u5e76\u8bb0\u5f55\u3002<\/li>\n<li>\u521b\u5efa<code>SubscriberMethodFinder<\/code>\uff0c\u7528\u4e8e\u67e5\u627e<strong>\u8ba2\u9605\u65b9\u6cd5<\/strong>\uff0c\u8be6\u7ec6\u4ecb\u7ecd\u770b\u540e\u9762\u7684-<code>SubscriberMethodFinder<\/code>\u3002<\/li>\n<\/ol>\n<\/blockquote>\n<h2>EventBus\u7684\u6ce8\u518c<\/h2>\n<p><strong>EventBus &#8211;&gt; register()<\/strong><\/p>\n<pre><code class=\"language-java\">public void register(Object subscriber) {\n    if (AndroidDependenciesDetector.isAndroidSDKAvailable() &amp;&amp; !AndroidDependenciesDetector.areAndroidComponentsAvailable()) {\n        \/\/ \u662fandroid\u5e73\u53f0\u4f46\u662f\u6ca1\u4f9d\u8d56eventbus\uff08Android\uff09\u5e93\uff0c\u5219\u629b\u51fa\u5f02\u5e38\u3002\n        throw new RuntimeException(&quot;It looks like you are using EventBus on Android, &quot; +\n                &quot;make sure to add the \\&quot;eventbus\\&quot; Android library to your dependencies.&quot;);\n    }\n    \/\/ 1\u3001\u83b7\u53d6\u5230\u8ba2\u9605\u8005\u7684Class\u5bf9\u8c61\u3002\n    Class&lt;?&gt; subscriberClass = subscriber.getClass();\n    \/\/ 2\u3001\u901a\u8fc7subscriberMethodFinder\u5bf9\u8c61\u627e\u5230\u8be5\u8ba2\u9605\u8005\u7684\u6240\u6709\u8ba2\u9605\u65b9\u6cd5\u3002\n    List&lt;SubscriberMethod&gt; subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);\n    \/\/ 3\u3001\u540c\u6b65\uff0c\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\u3002\n    synchronized (this) {\n        \/\/ 4\u3001\u904d\u5386\u96c6\u5408\u8fdb\u884c\u8ba2\u9605\n        for (SubscriberMethod subscriberMethod : subscriberMethods) {\n            \/\/ 5\u3001\u8ba2\u9605\uff0c\u4f20\u5165\u8ba2\u9605\u8005\u5bf9\u8c61\u3001\u8be5\u8ba2\u9605\u8005\u7684\u8ba2\u9605\u65b9\u6cd5\u5bf9\u8c61\u3002\n            subscribe(subscriber, subscriberMethod);\n        }\n    }\n}<\/code><\/pre>\n<p><code>register()<\/code>\u65b9\u6cd5\uff0c\u4e3a<strong>\u6ce8\u518c\u8ba2\u9605\u8005<\/strong>\u65b9\u6cd5\uff0c\u901a\u8fc7<code>subscriberMethodFinder<\/code>\u627e\u5230<strong>\u8be5\u8ba2\u9605\u8005<\/strong>\u7684<strong>\u6240\u6709\u8ba2\u9605\u65b9\u6cd5<\/strong>\uff0c\u7136\u540e\u8fdb\u884c\u904d\u5386\u4f9d\u6b21<strong>\u8ba2\u9605<\/strong>\uff0c<code>SubscriberMethodFinder<\/code>\u76f8\u5173\u6211\u4eec\u5355\u72ec\u4ecb\u7ecd\uff08\u770b\u540e\u9762\u7684-<code>SubscriberMethodFinder<\/code>\uff09\uff0c\u6211\u4eec\u5148\u6765\u770b\u4e00\u4e0b<code>SubscriberMethod<\/code>\u7c7b\u3002<\/p>\n<p><strong>SubscriberMethod\u7c7b<\/strong><\/p>\n<pre><code class=\"language-java\">public class SubscriberMethod {\n    \/\/ \u8ba2\u9605\u8005\u53cd\u5c04\u65b9\u6cd5\n    final Method method;\n    \/\/ \u8ba2\u9605\u65b9\u6cd5-\u7ebf\u7a0b\u6a21\u578b\n    final ThreadMode threadMode;\n    \/\/ \u8ba2\u9605\u65b9\u6cd5-\u4e8b\u4ef6\u7c7b\u578b\n    final Class&lt;?&gt; eventType;\n    \/\/ \u8ba2\u9605\u65b9\u6cd5-\u4f18\u5148\u7ea7\n    final int priority;\n    \/\/ \u8ba2\u9605\u65b9\u6cd5-\u662f\u5426\u662f\u7c98\u6027\n    final boolean sticky;\n    \/** Used for efficient comparison *\/\n    String methodString;\n\n    public SubscriberMethod(Method method, Class&lt;?&gt; eventType, ThreadMode threadMode, int priority, boolean sticky) {\n        this.method = method;\n        this.threadMode = threadMode;\n        this.eventType = eventType;\n        this.priority = priority;\n        this.sticky = sticky;\n    }\n\n    @Override\n    public boolean equals(Object other) {\n        if (other == this) {\n            return true;\n        } else if (other instanceof SubscriberMethod) {\n            checkMethodString();\n            SubscriberMethod otherSubscriberMethod = (SubscriberMethod)other;\n            otherSubscriberMethod.checkMethodString();\n            \/\/ Don&#039;t use method.equals because of http:\/\/code.google.com\/p\/android\/issues\/detail?id=7811#c6\n            return methodString.equals(otherSubscriberMethod.methodString);\n        } else {\n            return false;\n        }\n    }\n\n    private synchronized void checkMethodString() {\n        if (methodString == null) {\n            \/\/ Method.toString has more overhead, just take relevant parts of the method\n            StringBuilder builder = new StringBuilder(64);\n            builder.append(method.getDeclaringClass().getName());\n            builder.append(&#039;#&#039;).append(method.getName());\n            builder.append(&#039;(&#039;).append(eventType.getName());\n            methodString = builder.toString();\n        }\n    }\n\n    @Override\n    public int hashCode() {\n        return method.hashCode();\n    }\n}<\/code><\/pre>\n<p><code>SubscriberMethod<\/code>\u7c7b\uff0c\u4e3a<strong>\u8ba2\u9605\u65b9\u6cd5<\/strong>\u4fe1\u606f\u76f8\u5173\u7c7b\uff0c\u5b83\u8bb0\u5f55\u4e86<strong>\u8ba2\u9605\u65b9\u6cd5<\/strong>\u4e0a\uff0c<code>Subscribe<\/code>\u6ce8\u89e3\u7684\u4fe1\u606f\u3001\u53c2\u6570\u4fe1\u606f\u3001\u4ee5\u53ca\u53cd\u5c04\u65b9\u6cd5<code>Method<\/code>\uff08\u65b9\u4fbf\u540e\u7eed\u53cd\u5c04\u8c03\u7528\uff09\u3002<\/p>\n<p>\u5728\u770b<code>subscribe()<\/code>\u65b9\u6cd5\u524d\uff0c\u6211\u4eec\u5148\u6765\u770b\u4e00\u4e0b<code>Subscription<\/code>\u7c7b\uff0c\u5176\u5728<code>subscribe()<\/code>\u65b9\u6cd5\uff08\u7a0d\u540e\u4ecb\u7ecd\uff09\u5185\u4f7f\u7528\u3002<\/p>\n<p><strong>Subscription\u7c7b<\/strong><\/p>\n<pre><code class=\"language-java\">final class Subscription {\n    \/\/ \u8ba2\u9605\u8005\u5bf9\u8c61\n    final Object subscriber;\n    \/\/ \u8ba2\u9605\u65b9\u6cd5\n    final SubscriberMethod subscriberMethod;\n    \/\/ \u662f\u5426\u662f\u6d3b\u8dc3\u7684\n    volatile boolean active;\n\n    Subscription(Object subscriber, SubscriberMethod subscriberMethod) {\n        this.subscriber = subscriber;\n        this.subscriberMethod = subscriberMethod;\n        active = true;\n    }\n\n    @Override\n    public boolean equals(Object other) {\n        if (other instanceof Subscription) {\n            Subscription otherSubscription = (Subscription) other;\n            return subscriber == otherSubscription.subscriber\n                    &amp;&amp; subscriberMethod.equals(otherSubscription.subscriberMethod);\n        } else {\n            return false;\n        }\n    }\n\n    @Override\n    public int hashCode() {\n        return subscriber.hashCode() + subscriberMethod.methodString.hashCode();\n    }\n}<\/code><\/pre>\n<p><code>Subscription<\/code>\u7c7b\uff0c\u4e3a<strong>\u8ba2\u9605\u8005\u5bf9\u8c61<\/strong>\u3001<strong>\u8ba2\u9605\u65b9\u6cd5<\/strong>\u5c01\u88c5\u7c7b\uff0c\u65b9\u4fbf\u540e\u7eed<strong>\u53cd\u5c04<\/strong>\u8c03\u7528<strong>\u8ba2\u9605\u8005\u5bf9\u8c61<\/strong>\u7684<strong>\u8ba2\u9605\u65b9\u6cd5<\/strong>\u3002<\/p>\n<p>\u6211\u4eec\u7ee7\u7eed\u6765\u770b\u4e00\u4e0b<code>subscribe()<\/code>\u65b9\u6cd5\u3002<\/p>\n<p><strong>EventBus &#8211;&gt; subscribe()<\/strong><\/p>\n<pre><code class=\"language-java\">private void subscribe(Object subscriber, SubscriberMethod subscriberMethod) {\n    \/\/ 6\u3001\u83b7\u53d6\u8ba2\u9605\u65b9\u6cd5\u7684\u4e8b\u4ef6\u7c7b\u578b\n    Class&lt;?&gt; eventType = subscriberMethod.eventType;\n    \/\/ 7\u3001\u5c01\u88c5Subscription\u5bf9\u8c61\uff08\u4e4b\u540e\u4f1a\u5c06newSubscription\u6dfb\u52a0\u5230subscriptionsByEventType\u4e2d\uff09\u3002\n    Subscription newSubscription = new Subscription(subscriber, subscriberMethod);\n    \/\/ 8\u3001\u901a\u8fc7\u4e8b\u4ef6\u7c7b\u578b\u83b7\u53d6\u8be5\u4e8b\u4ef6\u7684Subscription\u96c6\u5408\uff08\u4e4b\u540e\u4f1a\u5c06newSubscription\u6dfb\u52a0\u5230subscriptions\u4e2d\uff09\u3002\n    CopyOnWriteArrayList&lt;Subscription&gt; subscriptions = subscriptionsByEventType.get(eventType);\n    if (subscriptions == null) {\n        \/\/ 9\u3001\u96c6\u5408\u4e3a\u7a7a\uff0c\u8bf4\u660e\u8be5\u4e8b\u4ef6\u4e3a\u7b2c\u4e00\u6b21\u8ba2\u9605\uff0c\u5219\u521b\u5efa\u96c6\u5408\uff0c\u5e76\u628a\u5f53\u524dEvent\u7c7b\u578b\u5b58\u5165subscriptionsByEventType\u4e2d\u3002\n        subscriptions = new CopyOnWriteArrayList&lt;&gt;();\n        subscriptionsByEventType.put(eventType, subscriptions);\n    } else {\n        \/\/ 10\u3001\u96c6\u5408\u4e0d\u4e3a\u7a7a\uff0c\u8bf4\u660e\u8be5\u4e8b\u4ef6\u5df2\u7ecf\u88ab\u8ba2\u9605\u8fc7\uff0c\u5219\u5224\u65ad\u8be5\u8ba2\u9605\u8005\u662f\u5426\u6709\u91cd\u590d\u8ba2\u9605\u7684\u73b0\u8c61\u3002\n        if (subscriptions.contains(newSubscription)) {\n            \/\/ 11\u3001\u5305\u542b\uff0c\u5219\u8bf4\u660e\u662f\u91cd\u590d\u8ba2\u9605\uff0c\u6b64\u65b9\u6cd5\u53ea\u6709\u88abregister()\u65b9\u6cd5\u8c03\u7528\uff0c\u6240\u4ee5\u8bf4\u660e\u662f\u91cd\u590d\u6ce8\u518c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\u3002\n            throw new EventBusException(&quot;Subscriber &quot; + subscriber.getClass() + &quot; already registered to event &quot;\n                    + eventType);\n        }\n    }\n\n    \/\/ 12\u3001\u904d\u5386\u8be5\u4e8b\u4ef6\u7684\u6240\u6709\u8ba2\u9605\u8005\uff0c\u6309\u7167\u4f18\u5148\u7ea7\u8fdb\u884c\u63d2\u5165\u3002\n    int size = subscriptions.size();\n    \/\/ 13\u3001i &lt;= size\uff0csize\u6700\u5c11\u4e3a0\uff0c\u6240\u4ee5\u81f3\u5c11\u4f1a\u6267\u884c\u4e00\u6b21for\u5faa\u73af\u3002\n    for (int i = 0; i &lt;= size; i++) {\n        \/\/ \u8bf4\u660e\uff1a\n        \/\/ 1. i == size\uff0c\u5219\u8bf4\u660e\u662f\u6700\u540e\u4e00\u4e2a\u3002\n        \/\/ 2. subscriberMethod.priority &gt; subscriptions.get(i).subscriberMethod.priority\uff0c\u8bf4\u660e\u65b0\u7684\u4f18\u5148\u7ea7\u9ad8\u4e8e\u5f53\u524d\u7684\u4f18\u5148\u7ea7\u3002\n        \/\/ 14\u3001\u5982\u679c\u662f\u6700\u540e\u4e00\u4e2a\uff0c\u6216\u8005\u65b0\u7684\u4f18\u5148\u7ea7\u9ad8\u4e8e\u5f53\u524d\u7684\u4f18\u5148\u7ea7\uff0c\u5219\u6dfb\u52a0\u5230\u5f53\u524d\u4f4d\u7f6e\uff0c\u5373\u5f53\u524d\u4f4d\u7f6e\u540e\u79fb\uff0c\u6574\u4f53\u4ece\u9ad8\u5230\u4f4e\u6392\u5e8f\u3002\n        if (i == size || subscriberMethod.priority &gt; subscriptions.get(i).subscriberMethod.priority) {\n            subscriptions.add(i, newSubscription);\n            break;\n        }\n    }\n\n    \/\/ 15\u3001\u83b7\u53d6\u8be5\u8ba2\u9605\u8005\u8ba2\u9605\u7684\u4e8b\u4ef6\u7684\u96c6\u5408\uff08\u4e4b\u540e\u4f1a\u5c06eventType\u6dfb\u52a0\u5230subscribedEvents\u4e2d\uff09\u3002\n    List&lt;Class&lt;?&gt;&gt; subscribedEvents = typesBySubscriber.get(subscriber);\n    if (subscribedEvents == null) {\n        \/\/ 16\u3001\u96c6\u5408\u4e3a\u7a7a\uff0c\u5219\u8bf4\u660e\u662f\u7b2c\u4e00\u6b21\u6dfb\u52a0\u8ba2\u9605\u8005\uff0c\u5219\u521b\u5efa\u96c6\u5408\uff0c\u5e76\u628a\u5f53\u524d\u8ba2\u9605\u8005\u5b58\u5165typesBySubscriber\u4e2d\u3002\n        subscribedEvents = new ArrayList&lt;&gt;();\n        typesBySubscriber.put(subscriber, subscribedEvents);\n    }\n    \/\/ 17\u3001\u5c06\u4e8b\u4ef6\u52a0\u5165\u5230\u8be5\u8ba2\u9605\u8005\u8ba2\u9605\u7684\u4e8b\u4ef6\u7684\u96c6\u5408\u4e2d\n    subscribedEvents.add(eventType);\n\n    \/\/ 18\u3001\u5224\u65ad\u8be5\u8ba2\u9605\u65b9\u6cd5\u662f\u5426\u662f\u7c98\u6027\u4e8b\u4ef6\uff0c\u5982\u679c\u662f\u7c98\u6027\u4e8b\u4ef6\uff0c\u5219\u6ce8\u518c\u65f6\u5c31\u4f1a\u901a\u77e5\u4e8b\u4ef6\uff0c\u56e0\u4e3a\u53d1\u9001\u7c98\u6027\u4e8b\u4ef6\u4e0d\u7ba1\u4e4b\u524d\u6ce8\u518c\u8fd8\u662f\u4e4b\u540e\u6ce8\u518c\u90fd\u4f1a\u901a\u77e5\u3002\n    if (subscriberMethod.sticky) {\n        \/\/ 19\u3001\u662f\u7c98\u6027\u4e8b\u4ef6\uff0c\u5219\u8fdb\u884c\u901a\u77e5\u8ba2\u9605\u65b9\u6cd5\u3002\n        if (eventInheritance) { \/\/ eventInheritance\uff1a\u4e8b\u4ef6\u662f\u5426\u6709\u7ee7\u627f\u6027\uff0c\u9ed8\u8ba4\u4e3atrue\u3002\n            \/\/ 20\u3001\u4e8b\u4ef6\u6709\u7ee7\u627f\u6027\uff0c\u5219\u901a\u77e5\u6240\u6709\u5df2\u53d1\u9001\u7c98\u6027\u4e8b\u4ef6\u7b26\u5408\u662f\u5176\u81ea\u5df1\u6216\u8005\u5176\u5b50\u7c7b\u7684\u4e8b\u4ef6\u3002\n            \/\/ 21\u3001\u83b7\u53d6\u6240\u6709\u5df2\u53d1\u9001\u7c98\u6027\u4e8b\u4ef6\uff0c\u904d\u5386\u5224\u65ad\u662f\u5426\u7b26\u5408\u662f\u5176\u81ea\u5df1\u6216\u8005\u5176\u5b50\u7c7b\u7684\u4e8b\u4ef6\u3002\n            Set&lt;Map.Entry&lt;Class&lt;?&gt;, Object&gt;&gt; entries = stickyEvents.entrySet();\n            for (Map.Entry&lt;Class&lt;?&gt;, Object&gt; entry : entries) {\n                Class&lt;?&gt; candidateEventType = entry.getKey();\n                if (eventType.isAssignableFrom(candidateEventType)) {\n                    \/\/ 22\u3001\u7b26\u5408\u662f\u5176\u81ea\u5df1\u6216\u8005\u5176\u5b50\u7c7b\u7684\u4e8b\u4ef6\uff0csubscriptions\u5219\u8fdb\u884c\u901a\u77e5\u3002\n                    Object stickyEvent = entry.getValue();\n                    \/\/ 23\u3001\u8c03\u7528checkPostStickyEventToSubscription\u65b9\u6cd5\u8fdb\u884c\u68c0\u67e5\u53d1\u9001\u3002\n                    checkPostStickyEventToSubscription(newSubscription, stickyEvent);\n                }\n            }\n        } else {\n            \/\/ 24\u3001\u4e8b\u4ef6\u6ca1\u6709\u7ee7\u627f\u6027\uff0c\u5219\u901a\u77e5\u6240\u6709\u5df2\u53d1\u9001\u7c98\u6027\u4e8b\u4ef6\u7b26\u5408\u662f\u5176\u81ea\u5df1\u7684\u4e8b\u4ef6\u3002\n            Object stickyEvent = stickyEvents.get(eventType);\n            \/\/ 25\u3001\u8c03\u7528checkPostStickyEventToSubscription\u65b9\u6cd5\u8fdb\u884c\u68c0\u67e5\u53d1\u9001\uff0cstickyEvent\u6709\u53ef\u80fd\u4e3anull\uff0c\u56e0\u4e3a\u53ef\u80fd\u4e4b\u524d\u6ca1\u53d1\u9001\u8fc7\u6b64\u7c7b\u4e8b\u4ef6\u7684\u7c98\u6027\u4e8b\u4ef6\u3002\n            checkPostStickyEventToSubscription(newSubscription, stickyEvent);\n        }\n    }\n}<\/code><\/pre>\n<p><code>subscribe()<\/code>\u65b9\u6cd5\uff0c\u4e3a<strong>\u8ba2\u9605<\/strong>\u65b9\u6cd5\uff0c\u5176\u4e3b\u8981\u7ef4\u62a4<code>subscriptionsByEventType<\/code>\u3001<code>typesBySubscriber<\/code>\u6570\u636e\uff0c\u5e76\u5904\u7406<strong>\u7c98\u6027\u65b9\u6cd5<\/strong>\uff0c\u5982\u679c\u662f<strong>\u7c98\u6027\u65b9\u6cd5<\/strong>\u5219\u8c03\u7528<code>checkPostStickyEventToSubscription()<\/code>\u65b9\u6cd5\u8fdb\u884c<strong>\u68c0\u67e5\u53d1\u9001<\/strong>\u3002<\/p>\n<blockquote>\n<p>\u8bf4\u660e\uff1a<\/p>\n<ol>\n<li>\u6b65\u9aa4<strong>6-14<\/strong>\uff0c\u4e3a<strong>\u7ef4\u62a4<\/strong><code>subscriptionsByEventType<\/code>\u96c6\u5408\u6570\u636e\u3002\u6b65\u9aa4<strong>14<\/strong>\uff0c\u5219\u4f1a\u5bfc\u81f4<strong>\u8be5\u4e8b\u4ef6<\/strong>\u7684<strong>\u6240\u6709<\/strong><code>Subscription<\/code>\uff0c\u6309<strong>\u8ba2\u9605\u65b9\u6cd5\u4f18\u5148\u7ea7<\/strong>\u4ece<strong>\u9ad8\u5230\u4f4e<\/strong>\u6392\u5e8f\u3002<\/li>\n<li>\u6b65\u9aa4<strong>15-17<\/strong>\uff0c\u4e3a<strong>\u7ef4\u62a4<\/strong><code>typesBySubscriber<\/code>\u96c6\u5408\u6570\u636e\u3002<\/li>\n<li>\u6b65\u9aa4<strong>18-25<\/strong>\uff0c\u4e3a\u5224\u65ad\u662f\u5426\u662f<strong>\u7c98\u6027\u65b9\u6cd5<\/strong>\uff0c\u5982\u679c<strong>\u662f<\/strong>\u5219\u518d\u5224\u65ad<strong>\u4e8b\u4ef6\u662f\u5426\u6709\u7ee7\u627f\u6027<\/strong>\uff0c\u627e\u5230\u5176\u5bf9\u5e94\u7684<strong>\u4e8b\u4ef6\u5bf9\u8c61<\/strong>\uff0c\u6700\u540e\u8c03\u7528<code>checkPostStickyEventToSubscription()<\/code>\u65b9\u6cd5\u8fdb\u884c<strong>\u68c0\u67e5\u53d1\u9001<\/strong>\uff0c\u5982<strong>\u68c0\u6d4b\u6210\u529f<\/strong>\u5219\u4f1a\u901a\u77e5<strong>\u8ba2\u9605\u65b9\u6cd5<\/strong>\u7684\u3002<\/li>\n<li><code>eventInheritance<\/code>\u4e3a<strong>\u4e8b\u4ef6<\/strong>\u662f\u5426\u6709<strong>\u7ee7\u627f\u6027<\/strong>\uff0c\u9ed8\u8ba4\u4e3a<code>true<\/code>\uff0c\u5373\u4f1a\u63a5\u6536<strong>\u5df2\u7ecf\u53d1\u9001<\/strong>\u7684<strong>\u7c98\u6027\u4e8b\u4ef6<\/strong>\u662f<strong>\u5176\u81ea\u5df1<\/strong>\u6216\u8005<strong>\u5176\u5b50\u7c7b<\/strong>\u7684\u4e8b\u4ef6\u3002<\/li>\n<li>\u5982\u679c<strong>\u5df2\u7ecf\u53d1\u9001<\/strong>\u4e86<strong>\u5f88\u591a\u7c98\u6027\u4e8b\u4ef6<\/strong>\uff0c\u5219\u904d\u5386<strong>\u6240\u6709\u4e8b\u4ef6<\/strong>\uff0c\u6240\u4ee5\u4f1a\u56e0\u6709<strong>\u5927\u91cf\u7c98\u6027\u4e8b\u4ef6<\/strong>\u800c<strong>\u6548\u7387\u4f4e\u4e0b<\/strong>\u3002<\/li>\n<\/ol>\n<\/blockquote>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u7ee7\u7eed\u6765\u770b\u4e00\u4e0b<code>checkPostStickyEventToSubscription()<\/code>\u65b9\u6cd5\u3002<\/p>\n<p><strong>EventBus &#8211;&gt; checkPostStickyEventToSubscription()<\/strong><\/p>\n<pre><code class=\"language-java\">private void checkPostStickyEventToSubscription(Subscription newSubscription, Object stickyEvent) {\n    if (stickyEvent != null) {\n        \/\/ 26\u3001\u5982\u679c\u7c98\u6027\u4e8b\u4ef6\u4e0d\u4e3a\u7a7a\uff0c\u5219\u8fdb\u884c\u53d1\u9001\u7c98\u6027\u4e8b\u4ef6\u5230\u8ba2\u9605\u65b9\u6cd5\uff0c\u5e76\u4f20\u5165\u662f\u5426\u662f\u5728\u4e3b\u7ebf\u7a0b\u53d1\u9001\u3002\n        postToSubscription(newSubscription, stickyEvent, isMainThread());\n    }\n}<\/code><\/pre>\n<p><code>checkPostStickyEventToSubscription()<\/code>\u65b9\u6cd5\uff0c\u4e3a<strong>\u68c0\u67e5\u53d1\u9001\u7c98\u6027\u4e8b\u4ef6\u5230\u8ba2\u9605\u65b9\u6cd5<\/strong>\u7684\u65b9\u6cd5\uff0c\u5982\u679c<strong>\u7c98\u6027\u4e8b\u4ef6<\/strong>\u4e0d\u4e3a\u7a7a\uff0c\u5219\u8fdb\u884c<strong>\u53d1\u9001<\/strong>\uff0c\u5e76\u4f20\u5165<strong>\u662f\u5426\u662f\u5728\u4e3b\u7ebf\u7a0b\u53d1\u9001<\/strong>\uff0c\u540e\u9762<code>postToSubscription()<\/code>\u5c31\u662f\u6211\u4eec\u7684\u53d1\u9001\u6d41\u7a0b\uff0c\u8be6\u7ec6\u4ecb\u7ecd\u770b\u540e\u9762\u7684-<code>EventBus<\/code>\u7684<code>post<\/code>\u3002<\/p>\n<p><strong>EventBus &#8211;&gt; isMainThread()<\/strong><\/p>\n<pre><code class=\"language-java\">private boolean isMainThread() {\n    return mainThreadSupport == null || mainThreadSupport.isMainThread();\n}<\/code><\/pre>\n<p><code>isMainThread()<\/code>\u65b9\u6cd5\uff0c\u4e3a<strong>\u68c0\u67e5\u5f53\u524d\u7ebf\u7a0b\u662f\u5426\u662f\u5728\u4e3b\u7ebf\u7a0b<\/strong>\u7684\u65b9\u6cd5\u3002<\/p>\n<blockquote>\n<p>\u8bf4\u660e\uff1a<\/p>\n<ol>\n<li>\u5982\u679c\u4e0d\u662f<code>Android<\/code>\u5e73\u53f0\u5219\u59cb\u7ec8\u4e3a<code>true<\/code>\uff0c\u5982\u679c\u662f<code>Android<\/code>\u5e73\u53f0\u5219\u901a\u8fc7<code>mainThreadSupport.isMainThread()<\/code>\u5224\u65ad<strong>\u662f\u5426\u662f\u5728\u4e3b\u7ebf\u7a0b<\/strong>\u3002<\/li>\n<\/ol>\n<\/blockquote>\n<h3>\u5c0f\u7ed3<\/h3>\n<blockquote>\n<ol>\n<li><code>EventBus<\/code>\u7684<strong>\u6ce8\u518c<\/strong>\uff0c\u4f20\u5165<strong>\u8ba2\u9605\u8005<\/strong>\u5bf9\u8c61\uff0c\u901a\u8fc7<strong>\u53cd\u5c04<\/strong>\u83b7\u53d6\u5230<strong>\u8ba2\u9605\u8005<\/strong>\u7684<code>Class<\/code>\u5bf9\u8c61\u3002<\/li>\n<li>\u901a\u8fc7<code>SubscriberMethodFinder<\/code><strong>\u83b7\u53d6<\/strong>\u8ba2\u9605\u8005\u4e2d\u6240\u6709<strong>\u8ba2\u9605\u65b9\u6cd5<\/strong>\u96c6\u5408\u3002<\/li>\n<li><strong>\u904d\u5386<\/strong>\u4e0a\u9762\u83b7\u53d6\u7684\u96c6\u5408\uff0c\u5c06<strong>\u8ba2\u9605\u8005<\/strong>\u548c<strong>\u4e8b\u4ef6<\/strong>\u8fdb\u884c\u5305\u88c5<strong>\u7ed1\u5b9a<\/strong>\u4e3a<code>Subscription<\/code>\u3002<\/li>\n<li><strong>\u7ef4\u62a4<\/strong><code>subscriptionsByEventType<\/code>\u3001<code>typesBySubscriber<\/code>\u96c6\u5408\u6570\u636e\uff0c\u5c06<code>Subscription<\/code>\u6dfb\u52a0\u5230<code>subscriptionsByEventType<\/code>\u7684<strong>\u5f53\u524d<\/strong><code>eventType<\/code>\uff08\u4ece<strong>\u8ba2\u9605\u65b9\u6cd5<\/strong>\u4e2d\u83b7\u53d6\uff09\u7684<code>List<\/code>\u4e2d\uff0c\u5c06<code>eventType<\/code>\u6dfb\u52a0\u5230<code>typesBySubscriber<\/code>\u7684<strong>\u5f53\u524d\u8ba2\u9605\u8005<\/strong>\u7684<code>List<\/code>\u4e2d\u3002<\/li>\n<li>\u5e76\u5224\u65ad\u6b64<strong>\u8ba2\u9605\u65b9\u6cd5<\/strong>\u662f\u5426\u662f<strong>\u7c98\u6027\u65b9\u6cd5<\/strong>\uff0c\u5982\u679c\u662f<strong>\u7c98\u6027\u65b9\u6cd5<\/strong>\uff0c\u5e76\u4e14<strong>\u4e4b\u524d<\/strong>\u5df2\u7ecf\u53d1\u9001\u8fc7<strong>\u6b64\u7c98\u6027<\/strong>\u4e8b\u4ef6\uff0c\u5219\u8fdb\u884c\u53d1\u9001<strong>\u7c98\u6027\u4e8b\u4ef6<\/strong>\u5230\u6b64<strong>\u8ba2\u9605\u65b9\u6cd5<\/strong>\u3002<\/li>\n<\/ol>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>EventBus\u6e90\u7801\u89e3\u6790\uff08\u4e00\uff09 \u6e90\u7801\u7248\u672c\uff1a EventBus\uff1a3.3.1 \u4f7f\u7528 \u5b9a\u4e49\u4e8b\u4ef6 class Mess [&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-113","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\/113","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=113"}],"version-history":[{"count":2,"href":"http:\/\/www.devblog.cn\/index.php?rest_route=\/wp\/v2\/posts\/113\/revisions"}],"predecessor-version":[{"id":117,"href":"http:\/\/www.devblog.cn\/index.php?rest_route=\/wp\/v2\/posts\/113\/revisions\/117"}],"wp:attachment":[{"href":"http:\/\/www.devblog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.devblog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=113"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.devblog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}