harmony 鸿蒙I/O Intensive Task Development

  • 2023-10-30
  • 浏览 (165)

I/O Intensive Task Development

Asynchronous concurrency can solve the problem of a single blocking I/O operation. In the case of I/O intensive tasks, the execution of other tasks in the thread is still blocked. To resolve this issue, multithread concurrency is introduced.

The 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.

  1. Define a concurrency function that internally calls I/O capabilities intensively.

    // a.ts
    import fs from '@ohos.file.fs';
    
    
    // Define a concurrency function that internally calls I/O capabilities intensively.
    // Implement file writing.
    export async function write(data: string, filePath: string): Promise<void> {
      let file: fs.File = await fs.open(filePath, fs.OpenMode.READ_WRITE);
      await fs.write(file.fd, data);
      fs.close(file);
    }
    
    import { write } from './a'
    import { BusinessError } from '@ohos.base';
    
    
    @Concurrent
    async function concurrentTest(fileList: string[]): Promise<boolean> {
      // Write the file cyclically.
      for (let i: number = 0; i < fileList.length; i++) {
        write('Hello World!', fileList[i]).then(() => {
          console.info(`Succeeded in writing the file. FileList: ${fileList[i]}`);
        }).catch((err: BusinessError) => {
          console.error(`Failed to write the file. Code is ${err.code}, message is ${err.message}`)
          return false;
        })
      }
      return true;
    }
    
  2. Use TaskPool to execute the concurrency function that contains the intensive I/O operations. Specifically, call execute() 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.

    import taskpool from '@ohos.taskpool';
    
    
    let filePath1: string = "path1"; // Application file path.
    let filePath2: string = "path2";
    
    
    // Use TaskPool to execute the concurrency function that contains the intensive I/O operations.
    // In the case of a large array, the distribution of I/O intensive tasks also preempts the main thread. Therefore, multiple threads are required.
    taskpool.execute(concurrentTest, [filePath1, filePath2]).then(() => {
      // Process the scheduling result.
    })
    

你可能感兴趣的鸿蒙文章

harmony 鸿蒙ArkTS Common Library

harmony 鸿蒙Comparison Between the Actor and Memory Sharing Models

harmony 鸿蒙Overview of ArkTS Common Library

harmony 鸿蒙Asynchronous Concurrency Overview

harmony 鸿蒙Concurrency Overview

harmony 鸿蒙Overview of Containers

harmony 鸿蒙CPU Intensive Task Development

harmony 鸿蒙Linear Containers

harmony 鸿蒙Multithread Concurrency Overview

harmony 鸿蒙Nonlinear Containers

0  赞