原文标题:OpenHarmony TaskPool 指定任务并发度场景
作者/来源:OpenHarmony docs contributors / OpenHarmony docs
许可证/授权:Creative Commons Attribution 4.0 International (CC BY 4.0)
转载声明:本文完整转载自 OpenHarmony docs 开源文档仓库,按 CC BY 4.0 署名转载。 本站转载仅用于技术学习与资料归档,已注明作者/来源与原文链接。如有侵权或不希望转载,请联系删除。
TaskPool指定任务并发度场景
TaskPool支持使用异步队列来控制任务的并发度,能有效避免资源过载,减少任务阻塞,适用于网络请求、视频流处理和数据库操作等场景。
此处提供使用TaskPool创建异步队列的开发指导,以相机预览流采集数据处理的功能为例。
由于处理过程是一个频繁且耗时的任务,当相机采集速度过快时,将丢弃之前的采集数据,仅保留最新的一帧数据进行处理。
-
导入需要用到的模块。
// TaskpoolAsyncLevel.ets import { taskpool } from '@kit.ArkTS'; import { BusinessError } from '@kit.BasicServicesKit'; import { PromptAction } from '@kit.ArkUI';
-
定义耗时任务。
// TaskpoolAsyncLevel.ets @Concurrent function collectFrame() { // 采集数据,并且进行处理 // 模拟处理过程,这里是个耗时任务,持续时间为30秒 let t = new Date().getTime() while (new Date().getTime() - t < 30000) { continue; } console.info('collectFrame finished'); }
-
创建异步队列并执行采集任务。
// TaskpoolAsyncLevel.ets @Entry @Component struct TaskpoolAsyncLevel { @State message: string = '触发采集任务'; @State returnMessage: string = 'return...'; @State promptAction: PromptAction = this.getUIContext().getPromptAction(); build() { Row() { Column() { Button(this.message) .fontSize(50) .fontWeight(FontWeight.Bold) .onClick(async () => { // 创建并发度为5的异步队列,等待队列个数为5,当加入的任务数量超过5时,等待列表中处于队头的任务会被丢弃 let asyncRunner:taskpool.AsyncRunner = new taskpool.AsyncRunner('async', 5, 5); // 触发采集任务 for (let i = 0; i < 20; i++) { let task:taskpool.Task = new taskpool.Task(`async${i}`,collectFrame); asyncRunner.execute(task).then(() => { console.info('the current task name is ' + task.name); }).catch((e:BusinessError) => { console.error('async: error is ' + e); }); } console.info('asyncRunner task finished'); this.returnMessage = 'asyncRunner task finished'; this.promptAction.showToast({ message: this.returnMessage }); }) // ... } .width('100%') } .height('100%') } }
发表评论