1# Using TaskPool for Multiple Time-Consuming Tasks
2
3If multiple tasks are executed simultaneously, their execution time and result return time vary according to the task complexity. If the main thread requires the execution results of all tasks, you can use the following code snippet:
4
5In addition, if a task needs to process a large amount of data (for example, a list contains 10,000 data records), it is time-consuming to process all the data in one task. In this case, you can split the data into multiple sublists, allocate one task for each sublist, and combine the results of all the tasks. This pattern reduces the processing time and improves user experience.
6
7The following uses image loading of a plurality of tasks as an example for description.
8
91. Implement the task to be executed by the sub-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
24   ```ts
25   // IndependentTask.ets
26   import { IconItemSource } from './IconItemSource';
27
28   @Concurrent // Methods executed in the task must be decorated by @Concurrent. Otherwise, they cannot be called.
29   @Concurrent
30   export function loadPicture(count: number): IconItemSource[] {
31     let iconItemSourceList: IconItemSource[] = [];
32     // Traverse and add six IconItem data records.
33     for (let index = 0; index < count; index++) {
34       const numStart: number = index * 6;
35       // Six images are used cyclically.
36       iconItemSourceList.push(new IconItemSource('$media:startIcon', `item${numStart + 1}`));
37       iconItemSourceList.push(new IconItemSource('$media:background', `item${numStart + 2}`));
38       iconItemSourceList.push(new IconItemSource('$media:foreground', `item${numStart + 3}`));
39       iconItemSourceList.push(new IconItemSource('$media:startIcon', `item${numStart + 4}`));
40       iconItemSourceList.push(new IconItemSource('$media:background', `item${numStart + 5}`));
41       iconItemSourceList.push(new IconItemSource('$media:foreground', `item${numStart + 6}`));
42
43     }
44     return iconItemSourceList;
45   }
46   ```
47
482. In this scenario, all the tasks to be executed are placed in a task group. After all the tasks in the task group are executed, the execution result of each task is placed in an array and returned to the main thread. The main thread can obtain all task execution results at a time.
49
50   ```ts
51   // MultiTask.ets
52   import { taskpool } from '@kit.ArkTS';
53   import { IconItemSource } from './IconItemSource';
54   import { loadPicture } from './IndependentTask';
55
56   let iconItemSourceList: IconItemSource[][];
57
58   let taskGroup: taskpool.TaskGroup = new taskpool.TaskGroup();
59   taskGroup.addTask(new taskpool.Task(loadPicture, 30));
60   taskGroup.addTask(new taskpool.Task(loadPicture, 20));
61   taskGroup.addTask(new taskpool.Task(loadPicture, 10));
62   taskpool.execute(taskGroup).then((ret: object) => {
63     let tmpLength = (ret as IconItemSource[][]).length
64     for (let i = 0; i < tmpLength; i++) {
65       for (let j = 0; j < ret[i].length; j++) {
66         iconItemSourceList.push(ret[i][j]);
67       }
68     }
69   })
70   ```
71