• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 使用TaskPool执行独立的耗时任务
2<!--Kit: ArkTS-->
3<!--Subsystem: CommonLibrary-->
4<!--Owner: @lijiamin2025-->
5<!--Designer: @weng-changcheng-->
6<!--Tester: @kirl75; @zsw_zhushiwei-->
7<!--Adviser: @ge-yafang-->
8
9对于独立运行的耗时任务,任务完成后将结果返回给宿主线程。可采用以下方式实现。
10
11下面通过图片加载来说明。
12
131. 实现子线程需要执行的任务。
14
15   ```ts
16   // IconItemSource.ets
17   export class IconItemSource {
18     image: string | Resource = '';
19     text: string | Resource = '';
20
21     constructor(image: string | Resource = '', text: string | Resource = '') {
22       this.image = image;
23       this.text = text;
24     }
25   }
26   ```
27   <!-- @[implement_child_thread_task](https://gitcode.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTS/ArkTsConcurrent/ConcurrentThreadCommunication/InterThreadCommunicationScenario/entry/src/main/ets/managers/IconItemSource.ets) -->
28
29   ```ts
30   // IndependentTask.ets
31   import { IconItemSource } from './IconItemSource';
32
33   // 在TaskPool线程中执行的方法,需要添加@Concurrent注解,否则无法正常调用。
34   @Concurrent
35   export function loadPicture(count: number): IconItemSource[] {
36     let iconItemSourceList: IconItemSource[] = [];
37     // 遍历添加6*count个IconItem的数据
38     for (let index = 0; index < count; index++) {
39       const numStart: number = index * 6;
40       // 此处循环使用6张图片资源
41       iconItemSourceList.push(new IconItemSource('$media:startIcon', `item${numStart + 1}`));
42       iconItemSourceList.push(new IconItemSource('$media:background', `item${numStart + 2}`));
43       iconItemSourceList.push(new IconItemSource('$media:foreground', `item${numStart + 3}`));
44       iconItemSourceList.push(new IconItemSource('$media:startIcon', `item${numStart + 4}`));
45       iconItemSourceList.push(new IconItemSource('$media:background', `item${numStart + 5}`));
46       iconItemSourceList.push(new IconItemSource('$media:foreground', `item${numStart + 6}`));
47     }
48     return iconItemSourceList;
49   }
50   ```
51   <!-- @[implement_child_thread_task](https://gitcode.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTS/ArkTsConcurrent/ConcurrentThreadCommunication/InterThreadCommunicationScenario/entry/src/main/ets/managers/IndependentTask.ets) -->
52
532. 使用TaskPool的execute方法执行任务,加载图片。
54
55   ```ts
56   // Index.ets
57   import { taskpool } from '@kit.ArkTS';
58   import { IconItemSource } from './IconItemSource';
59   import { loadPicture } from './IndependentTask';
60
61   @Entry
62   @Component
63   struct Index {
64     @State message: string = 'Hello World';
65
66     build() {
67       Row() {
68         Column() {
69           Text(this.message)
70             .fontSize(50)
71             .fontWeight(FontWeight.Bold)
72             .onClick(() => {
73               let iconItemSourceList: IconItemSource[] = [];
74               // 创建Task
75               let loadPictureTask: taskpool.Task = new taskpool.Task(loadPicture, 30);
76               // 执行Task,并返回结果
77               taskpool.execute(loadPictureTask).then((res: object) => {
78                 // loadPicture方法的执行结果
79                 iconItemSourceList = res as IconItemSource[];
80                 // 输出结果是:The length of iconItemSourceList is 180
81                 console.info("The length of iconItemSourceList is " + iconItemSourceList.length);
82               })
83             })
84         }
85         .width('100%')
86       }
87       .height('100%')
88     }
89   }
90   ```
91   <!-- @[execute_task](https://gitcode.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTS/ArkTsConcurrent/ConcurrentThreadCommunication/InterThreadCommunicationScenario/entry/src/main/ets/managers/IndependentTimeConsumingTask.ets) -->
92