EventBus源码阅读(15)-HandlerPoster

我们平时在开发中经常使用handler。

该类继承自Handler,说明要在指定的线程中执行,有可能是主线程,也可能是某个固定的线程,就看试用了。


源码:

/*
 * Copyright (C) 2012-2016 Markus Junginger, greenrobot (http://greenrobot.org)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.greenrobot.eventbus;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;

final class HandlerPoster extends Handler {

    private final PendingPostQueue queue;
    private final int maxMillisInsideHandleMessage;
    private final EventBus eventBus;
    private boolean handlerActive;

    HandlerPoster(EventBus eventBus, Looper looper, int maxMillisInsideHandleMessage) {
        super(looper);
        this.eventBus = eventBus;
        this.maxMillisInsideHandleMessage = maxMillisInsideHandleMessage;
        queue = new PendingPostQueue();//采用独立队列,与backgroundPoster一致
    }

    void enqueue(Subscription subscription, Object event) {
        PendingPost pendingPost = PendingPost.obtainPendingPost(subscription, event);
        synchronized (this) {//此处与BackgroundPoster采用了相同的处理方法,防止对queue的多线程访问
            queue.enqueue(pendingPost);
            if (!handlerActive) {
                handlerActive = true;
                if (!sendMessage(obtainMessage())) {
                    throw new EventBusException("Could not send handler message");
                }
            }
        }
    }

    @Override
    public void handleMessage(Message msg) {
        boolean rescheduled = false;
        try {
            long started = SystemClock.uptimeMillis();
            while (true) {
                PendingPost pendingPost = queue.poll();
                if (pendingPost == null) {
                    synchronized (this) {
                        // Check again, this time in synchronized
                        pendingPost = queue.poll();
                        if (pendingPost == null) {
                            handlerActive = false;
                            return;
                        }
                    }
                }
                eventBus.invokeSubscriber(pendingPost);
                long timeInMethod = SystemClock.uptimeMillis() - started;
                if (timeInMethod >= maxMillisInsideHandleMessage) {//任务执行超时,需要重新触发
                    if (!sendMessage(obtainMessage())) {
                        throw new EventBusException("Could not send handler message");
                    }
                    rescheduled = true;
                    return;
                }
            }
        } finally {
            handlerActive = rescheduled;
        }
    }
}

这个里边要注意 handlerActive与BackgroundPoster中executorRunning的功能上一至,但使用上却又些微的区别。要好好的观察为什么。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
abp-vnex eventbus是一个用于在ABP框架中进行事件通信的模块。要使用abp-vnex eventbus,您需要按照以下步骤进行安装和配置: 1. 首先,您需要安装abp-vnex eventbus模块。可以通过运行以下命令来安装: ```shell npm install abp-vnex-eventbus --save ``` 2. 安装完成后,您需要在您的应用程序的模块中导入abp-vnex eventbus模块。在您的模块文件中,添加以下代码: ```typescript import { AbpVnexEventBusModule } from 'abp-vnex-eventbus'; @NgModule({ imports: [ AbpVnexEventBusModule ] }) export class YourModule { } ``` 3. 现在,您可以在您的组件或服务中使用abp-vnex eventbus来发送和接收事件。首先,您需要导入`AbpVnexEventBusService`: ```typescript import { AbpVnexEventBusService } from 'abp-vnex-eventbus'; ``` 4. 在您的组件或服务中,您可以使用`AbpVnexEventBusService`的`emit`方法来发送事件。例如,发送一个名为`myEvent`的事件: ```typescript constructor(private eventBus: AbpVnexEventBusService) { } sendEvent() { this.eventBus.emit('myEvent', { data: 'Hello World' }); } ``` 5. 要接收事件,您可以使用`AbpVnexEventBusService`的`on`方法。在您的组件或服务中,添加以下代码: ```typescript constructor(private eventBus: AbpVnexEventBusService) { } ngOnInit() { this.eventBus.on('myEvent').subscribe((eventData) => { console.log(eventData.data); // 输出:Hello World }); } ``` 这样,您就可以使用abp-vnex eventbus模块来进行事件通信了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值