harmony 鸿蒙使用TaskPool执行独立的耗时任务

  • 2025-06-12
  • 浏览 (3)

使用TaskPool执行独立的耗时任务

对于独立运行的耗时任务,任务执行完毕后将结果返回给宿主线程,没有上下文依赖,可采用以下方式实现。

下面通过图片加载来说明。

  1. 实现子线程需要执行的任务。
   // IconItemSource.ets
   export class IconItemSource {
     image: string|Resource = '';
     text: string|Resource = '';
   
     constructor(image: string|Resource = '', text: string|Resource = '') {
       this.image = image;
       this.text = text;
     }
   }

   // IndependentTask.ets
   import { IconItemSource } from './IconItemSource';
    
   // 在TaskPool线程中执行的方法,需要添加@Concurrent注解,否则无法正常调用。
   @Concurrent
   export function loadPicture(count: number): IconItemSource[] {
     let iconItemSourceList: IconItemSource[] = [];
     // 遍历添加6*count个IconItem的数据
     for (let index = 0; index < count; index++) {
       const numStart: number = index * 6;
       // 此处循环使用6张图片资源
       iconItemSourceList.push(new IconItemSource('$media:startIcon', `item${numStart + 1}`));
       iconItemSourceList.push(new IconItemSource('$media:background', `item${numStart + 2}`));
       iconItemSourceList.push(new IconItemSource('$media:foreground', `item${numStart + 3}`));
       iconItemSourceList.push(new IconItemSource('$media:startIcon', `item${numStart + 4}`));
       iconItemSourceList.push(new IconItemSource('$media:background', `item${numStart + 5}`));
       iconItemSourceList.push(new IconItemSource('$media:foreground', `item${numStart + 6}`));
     }
     return iconItemSourceList;
   }

  1. 使用TaskPool中的execute方法执行上述任务,加载图片。
   // Index.ets
   import { taskpool } from '@kit.ArkTS';
   import { IconItemSource } from './IconItemSource';
   import { loadPicture } from './IndependentTask';
   
   @Entry
   @Component
   struct Index {
     @State message: string = 'Hello World';
   
     build() {
       Row() {
         Column() {
           Text(this.message)
             .fontSize(50)
             .fontWeight(FontWeight.Bold)
             .onClick(() => {
               let iconItemSourceList: IconItemSource[] = [];
               // 创建Task
               let loadPictureTask: taskpool.Task = new taskpool.Task(loadPicture, 30);
               // 执行Task,并返回结果
               taskpool.execute(loadPictureTask).then((res: object) => {
                 // loadPicture方法的执行结果
                 iconItemSourceList = res as IconItemSource[];
               })
             })
         }
         .width('100%')
       }
       .height('100%')
     }
   }

你可能感兴趣的鸿蒙文章

harmony 鸿蒙ArkTS(方舟编程语言)

harmony 鸿蒙在build-profile.json5中配置arkOptions

harmony 鸿蒙异步锁

harmony 鸿蒙方舟字节码文件格式

harmony 鸿蒙方舟字节码函数命名规则

harmony 鸿蒙方舟字节码基本原理

harmony 鸿蒙方舟字节码概述

harmony 鸿蒙共享容器

harmony 鸿蒙异步等待

harmony 鸿蒙ArkTS跨语言交互

0  赞