taskpool任务执行结束后如何执行回调方法

taskpool任务执行结束后如何执行回调方法

HarmonyOS
2024-01-31 18:32:20
1071浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
barzxl

可参考如下示例:

import taskpool from '@ohos.taskpool'; 
 
@Concurrent 
function imageProcessing(dataSlice: ArrayBuffer) { 
  // 步骤1: 具体的图像处理操作及其他耗时操作 
  return dataSlice; 
} 
 
function histogramStatistic(pixelBuffer: ArrayBuffer) { 
  // 步骤2: 分成三段并发调度 
  let number = pixelBuffer.byteLength / 3; 
  let buffer1 = pixelBuffer.slice(0, number); 
  let buffer2 = pixelBuffer.slice(number, number * 2); 
  let buffer3 = pixelBuffer.slice(number * 2); 
  let task1 = new taskpool.Task(imageProcessing, buffer1); 
  let task2 = new taskpool.Task(imageProcessing, buffer2); 
  let task3 = new taskpool.Task(imageProcessing, buffer3); 
  taskpool.execute(task1).then((ret: ArrayBuffer[]) => { 
    // 步骤3: 结果处理 
  }); 
  taskpool.execute(task2).then((ret: ArrayBuffer[]) => { 
    // 步骤3: 结果处理 
  }); 
  taskpool.execute(task3).then((ret: ArrayBuffer[]) => { 
    // 步骤3: 结果处理 
  }); 
} 
 
@Entry 
@Component 
struct Index { 
  @State message: string = 'Hello World' 
  build() { 
    Row() { 
      Column() { 
        Text(this.message) 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
          .onClick(() => { 
            let data: ArrayBuffer; 
            histogramStatistic(data); 
          }) 
      } 
      .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.
分享
微博
QQ
微信
回复
2024-02-01 21:35:54


相关问题
如何利用taskpool执行任务
2932浏览 • 1回复 待解决
HarmonyOS Scroll方法执行改变
765浏览 • 1回复 待解决
NAPI执行上层时,如何获取env
3129浏览 • 1回复 待解决
HarmonyOS zlib.compressFile不执行
546浏览 • 1回复 待解决
@Watch装饰器的执行顺序
946浏览 • 1回复 待解决
如何在NAPI执行上层时获取env
790浏览 • 1回复 待解决
HarmonyOS 如何实现后台执行任务
616浏览 • 1回复 待解决