• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Using TaskPool for Multiple Time-Consuming Tasks
2
3When multiple tasks are executed simultaneously, the execution time and the time to return data may vary due to differences in task complexity. If the host thread requires data from all tasks once they are completed, this can be achieved using [TaskGroup](../reference/apis-arkts/js-apis-taskpool.md#taskgroup10).
4
5Additionally, if the volume of data to be processed is large (for example, a list with 10,000 items), processing all the data in a single task can be time-consuming. Instead, you can split the original data into multiple sublists and assign each sublist to an independent task. After all tasks are completed, you can combine the results into a complete dataset. This approach can reduce processing time and enhance user experience.
6
7This example uses image loading of multiple tasks to illustrate the process.
8
91. Implement the task to be executed in a child thread.
10
11   ```ts
12   // IconItemSource.ets
13   export class IconItemSource {
14     image: string | Resource = '';
15     text: string | Resource = '';
16
17     constructor(image: string | Resource = '', text: string | Resource = '') {
18       this.image = image;
19       this.text = text;
20     }
21   }
22   ```
23   <!-- @[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) -->
24
25   ```ts
26   // IndependentTask.ets
27   import { IconItemSource } from './IconItemSource';
28
29   // Methods executed in the TaskPool thread must be decorated by @Concurrent. Otherwise, they cannot be called.
30   @Concurrent
31   export function loadPicture(count: number): IconItemSource[] {
32     let iconItemSourceList: IconItemSource[] = [];
33     // Add six IconItem data records.
34     for (let index = 0; index < count; index++) {
35       const numStart: number = index * 6;
36       // Use six images in the loop.
37       iconItemSourceList.push(new IconItemSource('$media:startIcon', `item${numStart + 1}`));
38       iconItemSourceList.push(new IconItemSource('$media:background', `item${numStart + 2}`));
39       iconItemSourceList.push(new IconItemSource('$media:foreground', `item${numStart + 3}`));
40       iconItemSourceList.push(new IconItemSource('$media:startIcon', `item${numStart + 4}`));
41       iconItemSourceList.push(new IconItemSource('$media:background', `item${numStart + 5}`));
42       iconItemSourceList.push(new IconItemSource('$media:foreground', `item${numStart + 6}`));
43
44     }
45     return iconItemSourceList;
46   }
47   ```
48   <!-- @[implement_child_thread_task](https://gitee.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTS/ArkTsConcurrent/ConcurrentThreadCommunication/InterThreadCommunicationScenario/entry/src/main/ets/managers/TaskSendDataUsage.ets) -->
49
502. Add the tasks to a task group and execute them collectively. When all the tasks in the task group finish executing, their results are placed in an array and sent back to the host thread. In this way, the host thread can access the combined results of all tasks at once, rather than receiving results individually as each task completes.
51
52   ```ts
53   // MultiTask.ets
54   import { taskpool } from '@kit.ArkTS';
55   import { IconItemSource } from './IconItemSource';
56   import { loadPicture } from './IndependentTask';
57
58   let iconItemSourceList: IconItemSource[][];
59
60   let taskGroup: taskpool.TaskGroup = new taskpool.TaskGroup();
61   taskGroup.addTask(new taskpool.Task(loadPicture, 30));
62   taskGroup.addTask(new taskpool.Task(loadPicture, 20));
63   taskGroup.addTask(new taskpool.Task(loadPicture, 10));
64   taskpool.execute(taskGroup).then((ret: object) => {
65     let tmpLength = (ret as IconItemSource[][]).length
66     for (let i = 0; i < tmpLength; i++) {
67       for (let j = 0; j < ret[i].length; j++) {
68         iconItemSourceList.push(ret[i][j]);
69       }
70     }
71   })
72   ```
73   <!-- @[execute_task_group](https://gitee.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTS/ArkTsConcurrent/ConcurrentThreadCommunication/InterThreadCommunicationScenario/entry/src/main/ets/managers/MultiTask.ets) -->
74