• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Using TaskPool for Independent Time-Consuming Tasks
2
3For a time-consuming task that runs independently, you only need to return the result to the host thread after the task is executed. There is no context dependency. You can use the approach described in this topic.
4
5This example uses image loading to illustrate the process.
6
71. Implement the task to be executed in a child thread.
8
9   ```ts
10   // IconItemSource.ets
11   export class IconItemSource {
12     image: string | Resource = '';
13     text: string | Resource = '';
14
15     constructor(image: string | Resource = '', text: string | Resource = '') {
16       this.image = image;
17       this.text = text;
18     }
19   }
20   ```
21   <!-- @[implement_child_thread_task](https://gitee.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTS/ArkTsConcurrent/ConcurrentThreadCommunication/InterThreadCommunicationScenario/entry/src/main/ets/managers/IconItemSource.ets) -->
22
23   ```ts
24   // IndependentTask.ets
25   import { IconItemSource } from './IconItemSource';
26
27   // Methods executed in the TaskPool thread must be decorated by @Concurrent. Otherwise, they cannot be called.
28   @Concurrent
29   export function loadPicture(count: number): IconItemSource[] {
30     let iconItemSourceList: IconItemSource[] = [];
31     // Add six IconItem data records.
32     for (let index = 0; index < count; index++) {
33       const numStart: number = index * 6;
34       // Use six images in the loop.
35       iconItemSourceList.push(new IconItemSource('$media:startIcon', `item${numStart + 1}`));
36       iconItemSourceList.push(new IconItemSource('$media:background', `item${numStart + 2}`));
37       iconItemSourceList.push(new IconItemSource('$media:foreground', `item${numStart + 3}`));
38       iconItemSourceList.push(new IconItemSource('$media:startIcon', `item${numStart + 4}`));
39       iconItemSourceList.push(new IconItemSource('$media:background', `item${numStart + 5}`));
40       iconItemSourceList.push(new IconItemSource('$media:foreground', `item${numStart + 6}`));
41     }
42     return iconItemSourceList;
43   }
44   ```
45   <!-- @[implement_child_thread_task](https://gitee.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTS/ArkTsConcurrent/ConcurrentThreadCommunication/InterThreadCommunicationScenario/entry/src/main/ets/managers/IndependentTask.ets) -->
46
472. Use the **execute** method of TaskPool to execute the task, that is, to load the images.
48
49   ```ts
50   // Index.ets
51   import { taskpool } from '@kit.ArkTS';
52   import { IconItemSource } from './IconItemSource';
53   import { loadPicture } from './IndependentTask';
54
55   @Entry
56   @Component
57   struct Index {
58     @State message: string = 'Hello World';
59
60     build() {
61       Row() {
62         Column() {
63           Text(this.message)
64             .fontSize(50)
65             .fontWeight(FontWeight.Bold)
66             .onClick(() => {
67               let iconItemSourceList: IconItemSource[] = [];
68               // Create a task.
69               let loadPictureTask: taskpool.Task = new taskpool.Task(loadPicture, 30);
70               // Execute the task and return the result.
71               taskpool.execute(loadPictureTask).then((res: object) => {
72                 // Execution result of the loadPicture method.
73                 iconItemSourceList = res as IconItemSource[];
74               })
75             })
76         }
77         .width('100%')
78       }
79       .height('100%')
80     }
81   }
82   ```
83   <!-- @[execute_task](https://gitee.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTS/ArkTsConcurrent/ConcurrentThreadCommunication/InterThreadCommunicationScenario/entry/src/main/ets/managers/IndependentTimeConsumingTask.ets) -->
84