1e41f4b71Sopenharmony_ci# Worker Introduction 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciWith the Worker module, you can provide a multithreaded environment for an application, so that the application can perform a time-consuming operation in a background thread. This greatly prevents a computing-intensive or high-latency task from blocking the running of the main thread. For details about the APIs and their usage, see [Worker](../reference/apis-arkts/js-apis-worker.md). 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ci## Worker Operating Mechanism 7e41f4b71Sopenharmony_ci 8e41f4b71Sopenharmony_ci**Figure 1** Worker operating mechanism 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ciThe thread that creates the worker thread is referred to as the host thread (not necessarily the main thread, since a worker thread can also create another worker thread). A worker thread is also named an actor thread. Each worker thread has an instance independent from the host thread, including the infrastructure, objects, and code segments. Memory overhead exists when each worker thread is started, and therefore the number of worker threads needs to be limited. The worker thread communicates with the host thread by means of message exchange. They use the serialization technique to exchange commands and data. 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci## Precautions for Worker 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci- A worker thread can be created manually or automatically. In manual creation mode, you must also perform related configurations. For details, see [Precautions for Creating a Worker Thread](#precautions-for-creating-a-worker-thread). 18e41f4b71Sopenharmony_ci- The URL of the worker thread file passed in to the constructor function varies according to the version in use. For details, see [Precautions for File URLs](#precautions-for-file-urls). 19e41f4b71Sopenharmony_ci- After a worker thread is created, you must manually manage its lifecycle. A maximum of 64 worker threads can run simultaneously. For details, see [Lifecycle Precautions](#lifecycle-precautions). 20e41f4b71Sopenharmony_ci- Context objects vary in different threads. Therefore, the worker thread can use only a thread-safe library, but not a non-thread-safe library (for example, UI-related non-thread-safe library). For details, see [Precautions for Multithread Safe](multi-thread-safety.md). 21e41f4b71Sopenharmony_ci- A maximum of 16 MB data can be serialized. 22e41f4b71Sopenharmony_ci- You must register the **onerror** API in the main thread to listen for worker thread errors, which might cause a JavaScript crash. 23e41f4b71Sopenharmony_ci- Worker thread files cannot be used across HAPs. 24e41f4b71Sopenharmony_ci- During the creation of a worker object, the worker thread files of the current module can be loaded, but those of other modules cannot. To use the worker provided by another module, encapsulate the entire worker logic into a method, export the method, and then import the method. 25e41f4b71Sopenharmony_ci- Before referencing the HAR or HSP, configure the dependency on the HAR or HSP. For details, see [Referencing a Shared Package](https://developer.huawei.com/consumer/en/doc/harmonyos-guides-V5/ide-har-import-V5). 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci### Precautions for Creating a Worker Thread 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ciThe worker thread file must be stored in the ***{moduleName}*/src/main/ets/** directory. Otherwise, it will not be packed into the application. A worker thread can be created manually or automatically. 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci- Manual creation: Manually create the directory and file, and configure the related field in **build-profile.json5** so that the file can be packed into the application. 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci Stage model: 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci ```json 37e41f4b71Sopenharmony_ci "buildOption": { 38e41f4b71Sopenharmony_ci "sourceOption": { 39e41f4b71Sopenharmony_ci "workers": [ 40e41f4b71Sopenharmony_ci "./src/main/ets/workers/worker.ets" 41e41f4b71Sopenharmony_ci ] 42e41f4b71Sopenharmony_ci } 43e41f4b71Sopenharmony_ci } 44e41f4b71Sopenharmony_ci ``` 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ci FA model: 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_ci ```json 49e41f4b71Sopenharmony_ci "buildOption": { 50e41f4b71Sopenharmony_ci "sourceOption": { 51e41f4b71Sopenharmony_ci "workers": [ 52e41f4b71Sopenharmony_ci "./src/main/ets/MainAbility/workers/worker.ets" 53e41f4b71Sopenharmony_ci ] 54e41f4b71Sopenharmony_ci } 55e41f4b71Sopenharmony_ci } 56e41f4b71Sopenharmony_ci ``` 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci- Automatic creation: DevEco Studio supports one-click generation of worker threads. Right-click any position in the {moduleName} directory and choose **New > Worker** to generate the template file and configuration information of the worker thread. You do not need to configure the field in **build-profile.json5**. 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ci### Precautions for File URLs 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ciBefore calling an API of the Worker module, you must create a **Worker** instance. The constructor function varies in different API versions, and the URL of the worker thread file must be passed in to **scriptURL** of the function. 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci```ts 66e41f4b71Sopenharmony_ci// Import the worker module. 67e41f4b71Sopenharmony_ciimport { worker } from '@kit.ArkTS'; 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci// Use the following function in API version 9 and later versions: 70e41f4b71Sopenharmony_ciconst worker1: worker.ThreadWorker = new worker.ThreadWorker('entry/ets/workers/MyWorker.ets'); 71e41f4b71Sopenharmony_ci// Use the following function in API version 8 and earlier versions: 72e41f4b71Sopenharmony_ciconst worker2: worker.Worker = new worker.Worker('entry/ets/workers/MyWorker.ets'); 73e41f4b71Sopenharmony_ci``` 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ci#### File URL Rules in the Stage Model 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ciThe requirements for **scriptURL** in the constructor function are as follows: 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ci- **scriptURL** consists of {moduleName}/ets and {relativePath}. 80e41f4b71Sopenharmony_ci- {relativePath} is the relative path of the worker thread file to the ***{moduleName}*/src/main/ets/** directory. 81e41f4b71Sopenharmony_ci 82e41f4b71Sopenharmony_ci(1) Loading a worker thread file of an ability 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ciTo load the worker thread file of an ability, use the URL {moduleName}/ets/{relativePath}. 85e41f4b71Sopenharmony_ci 86e41f4b71Sopenharmony_ci```ts 87e41f4b71Sopenharmony_ciimport { worker } from '@kit.ArkTS'; 88e41f4b71Sopenharmony_ci 89e41f4b71Sopenharmony_ci// URL of the worker thread file: "entry/src/main/ets/workers/worker.ets" 90e41f4b71Sopenharmony_ciconst workerStage1: worker.ThreadWorker = new worker.ThreadWorker('entry/ets/workers/worker.ets'); 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ci// URL of the worker thread file: "phone/src/main/ets/ThreadFile/workers/worker.ets" 93e41f4b71Sopenharmony_ciconst workerStage2: worker.ThreadWorker = new worker.ThreadWorker('phone/ets/ThreadFile/workers/worker.ets'); 94e41f4b71Sopenharmony_ci``` 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ci(2) Loading a worker thread file in Library-[HSP](../quick-start/in-app-hsp.md) 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ciTo load the worker thread file in HSP, use the URL {moduleName}/ets/{relativePath}. 99e41f4b71Sopenharmony_ci 100e41f4b71Sopenharmony_ci```ts 101e41f4b71Sopenharmony_ciimport { worker } from '@kit.ArkTS'; 102e41f4b71Sopenharmony_ci 103e41f4b71Sopenharmony_ci// URL of the worker thread file: "hsp/src/main/ets/workers/worker.ets" 104e41f4b71Sopenharmony_ciconst workerStage3: worker.ThreadWorker = new worker.ThreadWorker('hsp/ets/workers/worker.ets'); 105e41f4b71Sopenharmony_ci``` 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ci(3) Loading a worker thread file in Library-[HAR](../quick-start/har-package.md) 108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_ciThe worker thread file in the HAR may be loaded in either of the following cases: 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_ci- @ path loading mode: All types of modules load the worker thread file in the local HAR. The URL is @{moduleName}/ets/{relativePath}. 112e41f4b71Sopenharmony_ci 113e41f4b71Sopenharmony_ci- Relative path loading mode: The local HAR loads the worker thread file in the package. The URL is the relative path of the file where the Worker object is created to the worker thread file. 114e41f4b71Sopenharmony_ci 115e41f4b71Sopenharmony_ci>**NOTE** 116e41f4b71Sopenharmony_ci> 117e41f4b71Sopenharmony_ci> When **useNormalizedOHMUrl** is enabled (the **useNormalizedOHMUrl** field of the **strictMode** attribute in the **build-profile.json5** file at the same level as the entry in the project directory is set to **true**) or the HAR is packed into a third-party package, the worker thread file in the HAR can be loaded using a relative path. 118e41f4b71Sopenharmony_ci 119e41f4b71Sopenharmony_ci```ts 120e41f4b71Sopenharmony_ciimport { worker } from '@kit.ArkTS'; 121e41f4b71Sopenharmony_ci 122e41f4b71Sopenharmony_ci// @ Path loading mode 123e41f4b71Sopenharmony_ci// URL of the worker thread file: "har/src/main/ets/workers/worker.ets" 124e41f4b71Sopenharmony_ciconst workerStage4: worker.ThreadWorker = new worker.ThreadWorker('@har/ets/workers/worker.ets'); 125e41f4b71Sopenharmony_ci 126e41f4b71Sopenharmony_ci// Relative path loading mode: 127e41f4b71Sopenharmony_ci// URL of the worker thread file: "har/src/main/ets/workers/worker.ets" 128e41f4b71Sopenharmony_ci// URL of the file where the Worker object is created: "har/src/main/ets/components/mainpage/MainPage.ets" 129e41f4b71Sopenharmony_ciconst workerStage5: worker.ThreadWorker = new worker.ThreadWorker('../../workers/worker.ets'); 130e41f4b71Sopenharmony_ci``` 131e41f4b71Sopenharmony_ci 132e41f4b71Sopenharmony_ci#### File URL Rules in the FA Model 133e41f4b71Sopenharmony_ci 134e41f4b71Sopenharmony_ci**scriptURL** in the constructor function is the relative path between the worker thread file and "{moduleName}/src/main/ets/MainAbility". 135e41f4b71Sopenharmony_ci 136e41f4b71Sopenharmony_ci```ts 137e41f4b71Sopenharmony_ciimport { worker } from '@kit.ArkTS'; 138e41f4b71Sopenharmony_ci 139e41f4b71Sopenharmony_ci// The following three scenarios are involved. 140e41f4b71Sopenharmony_ci 141e41f4b71Sopenharmony_ci// Scenario 1: URL of the worker thread file: "{moduleName}/src/main/ets/MainAbility/workers/worker.ets" 142e41f4b71Sopenharmony_ciconst workerFA1: worker.ThreadWorker = new worker.ThreadWorker("workers/worker.ets", {name:"first worker in FA model"}); 143e41f4b71Sopenharmony_ci 144e41f4b71Sopenharmony_ci// Scenario 2: URL of the worker thread file: "{moduleName}/src/main/ets/workers/worker.ets" 145e41f4b71Sopenharmony_ciconst workerFA2: worker.ThreadWorker = new worker.ThreadWorker("../workers/worker.ets"); 146e41f4b71Sopenharmony_ci 147e41f4b71Sopenharmony_ci// Scenario 3: URL of the worker thread file: "{moduleName}/src/main/ets/MainAbility/ThreadFile/workers/worker.ets" 148e41f4b71Sopenharmony_ciconst workerFA3: worker.ThreadWorker = new worker.ThreadWorker("ThreadFile/workers/worker.ets"); 149e41f4b71Sopenharmony_ci``` 150e41f4b71Sopenharmony_ci 151e41f4b71Sopenharmony_ci 152e41f4b71Sopenharmony_ci 153e41f4b71Sopenharmony_ci### Lifecycle Precautions 154e41f4b71Sopenharmony_ci 155e41f4b71Sopenharmony_ci- Creating and terminating worker threads consume performance. Therefore, you are advised to manage available workers and reuse them. The worker threads keep running even when they are idle. When a worker thread is not required, call [terminate()](../reference/apis-arkts/js-apis-worker.md#terminate9) or [parentPort.close()](../reference/apis-arkts/js-apis-worker.md#close9) to terminate it. If a worker thread is terminated or being terminated, an error is thrown when it is called. 156e41f4b71Sopenharmony_ci 157e41f4b71Sopenharmony_ci 158e41f4b71Sopenharmony_ci- The number of worker threads is determined by the memory management policy. The required memory threshold is the smaller one between 1.5 GB and 60% of the physical memory of the device. If the memory is sufficient, a maximum of 64 worker threads can run simultaneously. If excess worker threads are to be created, the system displays the error message "Worker initialization failure, the number of workers exceeds the maximum." The number of actually running worker threads is dynamically adjusted based on the memory usage. Once the accumulated memory usage of all worker threads and main threads exceeds the threshold, Out of Memory (OOM) error occurs, and applications may crash. 159