1# I/O Intensive Task Development (TaskPool) 2 3 4I/O intensive tasks are tasks that require frequent I/O operations such as disk read/write and network communication. While asynchronous concurrency can address the thread blocking issue for single I/O tasks, it falls short in the case of I/O intensive tasks. This is where multithread concurrency comes into play. 5 6 7The performance focus of I/O intensive tasks is not the CPU processing capability, but the speed and efficiency of I/O operations, since such a task usually requires frequent operations such as disk read/write and network communication. The following uses frequent read/write operations on a system file to simulate concurrency processing of I/O intensive tasks. 8 9 101. Define a concurrency function that internally calls I/O capabilities intensively. 11 ```ts 12 // write.ets 13 import { fileIo } from '@kit.CoreFileKit' 14 15 // Define a concurrency function that internally calls I/O capabilities intensively. 16 // Write data to the file. 17 export async function write(data: string, filePath: string): Promise<void> { 18 let file: fileIo.File = await fileIo.open(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 19 await fileIo.write(file.fd, data); 20 fileIo.close(file); 21 } 22 ``` 23 24 ```ts 25 // Index.ets 26 import { write } from './write' 27 import { BusinessError } from '@kit.BasicServicesKit'; 28 import { taskpool } from '@kit.ArkTS'; 29 import { common } from '@kit.AbilityKit'; 30 31 @Concurrent 32 async function concurrentTest(context: common.UIAbilityContext): Promise<boolean> { 33 let filePath1: string = context.filesDir + "/path1.txt"; // Application file path 34 let filePath2: string = context.filesDir + "/path2.txt"; 35 // Write data to the file cyclically. 36 let fileList: Array<string> = []; 37 fileList.push(filePath1); 38 fileList.push(filePath2) 39 for (let i: number = 0; i < fileList.length; i++) { 40 write('Hello World!', fileList[i]).then(() => { 41 console.info(`Succeeded in writing the file. FileList: ${fileList[i]}`); 42 }).catch((err: BusinessError) => { 43 console.error(`Failed to write the file. Code is ${err.code}, message is ${err.message}`) 44 return false; 45 }) 46 } 47 return true; 48 } 49 ``` 50 512. Use **TaskPool** to execute the concurrency function that contains the intensive I/O operations. Specifically, call [execute()](../reference/apis-arkts/js-apis-taskpool.md#taskpoolexecute) to execute the tasks and process the scheduling result in a callback. For details about how to obtain **filePath1** and **filePath2** in the example, see [Obtaining Application File Paths](../application-models/application-context-stage.md#obtaining-application-file-paths). To use the context in **TaskPool**, prepare the context outside the concurrent function and pass in the context to the concurrent function as an input parameter. 52 53 ```ts 54 // Index.ets 55 @Entry 56 @Component 57 struct Index { 58 @State message: string = 'Hello World'; 59 build() { 60 Row() { 61 Column() { 62 Text(this.message) 63 .fontSize(50) 64 .fontWeight(FontWeight.Bold) 65 .onClick(() => { 66 let context = getContext() as common.UIAbilityContext; 67 68 // Use TaskPool to execute the concurrency function that contains the intensive I/O operations. 69 // In the case of a large array, the distribution of I/O intensive tasks also preempts the UI main thread. Therefore, multiple threads are required. 70 taskpool.execute(concurrentTest, context).then(() => { 71 // Process the scheduling result. 72 console.info("taskpool: execute success") 73 }) 74 }) 75 } 76 .width('100%') 77 } 78 .height('100%') 79 } 80 } 81 ``` 82