HarmonyOS emitter相关

同个系列的异步任务按理说emitter监听的eventid都会是一样的。列如10个异步任务都是掉同一个方法来实现。该方法是接口调用,那接口返回的处理逻辑就都会是一样。因此emitter通知的那部分代码也是一样的,可能就是传参不同。如果线程池里边的异步任务连续触发emitter,会不会导致前一个被后一个覆盖呢?如果说会被覆盖,那采用eventid不一致的方式监听,在一开始注册的时候不就得注册很多个id。如果是未知个数的异步任务,注册的时候没法确定需要注册几个emitter。

HarmonyOS
2025-01-09 13:32:30
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

不会覆盖,参考示例如下:

import taskpool from '@ohos.taskpool';
import emitter from '@ohos.events.emitter';

@Concurrent
function imageProcessing(dataSlice: ArrayBuffer): ArrayBuffer {
  // 步骤1: 具体的图像处理操作及其他耗时操作
  return dataSlice;
}

let event: emitter.InnerEvent = {
  eventId: 1
};

let callback = (eventData: emitter.EventData): void => {
  console.log('event callback:' + JSON.stringify(eventData.data?.content))

};

function histogramStatistic(pixelBuffer: ArrayBuffer): void {
  // 步骤2: 分成三段并发调度
  let number: number = pixelBuffer.byteLength / 3;
  let buffer1: ArrayBuffer = pixelBuffer.slice(0, number);
  let buffer2: ArrayBuffer = pixelBuffer.slice(number, number * 2);
  let buffer3: ArrayBuffer = pixelBuffer.slice(number * 2);

  taskpool.execute(imageProcessing, buffer1).then(() => {
    console.log("111")
    let eventData: emitter.EventData = {
      data: {
        content: 'a',
        id: 1,
        isEmpty: false
      }
    };
    emitter.emit(event, eventData);
  })
  taskpool.execute(imageProcessing, buffer2).then(() => {
    let eventData: emitter.EventData = {
      data: {
        content: 'b',
        id: 1,
        isEmpty: false
      }
    };
    console.log("222")
    emitter.emit(event, eventData);
  })
  taskpool.execute(imageProcessing, buffer3).then(() => {
    let eventData: emitter.EventData = {
      data: {
        content: 'c',
        id: 1,
        isEmpty: false
      }
    };
    console.log("333")
    emitter.emit(event, eventData);
  })

}

@Entry
@Component
struct Index {
  @State message: string = '发送请求'

  build() {
    Row() {
      Column() {
        Button("订阅事件").onClick(() => {
          emitter.on(event, callback);
        })
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            let buffer: ArrayBuffer = new ArrayBuffer(24);
            histogramStatistic(buffer);
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.

log 输出:

08-15 14:03:21.263   2772-2772     A03d00/JSAPP                    com.xxx  I     111
08-15 14:03:21.264   2772-2772     A03d00/JSAPP                    com.xxx  I     222
08-15 14:03:21.264   2772-2772     A03d00/JSAPP                    com.xxx  I     333
08-15 14:03:21.264   2772-2772     A03d00/JSAPP                    com.xxx  I     event callback:"a"
08-15 14:03:21.264   2772-2772     A03d00/JSAPP                    com.xxx  I     event callback:"b"
08-15 14:03:21.264   2772-2772     A03d00/JSAPP                    com.xxx  I     event callback:"c"
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
分享
微博
QQ
微信
回复
2025-01-09 16:28:34
相关问题
HarmonyOS emitter.on相关咨询
837浏览 • 1回复 待解决
HarmonyOS emitter注销方式
939浏览 • 1回复 待解决
HarmonyOS emitter关闭问题
1110浏览 • 1回复 待解决
HarmonyOS 关于emitter.EventData
937浏览 • 1回复 待解决
HarmonyOS 事件订阅 emitter问题
1572浏览 • 1回复 待解决
HarmonyOS emitter的性能问题
847浏览 • 1回复 待解决
HarmonyOS emitter 线程切换问题
1240浏览 • 1回复 待解决
HarmonyOS emitter传输数据问题
987浏览 • 1回复 待解决
HarmonyOS emitter事件处理问题
1113浏览 • 1回复 待解决
关于emitter、eventHub的使用场景
4253浏览 • 2回复 待解决
通过emitter实现worker间线程通讯
1891浏览 • 1回复 待解决
订阅emitter分享,谁有更好的办法?
1371浏览 • 1回复 待解决
Emitter如何声明回调函数类型
2406浏览 • 1回复 待解决
Emitter与EventHub的区别是什么?
1873浏览 • 2回复 待解决
HarmonyOS emitter.off 不执行会不会有问题
1159浏览 • 1回复 待解决