用户程序框架之线程间通信EventHandler 原创 精华

发布于 2022-2-28 10:27
浏览
3收藏

用户程序框架之线程间通信

作者:纪春祥

概述

应用在运行过程中,经常会有一些耗时较长的任务需要完成,并且不阻塞当前线程,需要独立于当前线程去执行,鸿蒙并引入EventHandler机制:线程间通信。通过EventRunner创建新线程,将耗时的操作放到新线程上执行。这样既不阻塞原来的线程,任务又可以得到合理的处理。主线程使用EventHandler创建子线程,子线程做耗时的下载图片操作,下载完成后,子线程通过EventHandler通知主线程,主线程再更新UI。

运作机制

EventHandler的运作机制如下图所示:

图1 EventHandler的运作机制
用户程序框架之线程间通信EventHandler-开源基础软件社区

使用EventHandler实现线程间通信的主要流程:

  1. EventHandler投递具体的InnerEvent事件或者Runnable任务到EventRunner所创建的线程的事件队列。
  2. EventRunner循环从事件队列中获取InnerEvent事件或者Runnable任务。
  3. 处理事件或任务:
    • 如果EventRunner取出的事件为InnerEvent事件,则触发EventHandler的回调方法并触发EventHandler的处理方法,在新线程上处理该事件。
    • 如果EventRunner取出的事件为Runnable任务,则EventRunner直接在新线程上处理Runnable任务。

接口

接口头文件:${OpenHarmony}\foundation\appexecfwk\standard\interfaces\innerkits\libeventhandler\include

实现文件:${OpenHarmony}\foundation\appexecfwk\standard\libs\libeventhandler\src

C++面向对象开发

EventHandler

接口 参数 返回 描述
EventHandler (const std::shared_ptr<EventRunner> &runner = nullptr) 构造函数,通过EventRunner的引用返回
Current () EventHandler 获取当前线程绑定的EventHandler
SendEvent InnerEvent::Pointer &event, int64_t delayTime = 0, Priority priority = Priority::LOW bool InnerEvent引用,延期时间,优先级
(InnerEvent::Pointer &event, Priority priority) bool 立即执行的InnerEvent
(uint32_t innerEventId, int64_t param, int64_t delayTime) bool
(uint32_t innerEventId, int64_t delayTime = 0, Priority priority = Priority::LOW) bool
(uint32_t innerEventId, Priority priority) bool
(uint32_t innerEventId, const std::shared_ptr<T> *&*object, int64_t delayTime = 0) bool
(uint32_t innerEventId, const std::weak_ptr<T> *&*object, int64_t delayTime = 0) bool
(uint32_t innerEventId, std::unique_ptr<T, D> *&*object, int64_t delayTime = 0) bool
(uint32_t innerEventId, std::unique_ptr<T, D> *&&*object, int64_t delayTime = 0) bool
SendImmediateEvent (InnerEvent::Pointer *&*event) bool
(InnerEvent::Pointer *&&*event) bool
(uint32_t innerEventId, int64_t param = 0) bool
(uint32_t innerEventId, const std::shared_ptr<T> *&*object) bool
(uint32_t innerEventId, const std::weak_ptr<T> *&*object) bool
(uint32_t innerEventId, std::unique_ptr<T, D> *&*object) bool
(uint32_t innerEventId, std::unique_ptr<T, D> *&&*object) bool
SendHighPriorityEvent (InnerEvent::Pointer *&*event, int64_t delayTime = 0) bool
(InnerEvent::Pointer *&&*event, int64_t delayTime = 0) bool
(uint32_t innerEventId, int64_t param = 0, int64_t delayTime = 0) bool
(uint32_t innerEventId, const std::shared_ptr<T> *&*object, int64_t delayTime = 0) bool
(uint32_t innerEventId, std::unique_ptr<T, D> *&*object, int64_t delayTime = 0) bool
(uint32_t innerEventId, std::unique_ptr<T, D> *&&*object, int64_t delayTime = 0) bool
SendTimingEvent InnerEvent::Pointer &event, int64_t taskTime, Priority priority = Priority::LOW bool 指定时间的任务:InnerEvent引用,执行时间,优先级
(InnerEvent::Pointer *&&*event, int64_t taskTime, Priority priority)
(InnerEvent::Pointer *&&*event, int64_t taskTime)
(uint32_t innerEventId, int64_t taskTime, int64_t param)
(uint32_t innerEventId, int64_t taskTime, Priority priority)
(uint32_t innerEventId, int64_t taskTime)
uint32_t innerEventId, const std::shared_ptr<T> *&*object, int64_t taskTime, Priority priority = Priority::LOW)
uint32_t innerEventId, const std::weak_ptr<T> *&*object, int64_t taskTime, Priority priority = Priority::LOW)
uint32_t innerEventId, std::unique_ptr<T, D> *&*object, int64_t taskTime, Priority priority = Priority::LOW)
uint32_t innerEventId, std::unique_ptr<T, D> *&&*object, int64_t taskTime, Priority priority = Priority::LOW)
PostTimingTask (const Callback *&*callback, int64_t taskTime, const std::string *&*name = std::string(), Priority priority = Priority::LOW) bool
(const Callback *&*callback, int64_t taskTime, Priority priority = Priority::LOW)
PostTask (const Callback *&*callback, const std::string *&*name = std::string(), int64_t delayTime = 0, Priority priority = Priority::LOW) bool
(const Callback *&*callback, Priority priority)
(const Callback *&*callback, int64_t delayTime, Priority priority = Priority::LOW)
PostImmediateTask (const Callback *&*callback, const std::string *&*name = std::string()) bool
PostHighPriorityTask (const Callback *&*callback, const std::string *&*name = std::string(), int64_t delayTime = 0) bool
(const Callback *&*callback, int64_t delayTime)
PostIdleTask (const Callback *&*callback, const std::string *&*name = std::string(), int64_t delayTime = 0) bool
(const Callback *&*callback, int64_t delayTime)
SendSyncEvent (InnerEvent::Pointer *&*event, Priority priority = Priority::LOW); bool
(InnerEvent::Pointer *&&*event, Priority priority = Priority::LOW)
(uint32_t innerEventId, int64_t param = 0, Priority priority = Priority::LOW)
(uint32_t innerEventId, Priority priority)
(uint32_t* innerEventId, const std::shared_ptr<T> *&*object, Priority priority = Priority::LOW)
(uint32_t innerEventId, const std::weak_ptr<T> *&*object, Priority priority = Priority::LOW)
(uint32_t innerEventId, std::unique_ptr<T, D> *&*object, Priority priority = Priority::LOW)
(uint32_t innerEventId, std::unique_ptr<T, D> *&&*object, Priority priority = Priority::LOW)
PostSyncTask (const Callback *&*callback, const std::string *&*name, Priority priority = Priority::LOW) bool
(const Callback *&*callback, Priority priority = Priority::LOW)
RemoveAllEvents bool
RemoveEvent (uint32_t innerEventId) bool
(uint32_t innerEventId, int64_t param); bool
RemoveTask (const std::string *&*name); bool
AddFDListener int32_t fileDescriptor, uint32_t events, const std::shared_ptr<FileDescriptorListener> *&*listener); bool
RemoveAFDListeners () bool
RemoveFDListener (int32_t fileDescriptor) bool
SetEventRunner (const std::shared_ptr<EventRunner> *&*runner); void
GetEventRunner EventRunner
DistributeEvent (const InnerEvent::Pointer *&*event) bool
DistributeTimeAction (const InnerEvent::Pointer *&*event, InnerEvent::TimePoint nowStart); bool
DeliveryTimeAction (const InnerEvent::Pointer *&*event, InnerEvent::TimePoint nowStart); bool
Dump (Dumper *&*dumper) void
HasInnerEvent (uint32_t innerEventId) bool 是否存在内部事件
(int64_t param)
GetEventName (const InnerEvent::Pointer *&*event); std::string 获取内部事件名称
IsIdle () bool 返回当前EventHandler是非空闲

总结:

1、SentEvent:发InnerEvent

PostTask:发RunableTask

2、delayTime:延迟时间,默认为0,立即执行

3、Priority:优先级

​ // Event that should be distributed at once if possible.

​ IMMEDIATE = 0,

​ // High priority event, sorted by handle time, should be distributed before low priority event.

​ HIGH,

​ // Normal event, sorted by handle time.

​ LOW,

​ // Event that should be distributed only if no other event right now.

​ IDLE,

4、其他接口多为围绕这些关键点展开,提供更便捷的接口操作

类图

用户程序框架之线程间通信EventHandler-开源基础软件社区

基本流程图时序图

用户程序框架之线程间通信EventHandler-开源基础软件社区

PS:好像叫线程间通信不是太合适,叫线程子任务调度确切些。
参考:https://developer.harmonyos.com/cn/docs/documentation/doc-guides/inter-thread-overview-0000000000038958

更多原创内容请关注:深开鸿技术团队

入门到精通、技巧到案例,系统化分享HarmonyOS开发技术,欢迎投稿和订阅,让我们一起携手前行共建鸿蒙生态。

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
已于2022-2-28 14:09:08修改
3
收藏 3
回复
举报
回复
添加资源
添加资源将有机会获得更多曝光,你也可以直接关联已上传资源 去关联
    相关推荐