harmony 鸿蒙Using Worker for Inter-Thread Communication

  • 2023-02-03
  • 浏览 (515)

Using Worker for Inter-Thread Communication

Worker is an independent thread running in parallel with the main thread. The thread that creates the worker thread is referred to as the host thread. The script file passed in during worker creation is executed in the worker thread. Generally, time-consuming operations are processed in the worker thread. However, pages cannot be directly updated in the worker thread.

To develop the Worker mode, perform the following steps:

  1. Configure the buildOption field in the module-level build-profile.json5 file of the project.
   "buildOption": {
     "sourceOption": {
       "workers": [
         "./src/main/ets/workers/worker.ts"
       ]
     }
   }
  1. Create the worker.ts file based on the configuration in build-profile.json5.
   import worker from '@ohos.worker';

   let parent = worker.workerPort;

   // Process messages from the main thread.
   parent.onmessage = (message) => {
     console.info("onmessage: " + message)
     // Send a message to the main thread.
     parent.postMessage("message from worker thread.")
   }
  1. In the main thread, use the following method to initialize and use the worker thread.

    • Stage model:
      import worker from '@ohos.worker';
    
    
      let wk = new worker.ThreadWorker("entry/ets/workers/worker.ts");
    
    
      // Send a message to the worker thread.
      wk.postMessage("message from main thread.")
    
    
      // Process messages from the worker thread.
      wk.onmessage = (message) => {
        console.info("message from worker: " + message)
    
    
        // Stop the worker thread based on service requirements.
        wk.terminate();
      }
    
    • FA model:
      import worker from '@ohos.worker';
    
    
      let wk = new worker.ThreadWorker("../workers/worker.ts");
    
    
      // Send a message to the worker thread.
      wk.postMessage("message from main thread.")
    
    
      // Process messages from the worker thread.
      wk.onmessage = (message) => {
        console.info("message from worker: " + message)
    
    
        // Stop the worker thread based on service requirements.
        wk.terminate();
      }
    

NOTE

  • If the relative path of worker.ts configured in build-profile.json5 is ./src/main/ets/workers/worker.ts, pass in the path entry/ets/workers/worker.ts when creating a worker thread in the stage model, and pass in the path ../workers/worker.ts when creating a worker thread in the FA model.
  • For details about the data types supported between the main thread and worker thread, see Sequenceable Data Types.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Application Models

harmony 鸿蒙Using Explicit Want to Start an Application Component

harmony 鸿蒙Using Implicit Want to Open a Website

harmony 鸿蒙AbilityStage Component Container

harmony 鸿蒙Accessing a DataAbility

harmony 鸿蒙Accessing a DataShareExtensionAbility from the FA Model

harmony 鸿蒙AccessibilityExtensionAbility

harmony 鸿蒙Common action and entities Values

harmony 鸿蒙API Switching Overview

harmony 鸿蒙Switching of app and deviceConfig

0  赞