中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
如何使用TaskPool执行任务组?
微信扫码分享
import taskpool from '@ohos.taskpool'; @Concurrent function imageProcessing(dataSlice: ArrayBuffer): ArrayBuffer { // 步骤1: 具体的图像处理操作及其他耗时操作 return dataSlice; } 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); let group: taskpool.TaskGroup = new taskpool.TaskGroup(); group.addTask(imageProcessing, buffer1); group.addTask(imageProcessing, buffer2); group.addTask(imageProcessing, buffer3); taskpool.execute(group, taskpool.Priority.HIGH).then((ret: Object) => { // 步骤3: 结果数组汇总处理 }) } @Entry @Component struct Index { @State message: string = 'Hello World' build() { Row() { Column() { Text(this.message) .fontSize(50) .fontWeight(FontWeight.Bold) .onClick(() => { let buffer: ArrayBuffer = new ArrayBuffer(24); histogramStatistic(buffer); }) } .width('100%') } .height('100%') } }